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 (SV) Program - Append Link List


 
 
 
 
 
 
 
 
Append Link list: Prog code by Sulakshana Maam.
This program creates sorted linked list.
 
 
 

  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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include<stdlib.h>
#include<iostream.h>
#include<conio.h>
//This program creates sorted linked list.
//1.Append two linked lists
  
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) //adding before first node or to empty list

 {
  pNew->link = pList->head;
  pList->head =pNew;
  //pNew->link=NULL;
  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;
  //while(pWalk!=NULL)
  i=0;
  cout<<"\n";
  while (i<pList->count)
  {
   cout<<pWalk->data<<"\t";
   pWalk=pWalk->link;
   i++;
  }
  cout<<"\n";
  cout<<"Num ber of elements in linked list is \n"<<pList->count;
 }
}
void appendList(HEAD *pList1, HEAD *pList2)
{
 NODE *pWalk;
 pWalk=pList1->head;
 if(pWalk==NULL)
 {
  cout<<"\n Linked list 1 is empty";
  pList1->head=pList2->head;
  pList1->count=pList2->count;
  traverse(pList1);
 }
 else
 {
  while(pWalk->link!=NULL)
   pWalk=pWalk->link;
  pWalk->link=pList2->head;
  pList1->count=pList1->count + pList2->count;
  traverse(pList1);
 }
}
};
int flag;

void main()
{

 int choice,datain;
 HEAD *pList1, *pList2;
 clrscr();
 linklist l;
 cout<<"\nFor first linked list";
 pList1=l.createHead();
 cout<<"\nFor second linked list";
 pList2=l.createHead();
 while(1)
 {
  cout<<"1. 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.Append Lists\n";
  cout<<"9. 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:
    l.appendList(pList1,pList2);
   break;
   
   case 9:
    l.destroyList(pList1);
    exit(1);
    break;
   default: cout<<"Illegal option\n";
  }
 }
}
© Copyright NMITD MCA Notes Published.. Blogger Templates
Back To Top