//implementation of stack operations using linked list int num;
#include <stdio.h> temp =(NODE*)malloc(1*sizeof(NODE));
#include <stdlib.h> printf("\n enter an element");
struct node scanf("%d",&num);
{ temp->data = num;
int data; if (top == NULL)
struct node *link; {
}; top = temp;
typedef struct node NODE; top->link = NULL;
NODE *top=NULL; }
/* Checks if Stack is empty or not */ else
int isEmpty() {
{ temp->link = top;
if (top == NULL) top = temp;
return 1; }
else }
return 0; /*Pop Operation: Removes Top Element of the Stack */
} void pop()
/* Returns the top element of Stack */ {
int peek() NODE *temp;
{ if (isEmpty(top))
printf("\n the top of the stack is %d",top->data); {
return; printf("\nStack is Empty\n");
} return;
/* Push an Element in Stack */ }
void push() else
{ {
NODE *temp; temp = top;
top = top->link; printf("\n 1. push");
printf("Removed Element : %d\n", temp->data); printf("\n 2. pop");
free(temp); printf("\n 3. peek");
} printf("\n 4. Display");
} printf("\n 5. Exit");
/* Prints the linked list representation of a stack */ while(choice!=5)
void display() {
{ printf("\nEnter Your choice : ");
NODE *temp; scanf("%d",&choice);
if(top==NULL) switch(choice)
printf("\n stack is empty"); {
else case 1: push();break;
{ case 2: pop();break;
temp=top; case 3: peek();break;
while (temp != NULL) case 4: display();break;
{ case 5: exit(0);
printf("%d ", temp->data); default: printf("\n wrong choice");
temp = temp->link; }
if(temp != NULL) }
printf("->"); return 0;
} }
printf("\n");
void main()
int choice;
printf("\n stack using linked list operations");