Lab 5
Lab 5
/*
* C Program to Implement a Stack using Linked List
*/
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *ptr;
}*top,*temp,*top1;
int main()
{
int no, ch, e;
printf("\n 1 - Push");
printf("\n 2 - Pop");
printf("\n 3 - Display");
printf("\n 4 - Exit");
create();
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
push(no);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default :
printf(" Wrong choice, Please enter correct choice ");
break;
}
}
getch();
return 0;
}
/* Create empty stack */
void create()
{
top = NULL;
}
if (top1 == NULL)
{
printf("Stack is empty");
return;
}
if (top1 == NULL)
{
printf("\n Error : Trying to pop from empty stack");
return;
}
else
top1 = top1->ptr;
printf("\n Popped value : %d", top->info);
free(top);
top = top1;
}
RESULT
-------
OUTPUT
1 - Push
2 - Pop
3 - Display
4 - Exit
Enter choice : 1
Enter data : 5
Enter choice : 1
Enter data : 2
Enter choice : 1
Enter data : 3
Enter choice : 1
Enter data : 4
Enter choice : 3
4 3 2 5
Enter choice : 2
Popped value : 4
Enter choice : 2
Popped value : 3
Enter choice : 2
Popped value : 2
Enter choice : 2
Popped value : 5
Enter choice : 2