Data structure Learning--The chain list implementation of stacks (programmatic)

Source: Internet
Author: User

For the basic concept of stacks and the relationship to Catalan numbers, see my other articles

Reference data structure and algorithm analysis--c language description


#include <stdio.h> #include <stdlib.h>/* Stack's linked list realizes */typedef struct stacknode{struct stacknode *next; int data;} Stacknode,*stack; Stack createstack (void);//Create an empty stack void Makeempty (stack s);//empty stack int IsEmpty (stack s);//test is empty stack void Push (int data,stack S)    ;//Into Stack void POPs (stack s);//stack int top (stack s);//return stack top element int main () {stack sp=null;    Sp=createstack ();    Push (5,SP);    Push (4,SP);    Push (8,SP);    printf ("%d\n", Top (SP));    Pop (SP);    printf ("%d\n", Top (SP)); return 0;}    Stack createstack (void) {stack S;    S=malloc (sizeof (Stacknode));    if (s==null) printf ("Out of space!\n");    s->next=null;    Makeempty (S); return S;}    void Makeempty (Stack S) {if (s==null) printf ("Please Create Stack first!\n"); else while (! IsEmpty (s)) Pop (s);} int IsEmpty (Stack S) {return s->next==null;}    void Push (int data,stack S) {Stack tmp;    Tmp=malloc (sizeof (Stacknode));    if (tmp==null) printf ("Out of space!\n"); else {tmp->dAta=data;        tmp->next=s->next;    s->next=tmp;    }}void Pop (Stack S) {stack tmp;    if (IsEmpty (S)) printf ("Empty stack!\n");        else {tmp=s->next;        s->next=s->next->next;    Free (TMP); }}int Top (Stack S) {if (!    IsEmpty (S)) return s->next->data;    else printf ("Empty stack!\n"); return 0;}


Data structure Learning--The chain list implementation of stacks (programmatic)

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.