Sei sulla pagina 1di 7

1 #include<iostream> 2 3 using namespace std; 4 5 typedef struct node 6 { 7 int data; // will store information 8 node *next; // the

reference to the next node 9 }; 10 11 12 int main() 13 { 14 node *head = NULL; //empty lin ed list 15 int info = 0, node_number = 0, counter = 0; 16 char ch; 17 18 do{ 19 cout<<"\n\n"; 20 cout<<"0.Quit\n"; 21 cout<<"1.Insert at first\n"; 22 cout<<"2.Traverse\n"; 23 cout<<"3.Insert at last\n"; 24 cout<<"4.Insert after specified number of node\n"; 25 cout<<"5.Delete at first node\n"; 26 cout<<"6.Delete at last node\n"; 27 cout<<"7.Delete specified number of node\n"; 28 cout<<"8.Sort nodes\n"; 29 30 cout<<"Enter your choice: "; 31 cin>>ch; 32 33 switch(ch) 34 { 35 36 case '0': brea ; 37 38 case '1':{ 39 cout<<"ENTER ANY NUMBER:"; 40 cin>>info; // ta e input dat a 41 cout<<"Input data: "<<info<<" Node No: "<<++counte r; 42 node *temp; // create a tem porary node 43 temp = (node*)malloc(sizeof(node)); // allocate spa ce for node 44 temp->data = info; // store data( first field) 45 temp->next=head; // store the addre ss of the pointer head(second field) 46 head = temp; // transfer the address of 'temp' to 'head' 47 brea ; 48 } 49 50 51 case '2':{ 52 node *temp1; // create a temporary node

temp1 = temp1->next; to 'temp' }

cout<<endl<<"Number of nodes: "<<counter<<endl; brea ; }

cout<<"Input data: "<<info; node *temp; // create a tem

temp = (node*)malloc(sizeof(node)); // allocate temp->data = info; temp->next = NULL; head = temp; counter++; } else { // store data( // second fiel // transfer the

cout<<"Input data: "<<info; node *temp1;

// create a

temp1=(node*)malloc(sizeof(node)); // allocate temp1 = head; while(temp1->next!=NULL) // transfer the // go to the l

cout<<"ENTER ANY NUMBER:"; cin>>info;

// ta e input

// chec lin ed list is empty if(head==NULL) { cout<<"ENTER ANY NUMBER:"; cin>>info;

53 address of 'head' to 'temp' 54 55 56 57 58 59 60 61 62 63 64 the data in the lin ed list 65 the address of 'temp->next' 66 67 68 69 70 71 } 72 73 74 case '3':{ 75 76 77 78 79 data 80 81 82 porary node 83 space for node 84 first field) 85 d will be null 86 address of 'temp' to 'head' 87 88 89 90 91 92 93 data 94 95 temporary node 96 space for node 97 address of 'head' to 'temp' 98 ast node

temp1 = head;

// transfer the

if(temp1 == NULL) { cout<<endl<<"The lin ed list is empty"<<endl; brea ; } else { cout<<"Lin ed list: "; while( temp1!=NULL ) { cout<< temp1->data<<" "; // show // tranfer

// ta e input

99 temp1 = temp1->next; //tranfer the address of 'temp->next' to 'temp' 100 101 node *temp; // create a tempora ry node 102 temp = (node*)malloc(sizeof(node));// allocate space for node 103 temp->data = info; // store data( first field) 104 temp->next = NULL; // second fiel d will be null(last node) 105 temp1->next = temp; // 'temp' node will be the last node 106 brea ; 107 } 108 } 109 110 111 case '4':{ 112 // chec lin ed list is empty 113 if(head==NULL) 114 { 115 cout<<"The Lin ed List is empty"<<endl; 116 brea ; 117 } 118 119 cout<<"ENTER ANY NUMBER:"; 120 cin>>info; // ta e input data 121 cout<<"Input data: "<<info<<endl; 122 123 cout<<"ENTER THE NODE NUMBER:"; 124 cin>>node_number; // ta e the n ode number from user 125 126 node *temp1; // create a temporary node 127 temp1 = (node*)malloc(sizeof(node)); // allocate space for node 128 temp1 = head; 129 130 131 for( int i = 1 ; i < node_number ; i++ ) 132 { 133 temp1 = temp1->next; // go to the n ext node 134 135 if( temp1 == NULL ) 136 { 137 cout<<node_number<<" node is not exist"<< e ndl; 138 brea ; 139 } 140 } 141 142 node *temp; // create a temporary node 143 temp = (node*)malloc(sizeof(node)); // allocate space for node 144 temp->data = info; // store data(

first field) 145 temp->next = temp1->next; //transfer th e address of temp1->next to temp->next 146 temp1->next = temp; //transfer the add ress of temp to temp1->next 147 counter++; 148 brea ; 149 } 150 151 152 case '5':{ 153 // chec lin ed list is empty 154 if(head==NULL) 155 { 156 cout<<"The Lin ed List is empty"<<endl; 157 brea ; 158 } 159 else // chec lin ed list has only one node 160 if(head->next==NULL) 161 { 162 head = NULL; 163 cout<<"The first node of the Lin ed List is deleted"<<endl; 164 cout<<"The Lin ed List is empty"<<endl; 165 counter--; 166 brea ; 167 } 168 169 node *temp; // create a tem porary node 170 temp = (node*)malloc(sizeof(node)); // allocate spa ce for node 171 temp = head; // transfer the address of 'head' to 'temp' 172 head = temp->next; // transfer th e address of 'temp->next' to 'head' 173 free(temp); 174 cout<<"The first node of the Lin ed List is deleted "<<endl; 175 counter--; 176 brea ; 177 } 178 179 case '6':{ 180 // chec lin ed list is empty 181 if(head==NULL) 182 { 183 cout<<"The Lin ed List is empty"<<endl; 184 brea ; 185 } 186 else // chec lin ed list has only one node 187 if(head->next==NULL) 188 { 189 head = NULL; 190 cout<<"The last node of the Lin ed List is deleted"<<endl; 191 cout<<"The Lin ed List is empty"<<endl; 192 counter--; 193 brea ; 194 }

195 196 197 node *temp1; // create a temporary node 198 temp1 = (node*)malloc(sizeof(node)); // allocate space for node 199 temp1 = head; //transfer the address of head to temp1 200 201 node *old_temp; // create a temporary node 202 old_temp = (node*)malloc(sizeof(node)); // allocate space for node 203 204 while(temp1->next!=NULL) // go to the last node 205 { 206 old_temp = temp1; // transfer the add ress of 'temp1' to 'old_temp' 207 temp1 = temp1->next; // transfer the ad dress of 'temp1->next' to 'temp1' 208 } 209 210 old_temp->next = NULL; // previous node o f the last node is null 211 212 free(temp1); 213 cout<<"The last node of the Lin ed List is deleted" <<endl; 214 counter--; 215 brea ; 216 } 217 218 219 case '7':{ 220 // Chec if lin list is empty 221 if(head==NULL) 222 { 223 cout<<"The Lin ed List is empty"<<endl; 224 brea ; 225 } 226 else // chec lin ed list has only one node 227 if(head->next==NULL) 228 { 229 head = NULL; 230 cout<<"The last node of the Lin ed List is deleted"<<endl; 231 cout<<"The Lin ed List is empty"<<endl; 232 counter--; 233 brea ; 234 } 235 236 cout<<"ENTER THE NODE NUMBER:"; 237 cin>>node_number; // ta e locat ion 238 239 // Chec if node is exist 240 if(node_number > counter) 241 { 242 cout<<"No such node is exist";

243 brea ; 244 } 245 246 node *temp1; // create a temporary node 247 temp1 = (node*)malloc(sizeof(node)); // allocate space for node 248 temp1 = head; // transfer the address of 'head' to 'temp1' 249 250 node *old_temp; // create a temporary node 251 old_temp = (node*)malloc(sizeof(node)); // allocate space for node 252 old_temp = temp1; // transfer the address of 'temp1' to 'old_temp' 253 254 // Chec node number is 1 255 if( node_number == 1 ) 256 { 257 head = temp1->next; // transfe r the address of 'temp1->next' to 'head' 258 free(temp1); 259 counter--; 260 cout<<node_number<<" node of the Lin ed List is deleted"<<endl; 261 brea ; 262 } 263 264 // Go to the node number of the node 265 for( int i = 1 ; i < node_number ; i++ ) 266 { 267 268 old_temp = temp1; // store previo us node 269 temp1 = temp1->next; // store curre nt node 270 271 } 272 273 old_temp->next = temp1->next; // transfer the a ddress of 'temp1->next' to 'old_temp->next' 274 free(temp1); 275 276 counter--; 277 cout<<node_number<<" node of the Lin ed List is del eted"<<endl; 278 brea ; 279 280 } 281 282 283 case'8':{ 284 node *temp1; // create a tem porary node 285 temp1 = (node*)malloc(sizeof(node)); // allocate spa ce for node 286 287 node *temp2; // create a tem porary node

288 temp2 = (node*)malloc(sizeof(node)); // allocate spa ce for node 289 290 int temp = 0; // store tempor ary data value 291 292 for( temp1 = head ; temp1!=NULL ; temp1 = temp1->next ) 293 { 294 for( temp2 = temp1->next ; temp2!=NULL ; temp2 = te mp2->next ) 295 { 296 if( temp1->data > temp2->data ) 297 { 298 temp = temp1->data; 299 temp1->data = temp2->data; 300 temp2->data = temp; 301 } 302 } 303 } 304 305 cout<<"Nodes of the Lin ed List is sorted"<<endl; 306 brea ; 307 308 309 310 311 } 312 313 } 314 315 316 317 318 }while(ch!='0'); 319 320 321 322 323 324 return 0; 325 }

Potrebbero piacerti anche