Previous references to Weiss's data structure and algorithm analysis have written two essays
An array implementation of stack ADT implemented by the linked list of the stack ADT
Because of the reasons for the postgraduate examination, now read the Min "data structure C version" also followed the write again, the principle is similar
Link Stacks:
/*Link Stacks*/typedef statustypedefstructnode Stack;typedefstructNode *Ptrtonode;structnode{intdata; Ptrtonode next;}; Status Initstack (Stack&S) {S.next=NULL; returnOK;} Status GetTop (Stack&s,structNode &e) {if(!stackempty (S))returnERROR; E= *(S.next); returnOK;} Status Push (Stack&s,inte) {ptrtonode tmp= (Ptrtonode)malloc(sizeof(structnode)); TMP->data =e; TMP->next =S.next; S.next=tmp; returnOK;}intStackempty (Stack &S) {return(S.next = =NULL);} Status Pop (Stack&s,int&e) {ptrtonode tmp=S.next; E= tmp->data; S.next= tmp->Next; Free(TMP); returnOK;}
View Code
Sequential Stacks:
#defineStack_init_size 100;#defineINCREMENT 10;structnode{Selementtype*Next; };//how the struct is declaredtypedefstructnode Selementtype;typedefstruct{Selementtype*Base; Selementtype*top; intStackSize;} Sqstack; Status Initstack (Sqstack&s) {s.Base= S.top = (Selementtype *)malloc(sizeof(selementtype) *stack_init_size); if(! S.Base) exit (OVERFLOW);//failed to malloc CuncuS.stacksize =Stack_init_size;} Status GetTop (Sqstack&s,selementtype &e) {if(S.Base==s.top)returnERROR; E= * (s.top-1); returnOK;} Status Push (Sqstack&S,selementtype e) { if(S.top-s.Base==s.stacksize) {//Search the result of ReAllocS.Base= (Selementtype *)realloc(S.Base,sizeof(selementtype) * (Stack_init_size +INCREMENT)); if(! S.Base) exit (OVERFLOW); S.top= S.Base+s.stacksize; S.stacksize+=INCREMENT; } *s.top++ =e; returnOK;} Status Pop (Sqstack&s,selementtype &e) {if(S.Base==s.top)returnERROR; S.top--; E= *S.top; returnOK;}
View Code
Difference:
- such as Initstack (Stack &s) or Initstack (Sqstack &s), declares that the struct variable is no longer within the function. The return value is also not a pointer, which in turn becomes status.
- Again such as GetTop (Sqstack &s,selementtype &e) becomes the stack top node this structure is left intact to E, rather than as long as the structure of data.
- If the stack stack is full, the Min book uses realloc to expand the array, Weiss is not.
Same point:
- In the chain list do stack this place, both have no say Judge stack full situation. Because it is true that you can add any node, unless you set the maximum number of nodes manually
Sequential stack and chain stack implementations