C implementing all the stack operations using linked list
#include <stdio.h>
#include <stdlib.h>
void push();
void pop();
void display();
struct node
{
int val;
struct node *next;
};
struct node *head;
void main()
{
int choice=0;
printf("\n\t\t -----------STACK OPERATIONS USING LINKED LIST -----------");
printf("\n\t\t ------- MENU -------- ");
printf("\n\t\t [Link] \n\t\t [Link] ");
printf("\n\t\t [Link] \n\t\t [Link]");
while(choice!=4)
{
printf("\n\t\t ENTER YOUR CHOICE : ");
scanf("%d",&choice);
switch(choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: printf("Exiting....");
break;
default:printf("Please Enter valid choice ");
}
}
}
void push ()
{
int val;
struct node *ptr=(struct node*)malloc(sizeof(struct node));
if(ptr==NULL)
{
printf("\n\t\t MAIN MEMORY OVERFLOW. . . CAN'T CREATE NODE");
}
else
{
printf("\n\t\t ENTER DATA : ");
scanf("%d",&val);
if(head==NULL)
{
ptr->val=val;
ptr->next=NULL;
head=ptr;
}
else
{
ptr->val=val;
ptr->next=head;
head=ptr;
}
printf("\n\t\t %d IS INSERTED IN LIST",val);
}
}
void pop()
{
int item;
struct node *ptr;
if (head==NULL)
{
printf("\n\t\t EMPTY LIST ");
}
else
{
item=head->val;
ptr=head;
head=head->next;
free(ptr);
printf("\n\t\t %d IS POPED FROM LIST",item);
}
}
void display()
{
int i;
struct node *ptr;
ptr=head;
if(ptr==NULL)
{
printf("\n\t\t EMPTY LIST");
}
else
{
printf("\n\t\t PRINTING STACK DATA : ");
while(ptr!=NULL)
{
printf(" %d ",ptr->val);
ptr=ptr->next;
}
}
}