Successfully reported this slideshow.

Linked List Implementation of Stack in C

965 views

Published on

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

Linked List Implementation of Stack in C

  1. 1. /*Linked List Implementation of the Stack in CAuthor: Kasun Ranga WijeweeraEmail: krw19870829@gmail.comDate: 20130501*/#include<stdio.h>#include<conio.h>#include<alloc.h>typedef struct stackNode *link;struct stackNode{int data;link next;};typedef struct{link top;}stack;void initStack(stack *s)
  2. 2. {s->top=NULL;}int isEmpty(stack *s){if((s->top)==NULL){return 0;}else{return 1;}}void pushStack(stack *s,int x){link t=(link)malloc(sizeof(struct stackNode));t->data=x;t->next=s->top;s->top=t;}int popStack(stack *s)
  3. 3. {link t;int x;t=s->top;x=t->data;s->top=t->next;free(t);return x;}void printStack(stack *s){link t=s->top;while(t!=NULL){printf("%d ",t->data);t=t->next;}}void main(){int x;stack *s;
  4. 4. clrscr();s=(stack*)malloc(sizeof(stack));initStack(s);pushStack(s,10);pushStack(s,20);if(isEmpty(s))x=popStack(s);pushStack(s,30);pushStack(s,40);pushStack(s,50);if(isEmpty(s))x=popStack(s);printStack(s);free(s);getch();}

×