Data structure code and parsing-----stack

Source: Internet
Author: User

1#include <stdio.h>2#include <stdlib.h>3#include <malloc.h>4 5typedefstructNode//Defining Nodes6 {7     intdata;8     structNode *Pnext;9}node, *Pnode;Ten  OnetypedefstructStack//defines the stack, which is equivalent to a box; a total of 2 nodes, one representing the top of the box, indicating the bottom. A { -Pnode ptop;//Top of Box -Pnode Pbottom;//Bottom the}stack, *Pstack; -  - voidInit (pstack PS) - { +Ps->ptop = (pnode) malloc (sizeof(NODE));//Create an empty stack first -     if(NULL = = ps->ptop) +     { Aprintf"dynamic memory allocation failure!\n"); atExit (-1); -     } -     Else -     { -Ps->pbottom = ps->ptop; -Ps->pbottom->pnext = NULL;//or use Ps->ptop->pnext = NULL in         //because they're just starting to point to the same place---bottom, as long as the bottom of the pointer field is empty. -     } to } +  - voidPush (Pstack PS,intval) the { *Pnode pnew = (pnode) malloc (sizeof(NODE));//Create a new node and put it in the stack . $     if(NULL = =pnew)Panax Notoginseng     { -printf"dynamic memory allocation failure!\n"); theExit (-1); +     } A  thePnew->data =Val; +Pnew->pnext = ps->ptop; -Ps->ptop =pnew; $  $     return; - } -  the voidTraverse (Pstack PS)//Traverse Output - {WuyiPnode p = ps->ptop;//Note that you must use P to temporarily store the node pointer on the top of the box, or the top of the box at the end of the node pointer changes---will affect other function execution the      -      while(P! = ps->pbottom)//when p = = Ps->pbottom is finished, the P has become the Ps->pbottom bottom Wu     { -printf"%d", p->data); Aboutp = p->Pnext; $     } -Putchar (Ten); -  -     return; A } +  the BOOLempty (pstack PS) - { $     if(Ps->ptop = = ps->pbottom) the     { the         return true; the     } the     Else -         return false; in } the //Stack the stack that the PS points to, and put the elements of the stack into the variable that pval points to. the BOOLPop (Pstack PS,int*pVal) About { the     if(Empty (PS)) the     { the         return false; +     } -     Else the     {BayiPnode r = ps->ptop; the*pval = r->data; thePs->ptop = r->Pnext; -          - Free (r); theR =NULL; the     } the  the     return true; - } the  the  the voidClear (Pstack PS)//empty--Frees up memory94 { the     if(Empty (PS)) the         return; the     Else98     { AboutPnode p = ps->ptop; -Pnode q =NULL;101 102          while(P! = ps->pbottom)103         {104Q = p->Pnext; the Free (p);106p = q;//at the end, p (ps->ptop) has been pointed at the bottom of the ps->pbottom, but the contents of ps->ptop are not empty nodes.107         }108 109Ps->ptop = ps->pbottom;//So when emptying, the data field to be Ps->ptop is also empty . the     }111 } the 113 intMainvoid) the { the     intVal; the STACK S;117Init (&S);118     119Push (&s,1); -Push (&s,2);121Push (&s,3);122Push (&s,4);123Push (&s,5);124Traverse (&S); the 126Pop (&s, &val);//out of the stack only once127printf"The number of data fields stored for the node you are out of the stack is:%d\n", Val); -Traverse (&S);129  theClear (&S);131Traverse (&s);//No, there'll be a blank line. the 133     return 0;134}

Data structure code and parsing-----stack

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.