Sunday 27 January 2013

Write a program to Insert and Delete an element in the Sorted Singular Linked List.


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
               int data;
               struct node *link;
}*start=NULL;

     void create_list()
               {
               char ch;
               do
               {
                               if(start==NULL)
                               {
                                              struct node *temp;
                                              temp=new node;
                                              cout<<"enter data:-";
                                              cin>>temp->data;
                                              temp->link=NULL;
                                              start=temp;
                               }
                               else
                               {
                                              struct node *temp,*ptr;
                                              temp=new node;
                                              cout<<"enter data:-";
                                              cin>>temp->data;
                                              temp->link=NULL;
                                              ptr=start;
                                              while(ptr->link!=NULL)
                                              {
                                                             ptr=ptr->link;
                                              }
                                              ptr->link=temp;

                               }
                               cout<<"do u want to continue(y/n):-";
                               cin>>ch;
               }while(ch=='y');
               }//end of function

               void sorted()
               {
                               struct node *temp;
                               temp=new node;
                               cout<<"enter data:-";
                               cin>>temp->data;
                               temp->link=NULL;
                               if(temp->data<start->data)
                               {
                                              temp->link=start;
                                              start=temp;
                              }
               else
               {
                               struct node *ptr1,*ptr;
                               ptr=start;
                               ptr1=start->link;
                               while(ptr1->data<temp->data)
                               {
                                              if(ptr1->link==NULL && ptr1->data<temp->data)
                                              {
                                                             ptr=ptr1;
                                                             break;
                                              }
                               ptr=ptr1;
                               ptr1=ptr1->link;
                               }
                temp->link=ptr->link;
                ptr->link=temp;
               }
               }//end of function

               void display()
               {
                               struct node *ptr;
                               ptr=start;
                               cout<<endl<<"first linked list is:-"<<endl;
                               while(ptr->link!=NULL)
                               {
                                              cout<<" "<<ptr->data<<"  ";
                                              ptr=ptr->link;
                               }//end of while loop
                                              cout<<" "<<ptr->data<<" ";
               }

               void main()
               {
                               clrscr();
                               int choice;
                               int n,loc;
                               cout<<" press 1. create linked list"<<endl;
                               cout<<" press 2. enter the element in the sorted list"<<endl;
                               cout<<" press 3.display linked list"<<endl;
                               cout<<" press 4. EXIT"<<endl;
                              while(1)
                               {
                                              cout<<" enter your choice:-";
                                              cin>>choice;
                                              switch(choice)
                                              {
                               case 1:
                                              create_list();
                                              break;
                               case 2:
                                              sorted();
                                              break;
                               case 3:
                                              display();
                                              break;
                               case 4:
                                              exit(0);
                                              break;
                               default:
                                              cout<<"try again";
                                              break;
                                              }//end of switch
                               getch();
                               }//end of while
               }//end of main








No comments:

Post a Comment