// Online C compiler to run C program online
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
}*new,*tos = NULL,*temp;
void push();
void pop();
void display();
int main()
{
int choice;
printf("\n:: Stack using Linked List ::\n");
while(1){
printf("\n****** MENU ******\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: push();
break;
case 2: pop(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("\nWrong selection!!! Please try again!!!\n");
}
}
}
void push()
{
int value;
new = (struct Node*)malloc(sizeof(struct Node));
printf("enter value");
scanf("%d",&value);
new->data = value;
if(tos == NULL)
new->next = NULL;
else
new->next = tos;
tos = new;
printf("\nInsertion is Success!!!\n");
}
void pop()
{
temp=tos;
if(tos == NULL)
printf("\nStack is Empty!!!\n");
else{
printf("\nDeleted element: %d", temp->data);
tos = temp->next;
free(temp);
}
}
void display()
{
if(tos == NULL)
printf("\nStack is Empty!!!\n");
else{
temp = tos;
while(temp != NULL){
printf("%d--->", temp->data);
temp = temp->next;
}
printf("NULL\n"); // just print NULL at the end, don't access temp->data
}
}