/*Singly Linked List with options:
insert,delete,display,count nodes*/
#include<stdio.h>
#include<conio.h>
//Declaration of structure
typedef struct node
{
int data;
struct node *link;
}node;
//main()
int main(void)
{
//declaration of variables
int choice=0;
node *head=NULL;
int count=0;
//Function prototypes
node* createlist(node *);
void display(node *);
node* insertatbeg(node *);
node* append(node *);
node* insertbypos(node *);
node* insertbyval(node *);
node* deletefirst(node *);
node* deletelast(node *);
node* deletebypos(node *);
node* deletebyval(node *);
int countnodes(node *);
clrscr();
do
{
printf("\n********MENU********");
printf("\nThe operations are:\n");
printf("\t1.Create\n\t2.Display\n");
printf("\t3.Insert At the beginning\n\t4.Append\n");
printf("\t5.Insert by position\n\t6.Insert by value\n");
printf("\t7.Delete first node\n\t8.Delete last node\n");
printf("\t9.Delete by position\n\t10.Delete by value\n");
printf("\t11.Count the nodes\n");
printf("Enter your choice:\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
//Create a singly linked list
head = createlist(head);
break;
case 2:
//display data in a singly linked list
display(head);
break;
case 3:
//insert nodes at the beginning
head=insertatbeg(head);
break;
case 4:
//insert node at the end
head=append(head);
break;
case 5:
//insert node by position
head=insertbypos(head);
break;
case 6:
//insert node by value
head=insertbyval(head);
break;
case 7:
//delete first node
head=deletefirst(head);
break;
case 8:
//delete last node
head=deletelast(head);
break;
case 9:
//delete node by position
head=deletebypos(head);
break;
case 10:
//delete node by value
head=deletebyval(head);
break;
case 11:
//count the nodes
count=countnodes(head);
printf("The present number of nodes are %d nodes in list",count);
break;
default:
printf("\nEntered a wrong choice\n");
}
}while(choice>0&&choice<=12);
getch();
return 0;
}
//Function for creating a list
node* createlist(node *head)
{
node *temp; char ch;
do
{
if(head==NULL)
{
head=temp=(node*)malloc(sizeof(node));
}
else
{
temp->link=(node *)malloc(sizeof(node));
temp=temp->link;
}
temp->link=NULL;
printf("Enter the data:");
scanf("%d",&temp->data);
printf("Do u want to enter more data");
flushall();
scanf("%c",&ch);
}while(ch=='y'||ch=='Y');
return(head);
}
//function for displaying data in the list
void display(node *head)
{
node *temp;
temp=head;
if(head==NULL)
{
printf("\nList is empty\n");
}
else
{
printf("\nThe data in the list is:\n");
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->link;
}
}
}
//Function to insert node at the beginning
node* insertatbeg(node *head)
{
node *temp;
if(head==NULL)
printf("List is empty");
else
{
temp=(node *)malloc(sizeof(node));
temp->link=head;
head=temp;
}
printf("Enter data to be inserted: ");
scanf("%d",&temp->data);
return(head);
}
//Function to insert node at the end
node* append(node *head)
{
node *temp,*p;
p=head;
if(head==NULL)
{
temp=(node *)malloc(sizeof(node));
temp->link=NULL;
head=temp;
}
else
{
while(p->link!=NULL)
{
p=p->link;
}
temp=(node *)malloc(sizeof(node));
temp->link=NULL;
p->link=temp;
p=p->link;
}
printf("Enter data to be inserted: ");
scanf("%d",&p->data);
return(head);
}
//Function to insert node in the middle
node* insertbypos(node *head)
{
int pos=1,i=1;
node *p,*temp;
p=head;
printf("Enter the insert position of the node: ");
scanf("%d",&pos);
while(i<pos)
{
p=p->link;
if(p==NULL)
printf("Very few nodes");
i++;
}
temp=(node *)malloc(sizeof(node));
temp->link=p->link;
p->link=temp;
printf("Enter data to be inserted: ");
scanf("%d",&temp->data);
return(head);
}
//Function to insert node by specific value
node* insertbyval(node *head)
{
int val=0;
node *p,*temp;
p=head;
printf("Enter the insert value after which to insert node: ");
scanf("%d",&val);
while(p->data!=val)
{
p=p->link;
if(p==NULL)
printf("Node not present");
}
temp=(node *)malloc(sizeof(node));
temp->link=p->link;
p->link=temp;
printf("Enter data to be inserted: ");
scanf("%d",&temp->data);
return(head);
}
//Function to delete 1st node
node* deletefirst(node *head)
{
node *p;
p=head;
head=head->link;
p->link=NULL;
free(p);
return(head);
}
//Function to delete last node
node* deletelast(node *head)
{
node *p,*temp;
p=head;
if(head==NULL)
{
printf("List is empty");
return(head);
}
else
{
if(head->link==NULL)
{
head=NULL;
free(p);
return(head);
}
while(p->link->link!=NULL)
p=p->link;
temp=p->link;
p->link=NULL;
free(temp);
}
return(head);
}
//Function to delete node i nthe middle
node* deletebypos(node *head)
{
node *p,*temp;
int pos=0,i=1;
p=head;
printf("Enter the delete position: ");
scanf("%d",&pos);
if(head==NULL)
{
printf("List is empty");
return(head);
}
if(pos==1)
{
deletefirst(head);
}
else
{
while(i<(pos-1))
{
p=p->link;
i++;
}
temp=p->link;
p->link=temp->link;
temp->link=NULL;
free(temp);
}
return(head);
}
//Function to delete node by value
node* deletebyval(node *head)
{
node *p,*temp;
int val=0,i=1;
p=head;
printf("Enter the delete value: ");
scanf("%d",&val);
if(head==NULL)
{
printf("List is empty");
return(head);
}
else
{
while(p->link->data!=val)
{
p=p->link;
i++;
}
temp=p->link;
p->link=temp->link;
temp->link=NULL;
free(temp);
}
return(head);
}
//To count the number of nodes
int countnodes(node *head)
{
int c=0;
node *p;
p=head;
while(p!=NULL)
{
p=p->link;
c++;
}
return(c);
}
No comments:
Post a Comment