Sequential stack and chain stack implementations

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.