29 August 2012

Data Structure Programs

0) Program of singly linked list
1) Program on Linked list, New node insert at begining position.
2)Program on Linked list, New node insert at begining position.
3)Program on Linked list, New node insert so that information field and store in ascending order.
4)Program: To delete a node whose value is given by x from singly Linked list.
 #include<iostream.h>  
 #include<conio.h>  
 #include<stdlib.h>  
 struct list  
 {  
 int info;  
 struct list *link;  
 }*node1,*temp,*first;  
 int ch,x;  
 void menu()  
 {  
 cout<<endl<<"singly linked list";  
 cout<<endl<<"=======================";  
 cout<<endl<<"1.insert a nod";  
 cout<<endl<<"2.display a nod";  
 cout<<endl<<"3.display in reverse";  
 cout<<endl<<"0.Exit";  
 cout<<endl<<"=======================";  
 cout<<endl<<"enter your choice:";  
 cin>>ch;  
 }  
 void insert()  
 {  
 cout<<"Enter x:";  
 cin>>x;  
 node1=(struct list *)malloc(10);  
 node1->info=x;  
 node1->link=NULL;  
 if(first==NULL)  
 first=node1;  
 else  
 {  
 node1->link=first;  
 first=node1;  
 }  
 }  
 void rev()  
 {  
 temp=first;  
 int i=0;  
 while(temp!=NULL)  
 {  
 i++;  
 temp=temp->link;  
 }  
      for(;i>0;i--)  
      {  
           temp=first;  
           for(int j=1;j<i;j++)  
           temp=temp->link;  
           cout<<temp->info<<" ";  
      }  
 }  
 void display()  
 {  
 temp=first;  
 while(temp!=NULL)  
 {  
 cout<<temp->info<<" ";  
 temp=temp->link;  
 }  
 }  
 void del()  
  {  
   int x;  
     if (first==NULL)  
       cout<<endl<<"Underflow...";  
     else  
     {  
        cout<<"Enter value of x :";  
        cin>>x;  
        temp=first;  
        while(temp->link!=NULL && temp->info!=x)  
        {  
           pred=temp;  
           temp=temp->link;  
        }  
        if(temp->info != x)  
           cout<<"Node not found.";  
        else  
           if( first->info==x)  
            first=first->link;  
           else  
            pred->link=temp->link;  
     }  
  }  
 void main()  
 {  
 first=(struct list *)malloc(10);  
 first=NULL;  
 clrscr();  
 do  
 {  
 clrscr();  
 menu();  
 if(ch==1) insert();  
 if(ch==2) display();  
 if(ch==3) rev();  
 if(ch==4) del();  
  getch();  
 }  
 while(ch!=0);  
 }  
  //Program on Linked list, New node insert at begining position.  
  #include<iostream.h>  
  #include<conio.h>  
  #include<stdlib.h>  
  struct list  
  {  
    int  info;  
    struct list *link;  
  };  
 struct list *node1,*first,*temp;  
 int ch,x;  
  void menu()  
  {  
      cout<<endl<<"Linked List ";  
      cout<<endl<<"=========";  
      cout<<endl<<"1. Insert ";  
      cout<<endl<<"2. Display ";  
      cout<<endl<<"3. Exit ";  
      cout<<endl<<"Enter your choice...";  
      cin>>ch;  
  }  
  void insert()  
  {  
      cout<<"Enter element...";  
      cin>>x;  
      node1 = (struct list *)malloc(10);  
      node1->info = x;  
      node1->link = NULL;  
      if ( first == NULL )  
      {  
         first = node1;  
      }  
      else  
      {  
           node1->link = first;  
           first = node1;  
      }  
  }  
  void display()  
  {  
      cout<<endl<<"Linked List Elements are...";  
      temp = first;  
      while (temp != NULL)  
      {  
           cout<<temp->info<<",";  
           temp = temp->link;  
      }  
  }  
  void main()  
  {  
    first = ( struct list *)malloc(10);  
    first = NULL ;  
    do  
    {  
      clrscr();  
      menu();  
      if (ch==1) insert();  
      if (ch==2) display();  
      getch();  
    }  
    while ( ch != 3 );  
  }  
  //Program on Linked list, New node insert at end.  
  #include<iostream.h>  
  #include<conio.h>  
  #include<stdlib.h>  
  struct list  
  {  
    int  info;  
    struct list *link;  
  };  
 struct list *node1,*first,*temp;  
 int ch,x;  
  void menu()  
  {  
      cout<<endl<<"Linked List ";  
      cout<<endl<<"=========";  
      cout<<endl<<"1. Insert ";  
      cout<<endl<<"2. Display ";  
      cout<<endl<<"3. Exit ";  
      cout<<endl<<"Enter your choice...";  
      cin>>ch;  
  }  
  void insert()  
  {  
      cout<<"Enter element...";  
      cin>>x;  
      node1 = (struct list *)malloc(10);  
      node1->info = x;  
      node1->link = NULL;  
      if ( first == NULL )  
      {  
         first = node1;  
      }  
      else  
      {  
           temp = first;  
           while ( temp->link != NULL )  
                temp = temp->link;  
           temp->link = node1;  
      }  
  }  
  void display()  
  {  
      cout<<endl<<"Linked List Elements are...";  
      temp = first;  
      while (temp != NULL)  
      {  
           cout<<temp->info<<",";  
           temp = temp->link;  
      }  
  }  
  void main()  
  {  
    first = ( struct list *)malloc(10);  
    first = NULL ;  
    do  
    {  
      clrscr();  
      menu();  
      if (ch==1) insert();  
      if (ch==2) display();  
      getch();  
    }  
    while ( ch != 3 );  
  }  
  //Program on Linked list, New node insert so that information field  
  //store in ascending order.  
  #include<iostream.h>  
  #include<conio.h>  
  #include<stdlib.h>  
  struct list  
  {  
    int  info;  
    struct list *link;  
  };  
 struct list *node1,*first,*temp,*succ;  
 int ch,x;  
  void menu()  
  {  
      cout<<endl<<"Linked List ";  
      cout<<endl<<"=========";  
      cout<<endl<<"1. Insert ";  
      cout<<endl<<"2. Display ";  
      cout<<endl<<"3. Exit ";  
      cout<<endl<<"Enter your choice...";  
      cin>>ch;  
  }  
  void insert()  
  {  
      cout<<"Enter element...";  
      cin>>x;  
      node1 = (struct list *)malloc(10);  
      node1->info = x;  
      node1->link = NULL;  
      if ( first == NULL )  
      {  
         first = node1;  
      }  
      else  
        if(node1->info<=first->info)  
        {  
           node1->link = first;  
           first = node1;  
        }  
        else  
        {  
           temp = first;  
           succ =temp->link;  
           while ( temp->link != NULL && succ->info <= node1->info )  
           {  
                temp=temp->link;  
                succ=temp->link;  
           }  
           node1->link=temp->link;  
           temp->link=node1;  
        }  
  }  
  void display()  
  {  
      cout<<endl<<"Linked List Elements are...";  
      temp = first;  
      while (temp != NULL)  
      {  
           cout<<temp->info<<",";  
           temp = temp->link;  
      }  
  }  
  void main()  
  {  
    first = ( struct list *)malloc(10);  
    first = NULL ;  
    do  
    {  
      clrscr();  
      menu();  
      if (ch==1) insert();  
      if (ch==2) display();  
      getch();  
    }  
    while ( ch != 3 );  
  }  
 //Program: To delete a node whose value is given by x from singly Linked list.  
  #include<iostream.h>  
  #include<conio.h>  
  #include<stdlib.h>  
  struct list  
  {  
    int  info;  
    struct list *link;  
  };  
 struct list *node1,*first,*temp,*pred;  
 int ch,x;  
  void menu()  
  {  
      cout<<endl<<"Linked List ";  
      cout<<endl<<"=========";  
      cout<<endl<<"1. Insert ";  
      cout<<endl<<"2. Delete ";  
      cout<<endl<<"3. Display";  
      cout<<endl<<"0. Exit ";  
      cout<<endl<<"Enter your choice...";  
      cin>>ch;  
  }  
  void insert()  
  {  
      cout<<"Enter element...";  
      cin>>x;  
      node1 = (struct list *)malloc(10);  
      node1->info = x;  
      node1->link = NULL;  
      if ( first == NULL )  
      {  
         first = node1;  
      }  
      else  
      {  //New node insert at end of a list.  
           temp=first;  
           while(temp->link!=NULL)  
                temp=temp->link;  
           temp->link = node1;  
      }  
  }  
  void del()  
  {  
   int x;  
     if (first==NULL)  
       cout<<endl<<"Underflow...";  
     else  
     {  
        cout<<"Enter value of x :";  
        cin>>x;  
        temp=first;  
        while(temp->link!=NULL && temp->info!=x)  
        {  
           pred=temp;  
           temp=temp->link;  
        }  
        if(temp->info != x)  
           cout<<"Node not found.";  
        else  
           if( first->info==x)  
            first=first->link;  
           else  
            pred->link=temp->link;  
     }  
  }  
  void display()  
  {  
    if(first==NULL)  
      cout<<endl<<"Linked list empty...";  
    else  
    {  
      cout<<endl<<"Linked List Elements are...";  
      temp = first;  
      while (temp != NULL)  
      {  
           cout<<temp->info<<",";  
           temp = temp->link;  
      }  
    }  
  }  
  void main()  
  {  
    first = ( struct list *)malloc(10);  
    first = NULL ;  
    do  
    {  
      clrscr();  
      menu();  
      if (ch==1) insert();  
      if(ch==2) del();  
      if (ch==3) display();  
      getch();  
    }  
    while ( ch != 0 );  
  }