Chained stacks-C language implementation

Source: Internet
Author: User

The chain stack is more flexible than the limited space of the sequential stack

#include <stdio.h>#include<malloc.h>typedefintSelemtype;//element TypetypedefintStatus;//return value type#defineOK 1//Operation succeeded#defineERROR 0//operation failedtypedefstructStacknode//Link Stack node structure{Selemtype date; //node Data    structStacknode *next;//Node Pointers}*linkstackptr;//Knot nametypedefstructLinkstack//Link Stack structure{linkstackptr top; //stack Top pointer    intCount//number of stack nodes}linkstack;//Stack name/*the stack operation of the link stack*/Status Push (linkstack*S, Selemtype e) {linkstackptr s= (linkstackptr)malloc(sizeof(Stacknode));//apply for a new node.S->date = e;//the data for the new node equals E.S->next = s->top;//The next node of the new node equals the current stack top node .S->top = s;//The top node of the stack equals the new node.s->count++;//data in the stack plus 1    returnOK;}/*Stack-out operation of the chain stack*/Status Pop (linkstack*s, Selemtype *e) {    if(S->count = =0)//determine if the stack is empty        returnERROR; Linkstackptr s= (linkstackptr)malloc(sizeof(Stacknode));//apply for a new node.s = s->top;//The new node equals the top node of the current stack .*e = s->date;//e equals the data of the top node of the stack.S->top = s->next;//The top of the stack equals the next node of the new node     Free(s);//releasing new nodes.s->count--;//data in stack minus 1    returnOK;}voidMain () {Linkstack S; //Create Stack LS.count =0;//stack Top pointer is-1, stack is empty    intE//elements into stacks and stacks     while(true) {printf ("Please select the operation for the link stack: \ n"); printf ("1. into the stack \ n"); printf ("2. Out of stack \ n"); printf ("3. Exit \ n"); intA; scanf ("%d", &a); Switch(a) { Case 1: printf ("Please enter the elements into the stack:"); scanf ("%d", &e); if(Push (&S, E)) printf ("into the stack successfully \ n"); Elseprintf ("failed to enter the Stack \ n");  Break;  Case 2:                if(Pop (&s, &e)) printf ("The elements of the stack are:%d\n", E); Elseprintf ("stack empty \ n");  Break;  Case 3:                return; default: printf ("Select Error \ n");  Break; }    }}

Chained stacks-C language implementation

Related Article

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.