#include <stdio.h>
#include <malloc.h>
#include<stdlib.h>
struct node
{
int data;
struct node *NEXT;
}*Head;
void display()
{
struct node *cur_ptr;
cur_ptr=Head;
if(Head==NULL)
printf("\n \nLinked list is Empty");
else
{
while(cur_ptr!=NULL)
{
printf("\n\n %d",cur_ptr->data);
cur_ptr=cur_ptr->NEXT;
}
}
}
void insert_begin(int value)
{
struct node *temp,*temp2;
temp2=Head;
temp=(struct node*)malloc(sizeof(struct node));
temp->data=value;
if(Head==NULL)
{
temp->NEXT=NULL;
Head=temp;
}
else
{
Head=temp;
temp->NEXT=temp2;
}
printf("\n%d is inserted at the begining of LL.",value);
}
void pop()
{
struct node *curptr;
curptr=Head;
if(Head==NULL)
{
printf("\n\nCannot be deleated UNDERFLOW!!!!\n");
}
else
{
printf("\n\n\n%d popped out\n\n",Head->data);
Head=curptr->NEXT;
//free(curptr);
}
}
void main()
{
int ch;
while(1)
{
printf("\nPlease enter your choice\n1. Insert\n2. Display\n3. Pop out\n\nChoice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
int temp;
printf("Enter the data to insert: ");
scanf("%d",&temp);
insert_begin(temp);
break;
}
case 2:
display();
break;
case 3:
{
pop();
break;
}
default:
exit(0);
}
}
}
0 Comments