You are on page 1of 7

//Link List

//----------------------------------------------------------------------------//
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
//----------------------------------------------------------------------------//
typedef struct node node;

//To form tree nodes

struct node
{
int data;
node *next;
};
//----------------------------------------------------------------------------//
node
void
node
void
node

*create_node();
//To
insert(node *);
//To
*insert_atanyposition(node
display(node *);
//To
*delete_node(node *); //To

create head node


insert node at the end
*); //To insert node at any position
display nodes
delete nodes

//To sort the list by interchanging the nodes


node* sel_sort_node(node *);
node* insertsort_node(node *);
node* bubblesort_node(node *);
//----------------------------------------------------------------------------//
main(void)
{
node *list;
int ch=1;
list=create_node();
while(ch==1 || ch==2 || ch==3 || ch==4 || ch==5 || ch==6)
{
printf("\nEnter choice\n");
printf(" 1:Insert at end\n 2:Insert at any position\n");
printf(" 3:Delete\n 4:Selection Sort\n 5:Insertion Sort\n");
printf(" 6:Bubble Sort\n");
scanf("%d",&ch);
if(ch==1)
insert(list);
if(ch==2)
list = insert_atanyposition(list);
if(ch==3)
list = delete_node(list);
if(ch==4)
list = sel_sort_node(list);
if(ch==5)
list = insertsort_node(list);

if(ch==6)
list = bubblesort_node(list);
}
}
//----------------------------------------------------------------------------//
node *create_node()
{
node *list;
node *head;
int n;
printf("\nEnter the number of elements : ");
scanf("%d",&n);
list=(node*)malloc(sizeof(node));
head=list;
printf("\nEnter the elements:\n");
scanf("%d",&(list->data));
//1st element
list->next=NULL;
n--;
while(n--)
//Remaining elements
{
list->next=(node*)malloc(sizeof(node));
list=list->next;
scanf("%d",&(list->data));
list->next=NULL;
}
return head;
}
//----------------------------------------------------------------------------//
void insert(node *list)
//To insert at the end of the list
{
node *head=list;
while(list->next!=NULL ) //To reach the last node
list=list->next;
list->next=(node*)malloc(sizeof(node));
list=list->next;
printf("\nEnter the element.\n");
scanf("%d",&(list->data));
list->next=NULL;
display(head);
}

//----------------------------------------------------------------------------//
node *insert_atanyposition(node *list)
{
node * head=list,*temp;
int pos,count=1;
temp=(node*)malloc(sizeof(node));
temp->next=NULL;
printf("\nEnter the position at which node is to be inserted:\n");
scanf("%d",&pos);
printf("\nEnter the element.\n");
scanf("%d",&(temp->data));
if(list==NULL)
head=temp;
while(list!=NULL)
{
if(pos==1)
//1st position
{
head=temp;
temp->next=list;
break;
}
else if(pos==count+1) //Any position
{
temp->next=list->next;
list->next=temp;
break;
}
list=list->next;
count++;
}
display(head);
return head;
}
//----------------------------------------------------------------------------//
node *delete_node(node *list)
{
node *head=list,*temp;
int data,flag=0;
printf("\nEnter the data to be deleted.\n");
scanf("%d",&data);
while(list!=NULL)
{
if(list->data==data)
{
flag=1;

if(head==list)
{
head=list->next;
free(list);
break;
}
else
{
temp->next=list->next;
free(list);
break;
}
}
temp=list;
list=list->next;
}
if(flag==1)
display(head);
else
printf("\nElement not found\n");
return head;
}
//----------------------------------------------------------------------------//
//Selection Sort
node* sel_sort_node(node * list)
{
node *temp,*head=list,*ptemp=NULL,*plist=list,*mid;
int i;
while(list!=NULL)
{
i=0;
ptemp=list;
temp = list->next;
while(temp!=NULL)
{
if( list->data > temp->data )
{
if(list==head)
{
head = temp;
if(i==1) //For rest of nodes but not c
onsecutive
{
ptemp->next = list;
//Swapping the next's of temp a
nd list nodes
mid = temp->next;
temp->next = list->next;
list->next = mid;
}
else

//For first 2 and consecutive n

odes
{
list->next = temp->next;
temp->next = list;
i=1;
}
}
else
{
if(i==0) //For 2 consecutive nodes
{
plist->next = temp;
list->next = temp->next;
temp->next = list;
i=1;
}
else //For rest of nodes but not cons
ecutive
{
plist->next = temp;
ptemp->next = list;
//Swapping the next's of temp a
nd list nodes
mid = temp->next;
temp->next = list->next;
list->next = mid;
}
}
mid = list;
list = temp;
temp = mid;
}
ptemp = temp;
temp = temp->next;
} //End of inner while loop
plist = list;
list = list->next;
}
display(head);
return head;
}
//----------------------------------------------------------------------------//
//Insertion Sort
node* insertsort_node(node *list)
{
node *temp,*ptemp,*plist,*head=list;
plist = list;
list = list->next;
while(list!=NULL)
{
temp=head;
while(temp!=list)
{
if(list->data < temp->data)

{
if(temp==head)
{
head = list;
plist->next = list->next;
list->next = temp;
}
else
{
plist->next = list->next;
ptemp->next = list;
list->next = temp;
}
list = plist;
break;
}
ptemp = temp;
temp=temp->next;
} // Inner while loop
plist = list;
list = list->next;
}
display(head);
return head;
}
//----------------------------------------------------------------------------//
//Bubble Sort
node* bubblesort_node(node *list)
{
node *temp,*ptemp=list,*end=NULL,*head=list,*ntemp;
while(list!=NULL)
{
temp=head;
ntemp = temp->next;
while(temp!=end)
{
if(temp->data > ntemp->data)
{
if(temp==head)
head = ntemp;
else
ptemp->next = ntemp;
temp->next = ntemp->next;
ntemp->next = temp;
ptemp = ntemp;
if(temp->next == end)
end = temp;
else
ntemp = temp->next;
}//end of if
else
{
ptemp = temp;
temp = temp->next;
if(temp->next == end)

end = temp;
else
ntemp = temp->next;
}
}//End of inner while loop
end = temp;
list = list->next;
}
display(head);
return head;
}
//----------------------------------------------------------------------------//
void display(node *list)
{
printf("\n");
while(list!=NULL)
{
printf("%d->",list->data);
list=list->next;
}
printf("\n");
}
//----------------------------------------------------------------------------//

You might also like