hello everyone
I just beginning my data structure course ad currently i have a problem with stack, I hope you guys can help me.
Ok,this is my code
Code:
/* File : stackeng.h */
/* Type declaration and primitive function prototype for stack type */
#ifndef stack_H
#define stack_H
#include "boolean.h"
#define Nil 0
#define Top(S) (S).Top
#define InfoTop(S) (S).T[(S).Top]
#define Size(S) (S).Size
typedef int infotype;
typedef int address;
typedef struct
{
infotype * T; /*Table for storing stack element*/
address Top; /*(Table index for he top element)*/
int Size; /*stack's size*/
}stack;
/*primitive prototype */
void CreateEmpty(stack * S, int size);
/* Create a empty stack S whose capacity is as big as size,
The Index is between 1 until size + 1,because i don't use the 0 index,
the stack is empty when the stack's Top property has 0 value */
void Push(stack * S, infotype x);
/* add x as elemen stack s
Initial state :stack S can be empty,the array container for stack elements is not full
Final state : x become the new top value in stackS,Stack's S Top atribute is incremented by 1
*/
void Pop(stack * S, infotype * x);
/* Delete x as stack S element
Initial state :stack S can not be empty
Final state : x is a former stack S Top element which had been popped by Pop function
*/
boolean IsEmpty(stack S);
/* check if stack S is empty ,return true if stack S is empty,false is not empty*/
boolean IsFull(stack S);
/* check if stack S is full ,return true if stack S is full,false is not full */
void Destruct(stack * S);
/*Destroy the stack S */
void Grow(stack * S);
/*Makes the stack capacity grows for one additional element
Initial state :stack S maybe full,maybe not.The stack's Size property is A
Final state : The stack's Size property is (A+1)
*/
#endif
Code:
/*Stack's Body primitive function */
#include "stackeng.h"
#include "boolean.h"
/*BODY PROTOTYPE*/
void CreateEmpty(stack * S, int size)
/* Create a empty stack S whose capacity is as big as size,
The Index is between 1 until size + 1,because i don't use the 0 index,
the stack is empty when the stack's Top property has 0 value */
{
(*S).T = (infotype *) malloc ((size + 1) * sizeof(infotype));
Top(*S) = Nil;
Size(*S) = size;
}
void Push(stack * S, infotype x)
/* add x as elemen stack s
Initial state :stack S can be empty,the array container for stack elements is not full
Final state : x become the new top value in stackS,Stack's S Top atribute is incremented by 1
*/
{
Top(*S)++;
InfoTop(*S) = x;
}
void Pop(stack * S, infotype * x)
/* Delete x as stack S element
Initial state :stack S can not be empty
Final state : x is a former stack S Top element which had been popped by Pop function
*/
{
*x = InfoTop(*S);
Top(*S)--;
}
boolean IsEmpty(stack S)
/* check if stack S is empty ,return true if stack S is empty,false is not empty*/
{
return Top(S) == Nil;
}
boolean IsFull(stack S)
/* check if stack S is full ,return true if stack S is full,false is not full */
{
return Top(S) == S.Size;
}
void Destruct(stack * S)
/*Destroy the stack S */
{
free((*S).T);
Top(*S) = Nil;
Size(*S) = 0;
}
void Grow(stack * S)
/*Makes the stack capacity grows for one additional element
Initial state :stack S maybe full,maybe not.The stack's Size property is A
Final state : The stack's Size property is (A+1)
*/
{
stack S2; /*(temporary stack initialisation)*/
CreateEmpty(&S2,Size(*S)); /*(make stack S2 empty )*/
S2 = (*S);/*(copy stack S to stack S2)*/
Destruct(&S);/*Destroy the stack S */
CreateEmpty(&S,(Size(*S2) + 1));/*(Create the new stack S whose size is one element bigger than the old one)*/
S = &S2;/*(copy stack S2 to stack S)*/
}
Code:
/* File: mstackeng.c*/
/*This file is writeen to check whether the primitive function can be implemented in main program or not */
#include <stdio.h>
#include "boolean.h"
#include "stackeng.c"
main ()
{
stack S1;
CreateEmpty(&S1,9);
if(IsEmpty(S1))
{
printf ("push works well\n");
Push(&S1,5);
}
int x;
Pop(&S1,&x);
printf ("pop works well %d\n",x);
Push(&S1,5);
Push(&S1,5);
Push(&S1,5);
Grow(&S1);
int z;
scanf ("%d",&z);
return 0;
}
When I compiled file mstackeng.c with Bloodshed Dev C++,i got this following error messages:
1.line 7 E:\stack\mstackeng.c In file included from E:\stack\mstackeng.c
E:\stack\stackeng.c In function `Grow':
2.line 67 E:\stack\stackeng.c [Warning] passing arg 1 of `Destruct' from incompatible pointer type
3.line 68 E:\stack\stackeng.c invalid type argument of `unary *'
4.line 68 E:\stack\stackeng.c [Warning] passing arg 1 of `CreateEmpty' from incompatible pointer type
I tried best to debug my program but it still won' budge.I think the misuse of pointer is the cause of this problem,but i still don't understand how to fix it.
Can you help me guys?
Thank you