C Program for Singly Linked List [Basic Functions]
#include<stdio.h>
#include<stdlib.h>
typedef struct singlelinklist
{
int data;
struct singlelinklist *link;
}slnode;
void create();
void insert_at_begin();
void insert_at_position();
void insert_at_end();
void delete_at_begin();
void delete_at_position();
void delete_at_end();
void display();
int count();
slnode *head=NULL, *temp=NULL, *next=NULL;
int value;
void main()
{
int option=0,a;
while(1)
{
printf("\n Enter your choice : \n");
printf("1: Create\n");
printf("2: Insert at beginning\n");
printf("3: Insert at position\n");
printf("4: Insert at end\n");
printf("5: Delete at beginning\n");
printf("6: Delete at position\n");
printf("7: Delete at end\n");
printf("8: Display\n");
printf("9: Count\n");
printf("Other: EXIT\n");
scanf("%d",&option);
switch(option)
{
case 1: create();
break;
case 2: insert_at_begin();
break;
case 3: insert_at_position();
break;
case 4: insert_at_end();
break;
case 5: delete_at_begin();
break;
case 6: delete_at_position();
break;
case 7: delete_at_end();
break;
case 8: display();
break;
case 9: a=count();
break;
default: exit(0);
}
}
}
void create(){
int n,i;
printf("How many nodes you want to create? \n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter the data for node %d: \n",i);
scanf("%d",&value);
temp=(slnode*)malloc(sizeof(slnode));
temp->data=value;
temp->link=NULL;
if(i==1)
{
head=temp;
}
else
{
next=head;
while(next->link!=NULL)
{
next=next->link;
}
next->link=temp;
}
}
}
void insert_at_begin()
{
printf("Enter the data for new first node : \n");
scanf("%d",&value);
temp=(slnode*)malloc(sizeof(slnode));
temp->data=value;
temp->link=head;
head=temp;
}
void insert_at_position()
{
int value,pos,i;
value=count();
next=head;
printf("Enter the position to insert node : \n");
scanf("%d",&pos);
if(pos>0&&pos<=value+1)
{
if(pos==1)
{
insert_at_begin();
}
else if(pos==value+1)
{
insert_at_end();
}
else
{
temp=(slnode*)malloc(sizeof(slnode));
printf("Enter the data for new node : \n");
scanf("%d",&temp->data);
for(i=1;i<pos-1;i++)
{
next=next->link;
}
temp->link=next->link;
next->link=temp;
printf("Node inserted sucessfully \n ");
}
}
else
{
printf("Invalid position");
}
}
void insert_at_end()
{
printf("Enter the data for new last node : \n");
scanf("%d",&value);
temp=(slnode*)malloc(sizeof(slnode));
temp->data=value;
temp->link=NULL;
next=head;
while(next->link!=NULL)
{
next=next->link;
}
next->link=temp;
}
void delete_at_begin()
{
if(head==NULL)
printf("List empty, no node to delete! \n");
else
{
next=head;
head=NULL;
printf("First node deleted! \n");
head=next->link;
}
}
void delete_at_end()
{
if(head==NULL)
printf("List empty, no node to delete! \n");
else
{
next=head;
while(next->link!=NULL)
{
temp=next;
next=next->link;
}
temp->link=next->link;
next=NULL;
printf("Last node deleted! \n");
}
}
void delete_at_position()
{
int value,pos,i;
value=count();
next=head;
printf("Enter the position to delete : \n");
scanf("%d",&pos);
if(pos>0&&pos<=value)
{
if(pos==1)
{
head=NULL;
head=next->link;
}
else
{
for(i=1;i<pos;i++)
{
temp=next;
next=next->link;
}
temp->link=next->link;
next=NULL;
}
printf("Node deleted Sucessfully \n ");
}
else
{
printf("Invalid position");
}
}
void display()
{
temp=head;
printf("Display the linked list \n ");
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->link;
}
}
int count()
{
int count=0;
temp=head;
while(temp!=NULL)
{
count++;
temp=temp->link;
}
printf("No of item in linked list : %d \n",count);
return count;
}
Output:-
Enter your choice :
1: Create
2: Insert at beginning
3: Insert at position
4: Insert at end
5: Delete at beginning
6: Delete at position
7: Delete at end
8: Display
9: Count
Other: EXIT
1
How many nodes you want to create?
4
Enter the data for node 1:
4
Enter the data for node 2:
3
Enter the data for node 3:
2
Enter the data for node 4:
1
Enter your choice :
1: Create
2: Insert at beginning
3: Insert at position
4: Insert at end
5: Delete at beginning
6: Delete at position
7: Delete at end
8: Display
9: Count
Other: EXIT
4
Enter the data for new last node :
6
Enter your choice :
1: Create
2: Insert at beginning
3: Insert at position
4: Insert at end
5: Delete at beginning
6: Delete at position
7: Delete at end
8: Display
9: Count
Other: EXIT
2
Enter the data for new first node :
7
Enter your choice :
1: Create
2: Insert at beginning
3: Insert at position
4: Insert at end
5: Delete at beginning
6: Delete at position
7: Delete at end
8: Display
9: Count
Other: EXIT
8
Display the linked list
7->4->3->2->1->6->
Enter your choice :
1: Create
2: Insert at beginning
3: Insert at position
4: Insert at end
5: Delete at beginning
6: Delete at position
7: Delete at end
8: Display
9: Count
Other: EXIT
3
No of item in linked list : 6
Enter the position to insert node :
3
Enter the data for new node :
9
Node inserted sucessfully
Enter your choice :
1: Create
2: Insert at beginning
3: Insert at position
4: Insert at end
5: Delete at beginning
6: Delete at position
7: Delete at end
8: Display
9: Count
Other: EXIT
8
Display the linked list
7->4->9->3->2->1->6->
Enter your choice :
1: Create
2: Insert at beginning
3: Insert at position
4: Insert at end
5: Delete at beginning
6: Delete at position
7: Delete at end
8: Display
9: Count
Other: EXIT
5
First node deleted!
Enter your choice :
1: Create
2: Insert at beginning
3: Insert at position
4: Insert at end
5: Delete at beginning
6: Delete at position
7: Delete at end
8: Display
9: Count
Other: EXIT
7
Last node deleted!
Enter your choice :
1: Create
2: Insert at beginning
3: Insert at position
4: Insert at end
5: Delete at beginning
6: Delete at position
7: Delete at end
8: Display
9: Count
Other: EXIT
8
Display the linked list
4->9->3->2->1->
Enter your choice :
1: Create
2: Insert at beginning
3: Insert at position
4: Insert at end
5: Delete at beginning
6: Delete at position
7: Delete at end
8: Display
9: Count
Other: EXIT
6
No of item in linked list : 5
Enter the position to delete :
2
Node deleted Sucessfully
Enter your choice :
1: Create
2: Insert at beginning
3: Insert at position
4: Insert at end
5: Delete at beginning
6: Delete at position
7: Delete at end
8: Display
9: Count
Other: EXIT
8
Display the linked list
4->3->2->1->
Enter your choice :
1: Create
2: Insert at beginning
3: Insert at position
4: Insert at end
5: Delete at beginning
6: Delete at position
7: Delete at end
8: Display
9: Count
Other: EXIT
0
0 Comments