Saturday, August 9, 2014

Insertion, Deletion, Peep, Display, Isempty of a Stack

This is a program for the operations with a stack. If you have any doubts, please let me know.


#include<stdio.h> #include<stdlib.h> struct node { int data; struct node *next; }*top=NULL,*newn,*temp; int num; void ins(); void del(); void peep(); void display(); void empty(); main() { int ch,c; do { printf("operations with a stack! "); printf("\nTHE MAIN MENU\n1.ins\n2.Del\n3.Peep\n4.Dip\n5.Empty"); printf("\n choice?:"); scanf("%d",&ch); switch(ch) { case 1:ins(); break; case 2:del(); break; case 3:peep(); break; case 4:display(); break; case 5:empty(); break; default:printf("\nWrong Choice"); break; } printf("\ncontinue?(1.Yes;2.No):"); scanf("%d",&c); }while(c!=2); } void ins() { printf("\nspecify the element you wish to enter:"); scanf("%d",&num); newn=(struct node *)malloc(sizeof(struct node)); newn->data=num; if(top==NULL) newn->next=NULL; else newn->next=top; top=newn; } void del() { if(top==NULL) printf("\nUnderflow"); else { temp=top; top=top->next; free(temp); } } void peep() { if(top==NULL) printf("\nUnderflow"); else printf("\n %d",top->data); } void display() { if(top==NULL) printf("\nUnderflow"); else { temp=top; while(temp!=NULL) { printf(" %d",temp->data); temp=temp->next; } } } void empty() { if(top==NULL) printf("\nStack is empty"); else printf("\nStack is not empty"); }

No comments:

Post a Comment