Stack Using LinkedList
Stack Using LinkedList
To implement stack using a single linked list that supports the basic operation of stack data
structure such as PUSH, POP and Traverse and it follows LIFO mechanism
Inserted of using arrays we can also used linked list to implement stack
To overcome the drawbacks of arrays that mean limited size and garbage memory allocation
Example:
TOP
4700
4700
Struct node
{
Int data;
Struct node *next;
};
Struct node * top = NULL;
Step -2: PUSH() - insert an element into the linked list
Step -4: traverse() – it shows output from the stack using linked list
program:
// implementing stack using linked list
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
while(1)
{
printf("Stack menu\n");
printf("1. push\n 2. pop\n 3. traverse\n 4.exit\n");
printf("Choose one:");
scanf("%d",&choose);
switch(choose)
{
case 1: printf("Enter an element:");
scanf("%d",&ele);
push(ele);
break;
case 2:pop();
break;
case 3:traverse();
break;
case 4: exit(0);
break;
default: printf("Invalid choose...\n");
}
}
getch();
}