// Stack Using Link List
#include<stdio.h> #include<process.h> #include<malloc.h> struct link{ int data; struct link *next; }; typedef struct link node; node* top=NULL; main() { void push(int);int pop();void display(); int data,ch,x; printf("Menu\n1.Push\n2.POP\n3.Display\n4.Exit\n"); do{ printf("\nEnter your Choice\n"); scanf("%d",&ch); switch(ch) { case 1: printf("Enter data\n"); scanf("%d",&data); push(data); break; case 2: x=pop(); printf("Deleted data is = %d\n",x); break; case 3: display(); break; case 4: exit(1); default: printf("Wrong choice\n"); } }while(ch!=4); } void push(int x) { node* nnode; nnode=(node*)malloc(sizeof(node)); nnode->data=x; nnode->next=top; top=nnode; } int pop() { int k; node* t;//we took t node to make memory free of node t t=top; if(top==NULL) printf("\nThere is no data\n"); else{ k=top->data; top=top->next; return(k); free(t); } } void display() { node* i; if(top==NULL) printf("Nothing to Display\n"); else{ i=top; for(i=top;i!=NULL;i=i->next) { printf("%d ",i->data); } } }
No comments:
Post a Comment