//stack using linkedlist
#include <stdio.h>
#include <stdlib.h>
void push();
void pop();
void display();
struct node
{
int data;
struct node *next;
};
struct node *head;
void main()
{
int choice=0;
printf("\n*********Stack operations using linked list*********");
printf("\n----------------------------------------------");
while(choice!=4)
{
printf("\n\nChose one from the below options...\n");
printf("\[Link]\[Link]\[Link]\[Link]");
printf("\n Enter your choice \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
exit(0);
break;
}
default:
{
printf("Please Enter valid choice ");
}
}
}
}
void push ()
{
int value;
struct node *ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\noverflow");
}
else
{
printf("Enter the value");
scanf("%d",&value);
if(head==NULL)
{
ptr->data = value;
ptr -> next = NULL;
head=ptr;
}
else
{
ptr->data = value;
ptr->next = head;
head=ptr;
}
printf("Item pushed");
}
}
void pop()
{
int item;
struct node *ptr;
if (head == NULL)
{
printf("\nUnderflow");
}
else
{
item = head->data;
ptr = head;
head = head->next;
free(ptr);
printf("Item popped");
}
}
void display()
{
struct node *ptr;
ptr=head;
if(ptr == NULL)
{
printf("\nStack is empty");
}
else
{
printf("Stack elements are\n");
while(ptr!=NULL)
{
printf("%d\n",ptr->data);
ptr = ptr->next;
}
}
}
/*output:
*********Stack operations using linked list*********
----------------------------------------------
Chose one from the below options...
[Link]
[Link]
[Link]
[Link]
Enter your choice
1
Enter the value10
Item pushed
Chose one from the below options...
[Link]
[Link]
[Link]
[Link]
Enter your choice
1
Enter the value20
Item pushed
Chose one from the below options...
[Link]
[Link]
[Link]
[Link]
Enter your choice
1
Enter the value30
Item pushed
Chose one from the below options...
[Link]
[Link]
[Link]
[Link]
Enter your choice
3
Stack elements are
30
20
10
Chose one from the below options...
[Link]
[Link]
[Link]
[Link]
Enter your choice
2
Item popped
Chose one from the below options...
[Link]
[Link]
[Link]
[Link]
Enter your choice
3
Stack elements are
20
10
Chose one from the below options...
[Link]
[Link]
[Link]
[Link]
Enter your choice
4
Process returned 0 execution time : 85.717 s
Press any key to continue.*/