Bigger

Saturday, 8 November 2014

All in one program on Singly Linked List

//--------------------------------All about 1-way List..-------------------

#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node * link;   
};
struct node *addatbeg(struct node *start,int num);
void addatend(struct node *start,int num);
int count(struct node *start);
void search(struct node *start,int num);
struct node * del(struct node *start,int num);
void display(struct node *start);
void addafter(struct node *start,int num,int num1);
struct node*addbef(struct node*start,int num,int num1);
struct node *reverse (struct node *start);
void alter (struct node * start);
void findmiddlenode(struct node*start);
void sort(struct node * start);
int main()
{
struct node * start=NULL;
int choice,num,num1;   
while(1)   
{
printf("\n-------------Enter the choice-----------\n");
printf("1.Display\n");
printf("2.Add at begin\n");
printf("3.Add at End\n");
printf("4.Add after the node\n");
printf("5.Add before the node\n");
printf("6.Delete a node\n");   
printf("7.Count number of nodes\n");
printf("8.Search an element\n");
printf("9.Reverse the list\n");
printf("10.Delete alternatively\n");
printf("11.Find Middle One\n");
printf("12.Sort the List\n");
printf("13.Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
display(start);
break;
case 2:
printf("Enter the element\n");
scanf("%d",&num);
start=addatbeg(start,num);
break;   
case 3:
printf("Enter the element\n");
scanf("%d",&num);
addatend(start,num);
break;
case 4:
printf("Enter the element\n");
scanf("%d",&num);
printf("Enter the element after which you want to add new node\n");
scanf("%d",&num1);
printf("Initaly our");
display(start);
addafter(start,num,num1);
printf("\nFinaly our");
display(start);
break;
case 5:
printf("Enter the element\n");
scanf("%d",&num);
printf("Enter the element before which you want to add new node\n");
scanf("%d",&num1);
printf("Initaly our");
display(start);
start=addbef(start,num,num1);
printf("\nFinaly our");
display(start);
break;
case 6:
printf("Enter the element you want to delete\n");
scanf("%d",&num);
start=del(start,num);
break;
case 7:
count(start);
break;
case 8:
printf("Enter the element to be searched\n");
scanf("%d",&num);
search(start,num);
break;
case 9:
printf("\nIntilally ");
display(start);
start=reverse(start);
printf("\nFinally ");
display(start);
break;   
case 10:
printf("\nIntilally ");
display(start);
alter(start);
printf("\nFinally ");
display(start);
break;
case 11:
findmiddlenode(start);   
break;
case 12:
printf("\nIntilally ");
display(start);
sort(start);
printf("\nFinally ");
display(start);
break;   
case 13:
printf("Terminating..........................\n");
exit(1);
break;
default:
printf("Invalid choice\n");   
}
}
return 0;   
}
void display(struct node * start)
{
if(start==NULL)
{
printf("The List is empty\n");   
return ;
}   
else
{
struct node *p=start;   
printf(" List is:- \n");
while(p!=NULL)
{
printf(" %d ",p->info);
p=p->link;   
}
}
}
struct node *addatbeg(struct node *start,int num)
{
struct node * temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->info=num;   
temp->link=start;
start=temp;
return start;
}
void addatend(struct node *start,int num)
{
if(start==NULL)
{
printf("This function can't be used to insert element in an empty list\n");   
return ;
}
else
{
struct node * temp,*p;
p=start;
temp=(struct node *)malloc(sizeof(struct node));
temp->info=num;
while(p->link!=NULL)
{
p=p->link;   
}
p->link=temp;
temp->link=NULL;   
}   
}
int count(struct node *start)
{
if(start==NULL)
{
printf("The List is empty\n");
return 0;   
}   
else
{
struct node* p=start;
int total=0;
while(p!=NULL)
{
p=p->link;   
total++;
}
printf("The total number of nodes are:-%d\n",total);
return total;
}
}

void search(struct node* start,int num)
{
struct node*p=start;
int nm=0;
if(start==NULL)
{
printf("The List is empty\n");
return ;   
}
else
{
while(p!=NULL)
{
nm++;
if(p->info==num)
{
printf("%d is found at %d Node in List",num,nm);   
return ;
}
p=p->link;   
}
}
printf("%d is not found in our List\n",num);   
   
}
struct node * del(struct node *start,int num)
{   
if(start==NULL)   
{
printf("The List is empty");   
return start;
}
else
{
struct node*temp,*p=start;
if(start->info==num)
{
start=p->link;
free(p);
return start;   
}
while(p->link!=NULL)
{
if(p->link->info==num)
{
temp=p->link;
p->link=temp->link;
free(temp);
return start;   
}
p=p->link;
}
printf("%d is not found in the list",num);
return start;
}
}
void addafter(struct node *start,int num,int num1)
{
if(start==NULL)
{
printf("This function can't be used to insert element in an empty list\n");   
return ;
}
struct node*p,*temp;   
p=start;
temp=(struct node *)malloc(sizeof(struct node));
temp->info=num;
while(p!=NULL)
{
if(p->info==num1)
{
temp->link=p->link;
p->link=temp;
return ;   
}
p=p->link;   
}
printf("%d doesn't exist in the list",num1);
return ;
}

struct node *reverse (struct node *start)
{
if(start==NULL)
{
return start;   
}   
else
{
struct node *p=start,*q=NULL,*temp;
while(p!=NULL)
{
temp=p;
p=p->link;
temp->link=q;
q=temp;   
}
return(q);   
}
}
void alter (struct node * p)
{
if(p==NULL||p->link==NULL)   
{
return ;   
}
else
{
struct node* temp;
temp=p->link;
p->link=temp->link;
free(temp);   
alter(p->link);   
}
}
struct node*addbef(struct node*start,int num,int num1)
{
if(start==NULL)
{
printf("This fxn can't be used to add element to an empty List\n");
return start;   
}   
else
{
struct node*p=start,*temp,*pre=NULL;   
temp=(struct node*)malloc(sizeof(struct node));
while(p!=NULL)//doing search
{
if(p->info==num1)
{
if(p==start)
{
temp->info=num;
temp->link=start;
return(start=temp);   
}
else
{
temp->info=num;
temp->link=pre->link;
pre->link=temp;
return start;   
}   
}
pre=p;
p=p->link;   
}
printf("No Such node %d is found\n",num1);
return start;
}
}
void findmiddlenode(struct node*start)
{
if(start==NULL)
{
printf("List is empty");
return ;   
}   
else
{
int what=count(start),c=0;   
struct node *p=start;
if(what%2==0)
{
printf("There are 2 middle nodes\n");
while(p->link!=NULL)
{
c++;
if(c==(what/2)||c==(what/2+1))
{
printf("%d is the middle node and data stored here is %d\n",c,p->info);   
}   
p=p->link;   
}
}   
else
{
printf("There is only 1 middle node\n");
while(p->link!=NULL)
{
c++;
if(c==((what+1)/2))
{
printf("%d is the middle node and data stored here is %d\n",c,p->info);   
}   
p=p->link;   
}
}
}
}

void sort(struct node * start)
{
if(start==NULL)
{
printf("The List is empty\n");
return ;   
}
else
{
int temp;
struct node *p=start;

while(p->link!=NULL)
{
if(p->info>p->link->info)
{
temp=p->info;
p->info=p->link->info;
p->link->info=temp;   
p=p->link;
}   
}   
}   
}

No comments:

Post a Comment