The stack definition and basic operation realization of data structure

Source: Internet
Author: User

Finally there can be time to write some data structure of the study summary, the previous period of time has been busy some projects, there is no time to learn the data structure, now finally can be a little breath, or the data structure is interesting, this two days to see the point stack of things, write down a summary, there is the wrong place to see the friend pointed out Appreciate.

According to learning, the stack is a linear data structure, the operation of the stack can only be done in a section of the table, so this data structure has a "LIFO" feature.

Next is the C language implementation of the stack. Where the stack consists of a top node and a bottom node, these two nodes point to the top and bottom of the stack, respectively. The constituent nodes of the stack are implemented by the structure, and the structure is composed of a database and a pointer field pointing to the next node, and the following is the C language implementation of the node:

1 /* 2  3*/4struct  node{5     int  data; 6     struct Node * Pnext; 7 }node,* Pnode;

With the implementation of the stack node, then the next step is how to implement the stack (the specific explanation to see the code):

1 /* 2  3*/4struct  strack{5    pnode ptop; 6     pnode pbottom; 7 }strack,* pstrack;

The basic operation of stack: stack initialization, stacking, stack traversal, stack operation, etc.

1 /*2 function to initialize the stack3 the stack initialization is simply to point the top and bottom pointers in the stack to a definite address, and this address points to the head node of the stack (the introduction of the head node can be4 greatly facilitates the operation of the stack). 5 */6 voidInit_strack (Pstrack pS) {7ps->ptop= (Pnode)malloc(sizeof(NODE));//define a new node, this node is the head node of the stack, and let ptop point to the node8     if(ps->ptop==NULL) {9printf"Memory allocation Failure");TenExit (-1); One     }  A     Else{ -ps->pbottom=ps->ptop;//The bottom and top points point to the head node, the initialization stack is complete, and there are no valid nodes in the stack . -ps->ptop->pnext=NULL; the     } -       - } -  + /* - Pressure stack function + because the stack has an "advanced post-out, LIFO" feature, the stack can only be pushed onto the top of the stack when the element is pressed into the stack. A Thus, the stack function is the top pointer of the stack pointing to the new node, and the pointer field of the new node points to the topmost element when it is not pressed into the stack.  at */ - intPush (Pstrack PS,intval) { -Pnode pnew= (Pnode)malloc(sizeof(NODE));//define a new node. -     if(pnew->pnext==NULL) { -printf"Memory allocation Failure"); -Exit (-1); in     } -Pnew->data=Val; topnew->pnext=ps->ptop;//The order of the pointer assignment can not be chaotic, in order to ensure that the address pointed to the top is not lost, so first let the new node's pointer field point to the node Ps->ptop Point +ps->ptop=pnew;//make the top pointer point to the new node -     return 0;//0 indicates that the current push succeeded the } *  $ /*Panax Notoginseng the traversal function of the stack - because it just traverses the output, it can't break the stack's original structure, so we only have to define a new node p to point to the top of the stack, the then traverse down one output to the value of the element it points to, the loop condition is P=ps->pbottom + */ A voidTraverse (pstrack PS) { thePnode p=ps->ptop;//let the newly defined struct pointer point to the top element of the stack +      while(p!=ps->Pbottom) { -printf"%d", p->data); $p=p->pnext;//to point the current pointer to the next node of the current point $     }  -printf"\ n"); -     return; the } - Wuyi  the /* - function to determine if the current stack is empty Wu if NULL, returns True - Otherwise, returns false About */ $ BOOLIsEmpty (pstrack PS) { -     if(ps->ptop==ps->Pbottom) { -         return true; -     } A     Else{ +         return false; the     } - } $  the /* the stack function: The stack is simply to pop the first value and then point the ps->ptop to the next node in the original first node (the first node after the stack) . the then release the element that will be ejected in memory.  the */ - BOOLPop (Pstrack PS,int*pVal) { in     if(IsEmpty (PS)) {//determines whether the current stack is empty and returns False if it is empty the         return false; the     } About     Else{    thePnode p=ps->ptop;//defines a node that points to the top of the stack each time the stack is stacked, which ensures that no missing elements are lost theps->ptop=p->pnext;//Point top node to top of stack element the*pval=p->data; +          Free(p);//will be released by the popup element -p=NULL; the         return true;Bayi     }      the}

The above implementation of the basic operation of the stack, of course, the specific application also depends on the specific algorithm requirements.

The stack definition and basic operation realization of data structure

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.