Implementation and operation of the stack (C language description)

Source: Internet
Author: User


#include <stdio.h>#include<malloc.h>#include<stdlib.h>typedefintElementType;//Defining data Types//Defining NodestypedefstructNode {ElementType Element; structNode *Next;} NODE,*Pnode;//define the stack structure bodytypedefstructStack {pnode ptop; //defining the top node of the stackPnode Pbottom;//define the bottom node of the stack}stack,*Pstack;//function DeclarationvoidInitstack (Pstack Stack);//Initialize StackvoidPushstack (Pstack Stack,intVal);//into the Stack functionvoidPopstack (Pstack Stack,int*val);//Out Stack functionvoidTraversestack (Pstack Stack);//Traversing stack functionsBOOLIsEmpty (Pstack Stack);//determine if the stack is an empty functionvoidClearstack (Pstack Stack);//emptying the Stack function//Main functionintMain () {stack stack; //Create a stack variable intval =0;//Define a variableInitstack (&stack);//Call Initialize stack functionIsEmpty (&stack);//call to determine if the stack is an empty functionPushstack (&stack, -);//call into the stack function to push 100 into the stackPushstack (&stack, $); Pushstack (&stack, -); Pushstack (&stack, -); Pushstack (&stack, -); Pushstack (&stack, -); IsEmpty (&Stack); Traversestack (&stack);//Call stack traversal functionPopstack (&stack, &val);//call out the stack functionTraversestack (&Stack); Clearstack (&stack);//Call empty stack functionIsEmpty (&Stack); return 0;}//defining initialization Stack functionsvoidInitstack (Pstack Stack) {pnode pnew= (Pnode)malloc(sizeof(NODE));//Create a new node if(pnew = = NULL) {//determine if memory is allocated successfullyprintf"new node space allocation failed! \ n"); Exit (-1); } Stack->ptop = pnew;//stack top pointer to new nodeStack->pbottom = pnew;//The bottom pointer points to the new nodePnew->next = NULL;//The new node pointer is pointing to an emptyprintf"Stack created successfully! \ n");}//define into Stack function//Insert value Val from top of StackvoidPushstack (Pstack Stack,intval) {Pnode P= (Pnode)malloc(sizeof(NODE));//Creating a new node for storing variables if(P = =NULL) {printf ("allocating space memory failed! "); Exit (-1); } P->element = val;//variable assignment to the data field of the nodep->next=stack->ptop;//to point a new node to the previous nodeStack->ptop = P;//Update the top node so that it points to the newly created nodeprintf"%d into the stack successfully! \ n", Val);}//define the Stack function//eject from the top of the stack and assign the address to the variable ValvoidPopstack (Pstack Stack,int*val) { if(Stack->pbottom = = stack->ptop) {//determine if the stack is emptyprintf"out of Stack failed, stack is empty! \ n"); } pnode P= stack->ptop;//set up a temporary node to point to the top node of the stack*val = p->element;//pops the top node element and puts the address in the variable Val .Stack->ptop = p->next;//point the top pointer to the previous stack node Free(P);//frees node memory to prevent memory space leaksP = NULL;//prevents the creation of wild pointersprintf"popped%d from the stack! \ n", *val);}//defines whether the stack is an empty functionBOOLIsEmpty (Pstack Stack) {if(Stack->pbottom = = stack->ptop) {printf ("stack is empty! \ n"); return true; } Else { return false; }}//define the traversal function of the stackvoidTraversestack (Pstack Stack) {if(IsEmpty (Stack)) {//determine if the stack is emptyprintf"traversal stack failed, stack is empty! "); Exit (-1); } pnode P= stack->ptop;//creates a temporary node pointer that points to the top of the stack when initialized//causes the temporary node pointer to traverse the stack from the top of the stack until the bottomprintf"The result of the traversal stack is:"); while(P! = stack->Pbottom) {printf ("%d", p->element);//Print out DataP = p->Next; } printf ("\ n"); }//define the empty function of the stackvoidClearstack (Pstack Stack) {if(IsEmpty (Stack)) {//determine if the stack is emptyprintf"stack is empty, no need to empty! \ n"); Exit (-1); } //Create two node pointers to release the stackPnode P = stack->Ptop; Pnode Q=NULL; //Empty Stack while(P! = stack->Pbottom) {Q= p->Next; Free(P); P=Q; } Stack->ptop = stack->pbottom;//the top of the stack points to the initial empty node pointing at the bottomprintf"The stack has been emptied! \ n");}

Run results

Implementation and operation of the stack (C language description)

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.