Write a Program that uses functions to' perform insertion operation on a singly linked list. Write a Program that uses functions to perform deletion operation on a singly linked list
/* Write a Program that uses functions to' perform insertion operation on a singly linked list.
&
Write a Program that uses functions to perform deletion operation on a singly linked list*/
#include<stdlib.h>
#include <stdio.h>
void display();
void insert_begin();
void delete_begin();
struct node
{
int info;
struct node *next;
};
struct node *start=NULL;
int main()
{
int choice;
while(1){
printf("\n MENU \n");
printf("\n 1.Display \n");
printf("\n 2.Insert at the beginning \n");
printf("\n 3.Delete from beginning \n");
printf("\n 4.Exit \n");
printf("\n------------------------------------\n");
printf("\nEnter your choice: \n");
scanf("%d",&choice);
switch(choice)
{ case 1:
display();
break;
case 2:
insert_begin();
break;
case 3:
delete_begin();
break;
case 4:
exit(0);
break;
}
}
return 0;
}
void display()
{
struct node *ptr;
if(start==NULL)
{
printf(" \n List is empty: \n");
return;
}
else
{
ptr=start;
printf("\n The List elements are: \n");
while(ptr!=NULL)
{
printf("%d \n ",ptr->info );
ptr=ptr->next ;
}
}
}
void insert_begin()
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
printf(" \n Enter the data value for the node: \n " );
scanf("%d",&temp->info);
temp->next =NULL;
if(start==NULL)
{
start=temp;
}
else
{
temp->next=start;
start=temp;
}
}
void delete_begin()
{
struct node *ptr;
if(ptr==NULL)
{
printf("\n List is Empty: \n ");
return;
}
else
{
ptr=start;
start=start->next ;
printf("\n The deleted element is :%d \n ",ptr->info);
free(ptr);
}
}
Comments
Post a Comment