You are on page 1of 4

#include<stdio.

h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
typedef struct node *NODE;
NODE create(int);
NODE insert_front(NODE , int);
NODE insert_end(NODE , int);
NODE delete_front(NODE );
NODE delete_end(NODE );
void display(NODE );
int main()
{
int choice,data;
NODE head=NULL;
while(1)
{
printf("\n-----SINGLY LINKED LIST MENU-----\n");
printf("1:Insert Front 2:Delete Front 3:Insert End 4:Delete End
5:DISPLAY 6:EXIT 7:Insert Specific 8:Delete Specific\n");
printf("Enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the number: \n");
scanf("%d",&data);
head=insert_front(head,data);
break;
case 2: head=delete_front(head);
break;
case 3: printf("Enter the number: \n");
scanf("%d",&data);
head=insert_end(head,data);
break;
case 4: head=delete_end(head);
break;
case 5: display(head);
break;
case 6: exit(0);
case 7: printf("Enter the number: \n");
scanf("%d",&data);
head=insert_specific(head,data);
break;
case 8: printf("Enter the number to delete: \n");
scanf("%d",&data);
head=delete_specific(head,data);
break;
default: printf("Invalid Choice\n");
}
}
return 0;
}
NODE create(int data)
{
NODE temp;
temp=(NODE)malloc(sizeof(NODE));
temp->data=data;
temp->link=NULL;
return(temp);
}
NODE insert_front(NODE head, int data)
{
NODE temp;
temp=create(data);
if(head==NULL)
head=temp;
else
{
temp->link=head;
head=temp;
}
return head;
}
NODE insert_end(NODE head, int data)
{
NODE p,temp;
temp=create(data);
if(head==NULL)
head=temp;
else
{
p=head;
while(p->link!=NULL)
p=p->link;
p->link=temp;
}
return head;
}
NODE insert_specific(NODE head, int data)
{
int nd = 0;
NODE p, ahead, temp;
temp = create(data);
if(head==NULL)
{
head=temp;
return head;
}
else
{
printf("\nEnter after which node you want to insert in the list?:
");
scanf("%d",&nd);
p=head;
while(p->data != nd)
{
p=p->link;
if(p==NULL)
break;
}
if(p==NULL)
printf("Number not in the list\n\n");
else
{
ahead=p->link;
p->link=temp;
temp->link=ahead;
}
}
return head;
}
NODE delete_front(NODE head)
{
NODE p;
if(head==NULL)
printf("The list is empty. \n");
else
{
p=head;
head=head->link;
printf("Deleted data is %d \n\n",p->data);
free(p);
}
return head;
}
NODE delete_end(NODE head)
{
NODE p,temp,prev;
if(head==NULL)
printf("The list is empty. \n");
else if(head->link==NULL)
{
printf("Deleted number is %d \n\n",head->data);
head=NULL;
free(head);
}
else
{
p=head;
while(p->link!=NULL)
{
prev=p;
p=p->link;
}
prev->link=NULL;
printf("Deleted number is %d \n\n",p->data);
free(p);
}
return head;
}
NODE delete_specific(NODE head, int data)
{
NODE p,prev,ahead;
p=head;
if(head==NULL)
printf("List is empty. \n");
else if(head->link==NULL)
{
printf("Deleted number is %d \n\n",head->data);
head=NULL;
free(head);
}
else
{
while(p->data!=data)
{
prev=p;
p=p->link;
if(p==NULL)
break;
}
if(p==NULL)
printf("Number not in the list \n\n");
else
{
ahead=p->link;
prev->link=ahead;
p->link=NULL;
printf("Deleted number is %d \n\n",p->data);
free(p);
}
}
return head;
}
void display(NODE head)
{
NODE p;
if(head==NULL)
printf("List is empty. \n");
else
{
p=head;
printf("Numbers in list are:\n");
while(p!=NULL)
{
printf("%d ->",p->data);
p=p->link;
}
}
}

You might also like