#include<stdio.h> #include<stdlib.h> #define MAX 5 void push(); void pop(); void display(); void peek(); struct node { int data; struct node * down; }; int count=0; struct node *top=NULL; int main() { int choice; do { printf("\n---- MENU ----\n"); printf("\n1.PUSH\n2.POP\n3.display\n4.PEEK\n5.EXIT\n"); printf("Enter your choice\n"); scanf("%d",&choice); switch(choice) { case 1: push();break; case 2: pop();break; case 3: display();break; case 4: peek();break; case 5: printf("~~~~~ THANK YOU ~~~~~\n");break; default: printf("\nINVALID INPUT !!!\n"); } }while(choice!=5); } void push() { int value; struct node *newnode; if(count>=5) printf("\nStack overflow....\n"); else { newnode=(struct node* )malloc(sizeof(struct node*)); printf("\nEnter element to be inserted\n"); scanf("%d",&value); newnode->data=value; newnode->down=top; top=newnode; count++; } } void pop() { struct node *temp; if(count==0) printf("\nStack underflow....\n"); else { temp=top; top=top->down; free(temp); count--; } } void peek() { if(count==0) printf("\nStack is empty....\n"); else printf("Element at the top = %d\n",top->data); } void display() { struct node *temp=top; int printer; printer=count; if(count==0) printf("\nStack is empty....\n"); else { printf("Elements are :\t"); while(printer>0) { printf("%d\t",temp->data); temp=temp->down; printer--; } } }
Labels: Data Structure