This program is stack program implementation using linked list. In this this program you can see the first program segment is related to create a node, then in main function is related to create instruction that will show the instruction to entered in the program then the push pop and display function will be called in the program.
Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us
/* Stack using linked list*/ /* http://native-code.blogspot.com */ # include<stdio.h> # include<malloc.h> struct node { int info; struct node *link; } *top=NULL; main() { int choice; while(1) { printf("1.Push Operation: \n"); printf("2.Pop Operation: \n"); printf("3.Display\n"); printf("4.Quit\n"); printf("Enter of your choice : ") ; scanf("%d", &choice); switch(choice) { case 1: push(); break; case 2: pop(); break; case 3: display(); break; case 4: exit(1); default : printf("Wrong choice\n"); } } } push() { struct node *tmp; int pushed_item; tmp = (struct node *)malloc(sizeof(struct node)); printf("Enter new value to push in to stack : "); scanf("%d",&pushed_item); tmp->info=pushed_item; tmp->link=top; top=tmp; } pop() { struct node *tmp; if(top == NULL) printf("Stack is empty\n"); else { tmp=top; printf("Popped item is %d\n",tmp->info); top=top->link; free(tmp); } } display() { struct node *ptr; ptr=top; if(top==NULL) printf("Stack is empty: \n"); else { printf("Stack elements are :\n"); while(ptr!= NULL) { printf("%d\n",ptr->info); ptr = ptr->link; } } } /* http://native-code.blogspot.com*/
Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us
Post a Comment