News
  • Submission of Theory Assignments 6,7,8 of OS on 1st April
  • Case Study Presentation of OS on 4st April
  • CN and PNS Journal Submission on 6th April

DS (RM) Program - Linked List - Implementation of two singly Linked Lists

DS (RM) Program - Linked List -     Implementation of two singly Linked Lists





  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include<stdlib.h>
#include<iostream.h>
#include<conio.h>

typedef struct node
{
   int data;
   struct node *link;
}NODE;

typedef struct {
  int count;
  int *pos;
   NODE *head;
}HEAD;

class linklist
{
 HEAD *pNew;
 public:
 HEAD *createHead()
 {

  pNew = new HEAD;
  if(pNew)
  {
   pNew->head = NULL;
   pNew->count = 0;
   cout<<"Memory allocated\n";
  }
  else
   pNew = NULL;
  return pNew;
 }
 void insertNode(HEAD *pList,int dain)
 {
 int loc,i;
 NODE *pNew,*pPre,*temp;
 pPre =pList->head;

 pNew = new NODE;
 if(pNew == NULL)
  cout<<"\nMemory overflow.\n";
else
 {
  pNew->data = dain;

 }
 temp = pList->head;
 if (dain<temp->data)
 {
  pPre=NULL;
  temp=NULL;
 }
 while (temp!=NULL)
 {
  if(dain>temp->data)
  {
   pPre=temp;
   temp=temp->link;

  }
  else
   temp=temp->link;

 }
 if(pPre==NULL)

 {
  pNew->link = pList->head;
  pList->head =pNew;

  pList->count = pList->count+1;
  cout<<"\ndata is inserted.\n";
 }
 else
 {
   pNew->link = pPre->link;
   pPre->link = pNew;
   pList->count = pList->count+1;
   cout<<"\ndata is inserted.\n";
  }

}
void traverse(HEAD *pList)
{
 NODE *pWalk;
 int i;
 if(pList->head==NULL)
  cout<<"List is empty\n";
 else
{
  pWalk = pList->head;

  i=0;
  cout<<"\n";
  while (i<pList->count)
  {
   cout<<pWalk->data<<"\t";
   pWalk=pWalk->link;
   i++;
  }
  cout<<"\n";
  cout<<"Number of elements in linked list is \n"<<pList->count;
 }
}
};
int flag;

void main()
{

 int choice,datain;
 HEAD *pList1, *pList2;
 clrscr();
 linklist l;
 cout<<"For first linked list\n";
 pList1=l.createHead();
 cout<<"\nFor second linked list\n";
 pList2=l.createHead();
 while(1)
 {
  cout<<"\n1. Insert linked list node\n";
  cout<<"2. Insert node into second linked list\n";
  cout<<"3. Traverse linked list1\n";
  cout<<"4. Traverse linked list2\n";
  cout<<"5. Exit\n";
  cout<<"\nEnter choice:";
  cin>>choice;
  switch(choice)
  {


   case 1:
    cout<<"\nEnter the element to be inserted\n";
    cin>>datain;
    l.insertNode(pList1,datain);
   break;
   case 2:
cout<<"\nEnter the element to be inserted\n";
    cin>>datain;
    l.insertNode(pList2,datain);

   break;
   case 3:
    l.traverse(pList1);
   break;
   case 4:
    l.traverse(pList2);
   break;
   case 5:
    exit(1);
    break;
   default: cout<<"Illegal option\n";
  }
 }
}
 Output:

For first linked list                    
Memory allocated                         
                                         
For second linked list                   
Memory allocated                         
                                         
1. Insert linked list node               
2. Insert node into second linked list   
3. Traverse linked list1                 
4. Traverse linked list2                 
5. Exit                                  
                                         
Enter choice:1                           
                                         
Enter the element to be inserted         
12                                       

                                             
data is inserted.                            
                                             
1. Insert linked list node                   
2. Insert node into second linked list       
3. Traverse linked list1                     
4. Traverse linked list2                     
5. Exit       
Enter choice:1
                                                             
Enter the element to be inserted                             
33                                                           
                                                             
data is inserted.                                            
                                                             
1. Insert linked list node                                   
2. Insert node into second linked list                       
3. Traverse linked list1                                     
4. Traverse linked list2                                     
5. Exit                                                      
               
Enter choice:1                                
                                              
Enter the element to be inserted              
27                                            
                                              
data is inserted.                             
                                              
1. Insert linked list node                    
2. Insert node into second linked list        
3. Traverse linked list1                      
4. Traverse linked list2                      
5. Exit                                       
                                                                                                 
Enter choice:3                                 
                                               
12      27      33                             
Number of elements in linked list is           
3                                              
1. Insert linked list node                     
2. Insert node into second linked list         
3. Traverse linked list1                       
4. Traverse linked list2                       
5. Exit                                        
                                               
                                                
Enter choice:2                                  
                                                
Enter the element to be inserted                
14                                              
                                                
data is inserted.                               
                                                
1. Insert linked list node                      
2. Insert node into second linked list          
3. Traverse linked list1 
4. Traverse linked list2                        
5. Exit                                         
                                                
                                                   
Enter choice:2                                     
                                                   
Enter the element to be inserted                   
5                                                  
                                                   
data is inserted.                                  
                                                   
1. Insert linked list node                         
2. Insert node into second linked list             
3. Traverse linked list1                           
4. Traverse linked list2                           
5. Exit                                            
                                                   
Enter choice:2                                 
                                               
Enter the element to be inserted               
20                                             
                                               
data is inserted.                              
                                               
1. Insert linked list node                     
2. Insert node into second linked list         
3. Traverse linked list1                       
4. Traverse linked list2                       
5. Exit                                        
                                               
                                                
Enter choice:4                                  
                                                
5       14      20                              
Number of elements in linked list is            
3                                               
1. Insert linked list node                      
2. Insert node into second linked list          
3. Traverse linked list1                        
4. Traverse linked list2                        
5. Exit                                         
                                                
Enter choice:5   
 
© Copyright NMITD MCA Notes Published.. Blogger Templates
Back To Top