|
| 1 | +#include <iostream> |
| 2 | +using namespace std; |
| 3 | + |
| 4 | +struct node |
| 5 | +{ |
| 6 | + int data; |
| 7 | + node *next; |
| 8 | +}; |
| 9 | + |
| 10 | +class list{ |
| 11 | + private: |
| 12 | + node *head; |
| 13 | + public: |
| 14 | + list(){ |
| 15 | + head=NULL; |
| 16 | + } |
| 17 | +}; |
| 18 | + |
| 19 | +int main() |
| 20 | +{ |
| 21 | + node *head; |
| 22 | + head=NULL; |
| 23 | + int n,t,pos,repeat,value; |
| 24 | + label: |
| 25 | + cout<<"\n ------------"<<endl; |
| 26 | + cout<<" LINKED LIST "<<endl; |
| 27 | + cout<<" ------------"<<endl; |
| 28 | + cout<<"\n Select the operation number you want to perform:\n"<<endl; |
| 29 | + cout<<" 1. Insert data at beginning of the link list. \n"; |
| 30 | + cout<<" 2. Insertion of a data at a particular position. \n"; |
| 31 | + cout<<" 3. Insert data at end of the link list. \n"; |
| 32 | + cout<<" 4. Deletion of a node at a particular position. \n"; |
| 33 | + cout<<" 5. Print the link list. \n"; |
| 34 | + cout<<" 6. Sort the link list. \n"; |
| 35 | + cout<<" 7. Reversing a link list. \n"; |
| 36 | + cout<<" 8. Exit. \n"; |
| 37 | + cin>>t; |
| 38 | + |
| 39 | +switch (t) |
| 40 | +{ |
| 41 | + case 1://at beginnning |
| 42 | + { |
| 43 | + cout<<"Enter the data : "; |
| 44 | + cin>>value; |
| 45 | + node *temp = new node ; |
| 46 | + temp->data = value; |
| 47 | + temp->next = NULL; |
| 48 | + |
| 49 | + if(head == NULL) |
| 50 | + { |
| 51 | + head= temp; |
| 52 | + temp=NULL; |
| 53 | + } |
| 54 | + else |
| 55 | + { |
| 56 | + temp->next = head; |
| 57 | + head = temp; |
| 58 | + } |
| 59 | + } |
| 60 | + goto label; |
| 61 | + |
| 62 | + case 2://at position |
| 63 | + { |
| 64 | + int count=1; |
| 65 | + int inc; |
| 66 | + cout<<"Enter the position where you want to insert: "; |
| 67 | + cin>>pos; |
| 68 | + { |
| 69 | + node *ptr = new node; |
| 70 | + ptr = head; |
| 71 | + while(ptr->next!=NULL) |
| 72 | + { |
| 73 | + ptr=ptr->next; |
| 74 | + count++; |
| 75 | + } |
| 76 | + } |
| 77 | + inc=count+1; |
| 78 | + cout<<inc; |
| 79 | + if(pos<=inc) |
| 80 | + { |
| 81 | + |
| 82 | + if (pos == 1) |
| 83 | + { |
| 84 | + cout<<"Enter the data in the node: "; |
| 85 | + cin>>value; |
| 86 | + node *temp = new node ; |
| 87 | + temp->data = value; |
| 88 | + temp->next = head; |
| 89 | + head = temp; |
| 90 | + } |
| 91 | + |
| 92 | + else |
| 93 | + { |
| 94 | + cout<<"Enter the data in the node: "; |
| 95 | + cin>>value; |
| 96 | + node *temp = new node; |
| 97 | + node *prev = new node; |
| 98 | + node *curr = new node; |
| 99 | + |
| 100 | + curr = head; |
| 101 | + for(int i=1;i<pos;i++) |
| 102 | + { |
| 103 | + prev=curr; |
| 104 | + curr=curr->next; |
| 105 | + } |
| 106 | + temp->data= value; |
| 107 | + prev->next= temp; |
| 108 | + temp->next= curr; |
| 109 | + } |
| 110 | + } |
| 111 | + else |
| 112 | + { |
| 113 | + cout<<"INVALID POSITION!"; |
| 114 | + } |
| 115 | + } |
| 116 | + goto label; |
| 117 | + |
| 118 | + case 3:// at end |
| 119 | + { |
| 120 | + cout<<"Enter the data : "; |
| 121 | + cin>>value; |
| 122 | + if (head==NULL) |
| 123 | + { |
| 124 | + cout<<"\nThe list is empty.\n"; |
| 125 | + } |
| 126 | + else |
| 127 | + { |
| 128 | + node *temp = new node; |
| 129 | + node *curr = new node; |
| 130 | + temp->data = value; |
| 131 | + temp->next = NULL; |
| 132 | + curr=head; |
| 133 | + |
| 134 | + while(curr->next!=NULL) |
| 135 | + { |
| 136 | + curr= curr->next; |
| 137 | + } |
| 138 | + |
| 139 | + curr->next = temp; |
| 140 | + temp->next = NULL; |
| 141 | + } |
| 142 | + } |
| 143 | + goto label; |
| 144 | + |
| 145 | + case 4: //delete node |
| 146 | + { |
| 147 | + int count=1; |
| 148 | + cout<<"Enter the position you want to delete: "; |
| 149 | + cin>>pos; |
| 150 | + node *check = new node; |
| 151 | + check = head; |
| 152 | + |
| 153 | + |
| 154 | + while(check->next!=NULL) |
| 155 | + { |
| 156 | + check= check->next; |
| 157 | + count ++; |
| 158 | + } |
| 159 | + |
| 160 | + if (pos<=count && pos>0) |
| 161 | + { |
| 162 | + |
| 163 | + if (pos == 1) |
| 164 | + { |
| 165 | + node *curr = new node; |
| 166 | + curr = head; |
| 167 | + head = curr->next; |
| 168 | + cout<<"\nDeletion successful"; |
| 169 | + } |
| 170 | + |
| 171 | + else |
| 172 | + { |
| 173 | + node *prev = new node; |
| 174 | + node *curr = new node; |
| 175 | + |
| 176 | + curr = head; |
| 177 | + for(int i=1;i<pos;i++) |
| 178 | + { |
| 179 | + prev=curr; |
| 180 | + curr=curr->next; |
| 181 | + } |
| 182 | + prev->next = curr->next; |
| 183 | + cout<<"\nDeletion sucessful!"; |
| 184 | + } |
| 185 | + } |
| 186 | + else |
| 187 | + { |
| 188 | + cout<<"INVALID POSITION!"; |
| 189 | + } |
| 190 | + } |
| 191 | + goto label; |
| 192 | + |
| 193 | + case 5: //print linklist |
| 194 | + { |
| 195 | + node *temp=new node; |
| 196 | + temp=head; |
| 197 | + cout<<endl; |
| 198 | + while(temp!=NULL) |
| 199 | + { |
| 200 | + cout<<temp->data<<" "; |
| 201 | + temp=temp->next; |
| 202 | + } |
| 203 | + } |
| 204 | + goto label; |
| 205 | + |
| 206 | + case 6://sorting |
| 207 | + { |
| 208 | + int box; |
| 209 | + node *temp = new node; |
| 210 | + temp=head; |
| 211 | + node *curr = new node; |
| 212 | + curr = temp->next; |
| 213 | + |
| 214 | + if(head=NULL){ |
| 215 | + cout<<"The link list is empty."; |
| 216 | + } |
| 217 | + |
| 218 | + while(temp->next!=NULL) |
| 219 | + { |
| 220 | + |
| 221 | + while(curr!=NULL) |
| 222 | + { |
| 223 | + if (temp->data > curr->data) |
| 224 | + { |
| 225 | + box=curr->data; |
| 226 | + curr->data=temp->data; |
| 227 | + temp->data=box; |
| 228 | + } |
| 229 | + curr=curr->next; |
| 230 | + } |
| 231 | + temp=temp->next; |
| 232 | + curr=temp->next; |
| 233 | + } |
| 234 | + } goto label; |
| 235 | + |
| 236 | + case 7: //reversing linklist |
| 237 | + { |
| 238 | + node *temp = new node; |
| 239 | + node *curr = new node; |
| 240 | + node *prev = new node; |
| 241 | + |
| 242 | + if(head == NULL) |
| 243 | + { |
| 244 | + cout<<"List is empty!\n"; |
| 245 | + } |
| 246 | + |
| 247 | + else{ |
| 248 | + prev = NULL; |
| 249 | + curr = head; |
| 250 | + temp = NULL; |
| 251 | + |
| 252 | + while (curr!=NULL) |
| 253 | + { |
| 254 | + temp = curr->next ; |
| 255 | + curr->next = prev; |
| 256 | + prev = curr; |
| 257 | + curr = temp; |
| 258 | + } |
| 259 | + head = prev; |
| 260 | + } |
| 261 | + |
| 262 | + } goto label; |
| 263 | + |
| 264 | + case 8: |
| 265 | + { |
| 266 | + goto exit; |
| 267 | + }break; |
| 268 | + |
| 269 | + default: |
| 270 | + { |
| 271 | + cout<<"\nINVALID ENTRY!!!"; |
| 272 | + } |
| 273 | +} |
| 274 | + exit: ; |
| 275 | + |
| 276 | +} |
0 commit comments