EX.NO:7
LINKED LIST IMPLEMENTATION – STACK
PROGRAM: #include<stdio.h> #include #include<stdlib.h> struct stack { int data; struct stack *next; }; struct stack *head; void push() { struct stack *push; push=(struct stack*)malloc(sizeof(struct stack)); printf("Enter the item to be inserted\n"); scanf("%d",&push->data); if(head==NULL) { head=push; head->next=NULL; } else { push->next=head; head=push; } } void pop() { struct stack *pop; if(head==NULL) printf("The stack is empty\n"); else { printf("The poped item is %d",head->data); head=head->next; } } void display() { struct stack *p;
p=(struct stack*)malloc(sizeof(struct stack)); if(head==NULL) printf("The stack is empty\n"); else p=head; while(p!=NULL) { printf("%d\n",p->data); p=p->next; } } void modify() { struct stack *mod,*data; int x,num,no; printf("Enter the number to be modified\n"); scanf("%d",&no); printf("Enter the new number\n"); scanf("%d",&num); for(mod=head;mod!=NULL;mod=mod->next) { if(mod->data==no) { mod->data=num; x=1; } } if(x==1) printf("The number has been modified\n"); } void main() { int ch; char a='y'; clrscr(); printf("1.PUSH\t2.POP\t3.DISPLAY\t4.MODIFY\t5.EXIT\t"); do { printf("\nEnter the choice\n"); scanf("%d",&ch); switch(ch) { case 1: push(); break; case 2:
pop(); break; case 3: display(); break; case 4: modify(); break; default: printf("\nEnter correct choice\n"); } printf("\nDo you want to continue(y/n):\n"); a=getch(); }while(a=='y'); getch(); }