C Program for Linked List Implementation
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node*link;
};
void insert_begin_sll(struct node**,int);
void traverse_sll(struct node*);
void insert_end_sll(struct node **,int);
void insert_position_sll(struct node**,int,int);
void delete_begin_sll(struct node **);
void delete_end_sll(struct node **);
void delete_position_sll(struct node **,int);
void search_sll(struct node**,int);
int main()
{
int ch,position,key;
struct node *start=NULL;
while(1)
{
printf("Select from the menu\n");
printf("1.Insert node at beginning\n2.Traversing\n3.Insert node at end\n4.Insert node at specific position\n5.Delete node from beginning\n6.Delete node from end\n7.Delete node from specified position\n8.searching\n9.exit\n");
printf("Enter a choice\n");
scanf("%d",&ch);
switch (ch)
{
case 1:
printf("Enter key\n");
scanf("%d",&key);
insert_begin_sll(&start,key);
break;
case 2:
traverse_sll(start);
break;
case 3:
printf("Enter key\n");
scanf("%d",&key);
insert_end_sll(&start,key);
break;
case 4:
printf("Enter key\n");
scanf("%d",&key);
printf("Enter position\n");
scanf("%d",&position);
insert_position_sll(&start,key,position);
break;
case 5:
delete_begin_sll(&start);
break;
case 6:
delete_end_sll(&start);
break;
case 7:
printf("Enter position\n");
scanf("%d",&position);
delete_position_sll(&start,position);
break;
case 8:
printf("Enter the key to be searched\n");
scanf("%d",&key);
search_sll(&start,key);
break;
case 9:
exit(0);
}
}
}
void insert_begin_sll(struct node **start,int key)
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
if (temp==NULL)
printf("Node is not created \n Insertion is not possible\n");
else
{
temp->data=key;
temp->link=NULL;
temp->link=*start;
*start=temp;
}
}
void traverse_sll(struct node *start)
{
struct node *temp;
if (start==NULL)
printf("Linked list is empty\n");
else
{
temp=start;
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->link;
}
printf("NULL\n");
}
}
void insert_end_sll(struct node**start,int key)
{
struct node *temp,*temp1;
temp=(struct node *)malloc(sizeof(struct node));
if (temp==NULL)
printf("node is not created\nInsertion is not possible\n");
else
{
temp->data=key;
temp->link=NULL;
if(*start==NULL)
*start=temp;
else
{
temp1=*start;
while(temp1->link!=NULL)
temp1=temp1->link;
temp1->link=temp;
}
}
}
void insert_position_sll(struct node **start,int key,int position)
{
struct node *temp1,*temp;
int count;
if (position<=0)
printf("position is invalid\n");
else
{
temp=(struct node*)malloc(sizeof(struct node));
if (temp==NULL)
printf("Node is not created\nInsertion is not possible\n");
else
{
temp->data=key;
temp->link=NULL;
if (position==1)
{
temp->link=*start;
*start=temp;
}
else
{
temp1=*start;
count=1;
while (count<position-1 && temp1!=NULL)
{
temp1=temp1->link;
count++;
}
if(temp1==NULL)
printf("Linked list is out of range\n");
else
{
temp->link=temp1->link;
temp1->link=temp;
}
}
}
}
}
void delete_begin_sll(struct node **start)
{
struct node *temp;
if(*start==NULL)
printf("Linked list is empty\n Deletion is not possible\n");
else
{
temp=*start;
*start=(*start)->link;
free(temp);
}
}
void delete_end_sll(struct node **start)
{
struct node *prev,*temp;
if(*start==NULL)
printf("Linked list is empty \nDeletion is not possible\n");
else
{
temp=*start;
prev=*start;
while(temp->link!=NULL)
{
prev=temp;
temp=temp->link;
}
if(temp==prev)
*start=NULL;
else
prev->link=NULL;
free(temp);
}
}
void delete_position_sll(struct node **start,int position)
{
struct node *temp,*prev;
int count;
if (position<=0)
printf("position is invalid\n");
else
{
if (position==1)
{
temp=*start;
*start=(*start)->link;
}
else
{
temp=*start;
prev=*start;
count=1;
}
while (count<position && temp!=NULL)
{
prev=temp;
temp=temp->link;
count++;
}
if(temp==NULL)
printf("Linked list is out of range\n");
else
prev->link=temp->link;
free(temp);
}
}
void search_sll(struct node **start,int key)
{
struct node *temp;
int flag=0;
if(start==NULL)
printf("Linked list is empty.\nSearching is not possible\n");
else
{
temp=*start;
while (temp!=NULL && flag==0)
{
if (key==temp->data)
flag=1;
temp=temp->link;
}
if (flag==0)
printf("key is not found\n");
else
printf("Key is found\n");
}
}
Output
No comments:
Post a Comment