Tuesday, 4 November 2014

// program for singly linked list..........


#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
}*start;


void create(int);
void insertAtBeg(int );
void insertAtMid(int , int );
void insertAtEnd(int );

void count();
void search(int);
void sort();
void delt(int);
void display();

int main()
{

  int a, dt,pos;
   start = NULL;
   while(1)
     {
      printf("\n 1 for create a node");
      printf("\n 2 for insert an item at begning");
      printf("\n 3 for insert a node at specific position");
      printf("\n 4 for insert a node at end");
      printf("\n 5 for sorting  ");
      printf("\n 6 for count how many nodes in list ");
      printf("\n 7 for search an item");
      printf("\n 8 for Deleting the item");
      printf("\n 9 for displaying all item of list");
     
     
     
      printf("\n Enter your choice \t");
      scanf("%d",&a);
      switch(a)
      {
      case 1: printf("\n Enter element to a node");
             scanf("%d",&dt);
             create(dt);
             break;
           
          case 2: printf("\n Enter element to a node");
             scanf("%d",&dt);
             insertAtBeg(dt);
             break;
           
               
          case 3:
       printf("\n Enter element to a node and their position");
             scanf("%d%d",&dt,&pos);
             insertAtMid(dt,pos);
             break;
           

case 4:         printf("\n Enter element to a node");
             scanf("%d",&dt);
             insertAtEnd(dt);
             break;
     
case 5:   sort();
        break;
       
        case 6:  count();
               break;
       
          case 7:         printf("\n Enter element to be search");
             scanf("%d",&dt);
             search(dt);
             break;
        case 8:          printf("\n Enter element to be deleted");
             scanf("%d",&dt);
             delt(dt);
             break;
           
         case 9: display();
                 break;
       

      }
     
     }

return 0;
}

void create(int d)
{
   struct node *x , *y , *z;
 
   x =  (struct node*)malloc(sizeof(struct node));
 
   x->info = d;
   x->link = NULL;
   if(start == NULL)
   start = x;
   else
   {
 
      y = start;
      while(y->link!= NULL)
      {
      y = y->link;
      }
      y->link= x;
  }
     
}
 
void delt(int d)
{
int dt;
struct node  *y,*x,*z;

if(start == NULL)
printf("\n List is empty,Deletion is not possible\n");
else
  {
 
  y= start;
  if(y->info== d)
  {
     dt == d;
  y = start;
  start = y->link;
  free(y);
  }
  else
  {
  y= start;
   z = start;
    while(y->link->link!=NULL)
   {
    if(y->link->info==d)
    {
    dt == d;
    x= y->link;
    y->link = x->link;
    free(x);
    return ;
    }
 
    y= y->link;
 
      }
      if(y->link->info == d)
      { dt = d;
     
      z=y->link ;
     y->link = NULL;
 
      free(z);
     
     
      }

 
  }
  if (d== dt)
   printf("\n deleted element is -  %d\t",d);
   else
    printf("\n  element is not found..,,,\t");
 
 
      }
}  
 
void display()  
{
    struct node  *y;

if(start == NULL)
{

printf("\n List is empty\n");
}
else
{
y= start;
printf("\n Element of List are..........\n");
while(y!=NULL)
{
printf("\t %d",y->info);
y= y->link;
}
}
 }
 
 
   void insertAtBeg(int d)
{
   struct node *x ;
 
   x =  (struct node*)malloc(sizeof(struct node));
 
   x->info = d;
   x->link = NULL;
   if(start == NULL)
 
   start = x;
   else
   {
 
    x->link = start;
    start = x;
  }
     
}

  void insertAtMid(int d,int p)
{
int i;
   struct node *x ,*y;
 
   x =  (struct node*)malloc(sizeof(struct node));
 
   x->info = d;
   x->link = NULL;
   if(start == NULL)
 
   start = x;
   else
   { y= start;
   for(i=1; i<p-1;i++)
    y=y->link ;
   
x->link = y->link;
y->link = x;
  }
     
}

  void insertAtEnd(int d)
{
   struct node *x ,*y;
 
   x =  (struct node*)malloc(sizeof(struct node));
 
   x->info = d;
   x->link = NULL;
   if(start == NULL)
 
   start = x;
   else
   {
     y = start;
     while(y->link!=NULL)
     y=y->link;
   
     y->link = x;
  }
     
}

void sort()
{
struct node *x,*y,*z;
int t;

if(start == NULL)
printf("\n list is empty\n");
else
{
y= start;
x= y->link;
for(;y!= NULL;y=y->link)
{
 for(;x!=NULL;x=x->link)
  {
    if(y->info > x->info)
    {
    t= x->info;
    x->info = y->info;
   
    y->info = t;
    printf("\t %d",y->info);
    }
  }
  printf("\n");
   }
z= start;
printf("\n sorted elements of list are....\n");  
while(z!=NULL)  
{
printf("\t %d",z->info);
z= z->link;
}
}
}

void search(int d)
{
int f=0,c=1;
struct node *p;
if(start == NULL)
printf("\n list is empty\n");
else
{
p=start;
while(p!=NULL)
{
if(p->info == d)
{
f=1;
break;
}
p=p->link;
c++;

}
if(f==1)
printf("\n %d found at %d pos....",d,c);
else
printf("\n %d is not found....",d,c);
}
}

void count()
{
int c=0;
struct node  *y;

if(start == NULL)
{

printf("\n List is empty\n");
}
else
{
y= start;

while(y!=NULL)
{

y= y->link;
c++;
}
printf("\n no of Element in List is ....\t %d",c);
}
}


output-----

No comments:

Post a Comment

Note: only a member of this blog may post a comment.