0% found this document useful (0 votes)
14 views22 pages

ProjectPdf 1

The document is a C++ implementation of a singly linked list, including various operations such as insertion, deletion, searching, and reversing the list. It also contains functions for stack and queue operations, including push, pop, enqueue, and dequeue. Additionally, there are methods for sorting the list and removing duplicates.

Uploaded by

zunayrajunaid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views22 pages

ProjectPdf 1

The document is a C++ implementation of a singly linked list, including various operations such as insertion, deletion, searching, and reversing the list. It also contains functions for stack and queue operations, including push, pop, enqueue, and dequeue. Additionally, there are methods for sorting the list and removing duplicates.

Uploaded by

zunayrajunaid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

1: #include <iostream>

2: using namespace std;


3:
4: struct node {
5: int data;
6: node* next;
7: };
8: node* head = NULL;
9: node* tail = NULL;
10:
11: struct dnode {
12: int data;
13: dnode* next;
14: dnode* prev;
15: };
16: dnode* dhead = NULL;
17:
18: node* list1 = NULL;
19: node* list2 = NULL;
20:
21: int size = 0;
22: node* top = NULL;
23: node* front = NULL;
24: node* rear = NULL;
25:
26: bool isEmpty() {
27: return head == NULL;
28: }
29:
30: void insertAtFront(int val) {
31: node* n = new node;
32: n->data = val;
33: n->next = head;
34: head = n;
35:
36: if (tail == NULL) {
37: tail = n;
38: }
39: }
40:
41: void insertAtTail(int val) {
42: node* n = new node;
43: n->data = val;
44: n->next = NULL;
45:
46: if (isEmpty()) {
47: head = n;
48: tail = n;
49: } else {
50: tail->next = n;
51: tail = n;
52: }
53: }
54:
55: void insertAfter(int val, int key) {
56: if (isEmpty()) {
57: cout << "List is empty." << endl;
58: return;
59: }
60:
61: node* temp = head;
62: while (temp != NULL && temp->data != val) {
63: temp = temp->next;
64: }
65:
66: if (temp == NULL) {
67: cout << "Value " << val << " not found in the list." << en
68: return;
69: }
70:
71: node* n = new node;
72: n->data = key;
73: n->next = temp->next;
74: temp->next = n;
75:
76: if (n->next == NULL) {
77: tail = n;
78: }
79: }
80:
81: void insertBefore(int val, int key) {
82: if (isEmpty()) {
83: cout << "List is empty." << endl;
84: return;
85: }
86:
87: if (head->data == val) {
88: insertAtFront(key);
89: return;
90: }
91:
92: node* temp = head;
93: while (temp->next != NULL && temp->next->data != val) {
94: temp = temp->next;
95: }
96:
97: if (temp->next == NULL) {
98: cout << "Value " << val << " not found in the list." << en
99: return;
100: }
101:
102: node* n = new node;
103: n->data = key;
104: n->next = temp->next;
105: temp->next = n;
106: }
107:
108: int getFront() {
109: if (isEmpty()) {
110: cout << "List is empty." << endl;
111: return -1;
112: }
113: return head->data;
114: }
115:
116: int getTail() {
117: if (isEmpty()) {
118: cout << "List is empty." << endl;
119: return -1;
120: }
121: return tail->data;
122: }
123:
124: node* search(int key) {
125: node* temp = head;
126: while (temp != NULL) {
127: if (temp->data == key) {
128: return temp;
129: }
130: temp = temp->next;
131: }
132: return NULL;
133: }
134:
135: void removeFront() {
136: if (isEmpty()) {
137: cout << "List is empty." << endl;
138: return;
139: }
140:
141: node* temp = head;
142: head = head->next;
143: delete temp;
144:
145: if (head == NULL) {
146: tail = NULL;
147: }
148: cout << "Front removed." << endl;
149: }
150:
151: void removeTail() {
152: if (isEmpty()) {
153: cout << "List is empty." << endl;
154: return;
155: }
156:
157: if (head->next == NULL) {
158: delete head;
159: head = NULL;
160: tail = NULL;
161: cout << "Tail removed." << endl;
162: return;
163: }
164:
165: node* temp = head;
166: while (temp->next != tail) {
167: temp = temp->next;
168: }
169:
170: delete tail;
171: tail = temp;
172: tail->next = NULL;
173: cout << "Tail removed." << endl;
174: }
175:
176: void reverse() {
177: if (isEmpty()) {
178: cout << "List is empty." << endl;
179: return;
180: }
181:
182: node* prev = NULL;
183: node* curr = head;
184: node* next = NULL;
185:
186: while (curr != NULL) {
187: next = curr->next;
188: curr->next = prev;
189: prev = curr;
190: curr = next;
191: }
192:
193: tail = head;
194: head = prev;
195:
196: cout << "List has reversed." << endl;
197: }
198:
199: void insert(int value) {
200: node* n = new node;
201: n->data = value;
202: n->next = NULL;
203:
204: if (isEmpty() || value <= head->data) {
205: n->next = head;
206: head = n;
207: if (tail == NULL) {
208: tail = n;
209: }
210: return;
211: }
212:
213: node* temp = head;
214: while (temp->next != NULL && temp->next->data < value) {
215: temp = temp->next;
216: }
217: n->next = temp->next;
218: temp->next = n;
219:
220: if (n->next == NULL) {
221: tail = n;
222: }
223: }
224:
225: void sortBubble() {
226: if (isEmpty()) {
227: cout << "List is empty." << endl;
228: return;
229: }
230:
231: node* prev;
232: node* curr;
233: int temp;
234:
235: for (prev = head; prev != NULL; prev = prev->next) {
236: for (curr = prev->next; curr != NULL; curr = curr->next) {
237: if (prev->data > curr->data) {
238: temp = prev->data;
239: prev->data = curr->data;
240: curr->data = temp;
241: }
242: }
243: }
244: cout << "List has sorted." << endl;
245: }
246:
247: void removeDup() {
248: if (isEmpty()) {
249: cout << "List is empty." << endl;
250: return;
251: }
252:
253: node* curr = head;
254:
255: while (curr != NULL) {
256: node* dup = curr;
257: while (dup->next != NULL) {
258: if (dup->next->data == curr->data) {
259: node* duplicate = dup->next;
260: dup->next = dup->next->next;
261: delete duplicate;
262: } else {
263: dup = dup->next;
264: }
265: }
266: curr = curr->next;
267: }
268: cout << "Duplicates removed." << endl;
269: }
270:
271: void createLoop(int pos) {
272: if (isEmpty()) {
273: cout << "List is empty." << endl;
274: return;
275: }
276:
277: node* temp = head;
278: node* loopNode = NULL;
279: int count = 1;
280:
281: while (temp->next != NULL) {
282: if (count == pos) {
283: loopNode = temp;
284: }
285: temp = temp->next;
286: count++;
287: }
288:
289: temp->next = loopNode;
290: cout << "Loop created from: " << pos << endl;
291: }
292:
293: node* findLoopStart() {
294: if (isEmpty()) {
295: cout << "List is empty." << endl;
296: return NULL;
297: }
298:
299: node* slow = head;
300: node* fast = head;
301:
302: while (fast != NULL && fast->next != NULL) {
303: slow = slow->next;
304: fast = fast->next->next;
305:
306: if (slow == fast) {
307: slow = head;
308: while (slow != fast) {
309: slow = slow->next;
310: fast = fast->next;
311: }
312: return slow;
313: }
314: }
315: return NULL;
316: }
317:
318: node* NthToLast(int n) {
319: if (isEmpty() || n <= 0) {
320: cout << "List is empty." << endl;
321: return NULL;
322: }
323:
324: node* temp = head;
325: node* curr = head;
326:
327: for (int i = 0; i < n; i++) {
328: if (curr == NULL) {
329: return NULL;
330: }
331: curr = curr->next;
332: }
333:
334: while (curr != NULL) {
335: temp = temp->next;
336: curr = curr->next;
337: }
338: return temp;
339: }
340:
341: void insertForAddList(node*& head, int value) {
342: node* n = new node;
343: n->data = value;
344: n->next = NULL;
345:
346: if (head == NULL) {
347: head = n;
348: } else {
349: node* temp = head;
350: while (temp->next != NULL) {
351: temp = temp->next;
352: }
353: temp->next = n;
354: }
355: }
356:
357: node* addLists(node* L1, node* L2) {
358: node* result = NULL;
359: node* temp = NULL;
360: int carry = 0;
361:
362: while (L1 || L2 || carry) {
363: int sum = carry;
364: if (L1) {
365: sum += L1->data;
366: L1 = L1->next;
367: }
368: if (L2) {
369: sum += L2->data;
370: L2 = L2->next;
371: }
372:
373: carry = sum / 10;
374: sum = sum % 10;
375:
376: node* newNode = new node();
377: newNode->data = sum;
378: newNode->next = NULL;
379:
380: if (result == NULL) {
381: result = newNode;
382: } else {
383: temp->next = newNode;
384: }
385: temp = newNode;
386: }
387:
388: return result;
389: }
390:
391: void displayForAddList(node* head) {
392: node* temp = head;
393: cout << "List: ";
394: while (temp != NULL) {
395: cout << temp->data << " -> ";
396: temp = temp->next;
397: }
398: cout << "NULL" << endl;
399: }
400:
401: void DeletePos(int val) {
402: if (isEmpty()) {
403: cout << "Linked List is empty!" << endl;
404: return;
405: }
406:
407: node* curr = head;
408: node* temp = NULL;
409:
410: if (val == 0) {
411: temp = head;
412: head = head->next;
413: delete temp;
414: return;
415: }
416:
417: for (int i = 0; curr != NULL && i < val - 1; i++) {
418: curr = curr->next;
419: }
420:
421: if (curr == NULL || curr->next == NULL) {
422: cout << "Invalid Position!" << endl;
423: return;
424: }
425:
426: temp = curr->next;
427: curr->next = curr->next->next;
428: delete temp;
429: }
430:
431: void push(int val) {
432: node* n = new node;
433: n->data = val;
434: n->next = top;
435: top = n;
436: size ++;
437: cout << val << " pushed to stack." << endl;
438: }
439:
440: void pop() {
441: if (top == NULL) {
442: cout << "Stack is underflow." << endl;
443: return;
444: }
445:
446: cout << "Popped element is: " << top->data << endl;
447: top = top->next;
448: size--;
449: }
450:
451: void displayStack() {
452: if (top == NULL) {
453: cout << "Stack is empty." << endl;
454: return;
455: }
456:
457: node* ptr = top;
458: cout << "Stack elements are: ";
459: while (ptr != NULL) {
460: cout << ptr->data << " ";
461: ptr = ptr->next;
462: }
463: cout << endl;
464: }
465:
466: void insertQueue(int val) {
467: node* n = new node;
468: n->data = val;
469: n->next = NULL;
470:
471: if (rear == NULL) {
472: front = rear = n;
473: } else {
474: rear->next = n;
475: rear = n;
476: }
477: cout << val << " enqueued." << endl;
478: }
479:
480: void deleteQueue() {
481: node* temp = front;
482:
483: if (front == NULL) {
484: cout << "Queue is underflow." << endl;
485: return;
486: }
487:
488: else
489: {
490: if (temp->next != NULL)
491: {
492: temp = temp->next;
493: cout << "Element deleted from queue is : " << front->d
494: delete (front);
495: front = temp;
496: }
497: else
498: {
499: cout << "Element deleted from queue is : " << front->d
500: delete (front);
501: front = NULL;
502: rear = NULL;
503: }
504: }
505: }
506:
507: void displayQueue() {
508: if (front == NULL) {
509: cout << "Queue is empty." << endl;
510: return;
511: }
512:
513: node* temp = front;
514: cout << "Queue elements are: ";
515: while (temp != NULL)
516: {
517: cout << temp->data << " | ";
518: temp = temp->next;
519: }
520: cout << endl;
521: }
522:
523: void display() {
524: if (isEmpty()) {
525: cout << "List is empty." << endl;
526: return;
527: }
528:
529: node* temp = head;
530: cout << "List: ";
531: for (int i = 0; i < 10; i++) {
532: cout << temp->data << " -> ";
533: temp = temp->next;
534: if (temp == NULL) {
535: break;
536: }
537: }
538: cout << "NULL" << endl;
539: }
540:
541: void Implementing_DLL(int value) {
542: dnode* newNode = new dnode;
543: newNode->data = value;
544: newNode->next = NULL;
545: newNode->prev = NULL;
546:
547: if (dhead == NULL) {
548: dhead = newNode;
549: } else {
550: newNode->next = dhead;
551: dhead->prev = newNode;
552: dhead = newNode;
553: }
554: }
555:
556: void DeletionIn_DLL() {
557: if (dhead == NULL) {
558: cout<<"List is Empty."<<endl;
559: return;
560: }
561:
562: dnode* temp = dhead;
563: dhead = dhead->next;
564: dhead->prev = NULL;
565: delete temp;
566: }
567:
568: void Display_DLL() {
569: dnode* curr = dhead;
570: cout << "Doubly Linked List: ";
571: while (curr != NULL) {
572: cout << curr->data << " <-> ";
573: curr = curr->next;
574: }
575: cout << "NULL" << endl;
576: }
577:
578: void Reverse_DLL() {
579: dnode* temp = NULL;
580: dnode* curr = dhead;
581:
582: while (curr != NULL) {
583: temp = curr->prev;
584: curr->prev = curr->next;
585: curr->next = temp;
586: curr = curr->prev;
587: }
588:
589: if (temp != NULL) {
590: dhead = temp->prev;
591: }
592: }
593:
594: int main() {
595: int choice, value, key, pos, n;
596:
597: do {
598: cout << "\n\t\t*SINGLE LINKED LIST*\n";
599: cout << "1. Insert at Front\t2. Insert at Tail\t2. Insert
600: cout << "5. Get Front\t\t6. Get Tail\t\t7. Search\t\t8. Re
601: cout << "9. Remove Tail\t\t10. Insert Sort\t\t11. Reverse
602: cout << "13. Remove Duplicates\t14. Get Nodes from Nth to
603: cout << "17. Create Loop or Circular\t\t18. Get Beginning
604: cout << "\n\t\t*STACK*\n";
605: cout << "20. Push in Stack\t21. Pop from Stack\n";
606: cout << "\n\t\t*Queue*\n";
607: cout << "22. Insert in Queue\t23. Delete from Queue\n";
608: cout << "\n\t\t*DOUBLE LINKED LIST*\n";
609: cout << "24. Insert in DList\t25. Delete in DList\n\n";
610: cout << "26. Display DList\t27. Reverse DList\n\n";
611: cout << "28. Exit\n";
612: cout << "Enter your choice: ";
613: cin >> choice;
614:
615: switch (choice) {
616: case 1:
617: cout << "Enter value to insert at front: ";
618: cin >> value;
619: insertAtFront(value);
620: break;
621:
622: case 2:
623: cout << "Enter value to insert at tail: ";
624: cin >> value;
625: insertAtTail(value);
626: break;
627:
628: case 3:
629: cout << "Enter value after which to insert: ";
630: cin >> key;
631: cout << "Enter value to insert: ";
632: cin >> value;
633: insertAfter(key, value);
634: break;
635:
636: case 4:
637: cout << "Enter value before which to insert: ";
638: cin >> key;
639: cout << "Enter value to insert: ";
640: cin >> value;
641: insertBefore(key, value);
642: break;
643:
644: case 5:
645: value = getFront();
646: if (value != -1) {
647: cout << "Front element: " << value << endl;
648: } else {
649: cout << "List is empty." << endl;
650: }
651: break;
652:
653: case 6:
654: value = getTail();
655: if (value != -1) {
656: cout << "Tail element: " << value << endl;
657: } else {
658: cout << "List is empty." << endl;
659: }
660: break;
661:
662: case 7:
663: cout << "Enter value to search: ";
664: cin >> key;
665: if (search(key)) {
666: cout << "Element " << key << " found in the list."
667: } else {
668: cout << "Element " << key << " not found in the li
669: }
670: break;
671:
672:
673: case 8:
674: removeFront();
675: break;
676:
677: case 9:
678: removeTail();
679: break;
680:
681: case 10:
682: cout << "Enter value to insert with sorting: ";
683: cin >> value;
684: insert(value);
685: break;
686:
687: case 11:
688: reverse();
689: break;
690:
691: case 12:
692: sortBubble();
693: break;
694:
695: case 13:
696: removeDup();
697: break;
698:
699: case 14:
700: cout << "Enter N to get Nth to last node: ";
701: cin >> n;
702: if (node* nthNode = NthToLast(n)) {
703: cout << "The " << n << "th to last node is: " << n
704: } else {
705: cout << "Invalid N value or list is empty." << end
706: }
707: break;
708:
709: case 15: {
710: int pos;
711: cout << "Enter position to delete: ";
712: cin >> pos;
713: DeletePos(pos);
714: break;
715: }
716:
717: case 16: {
718: int count;
719: cout << "Enter the number of nodes for List 1: ";
720: cin >> count;
721: for (int i = 0; i < count; i++) {
722: cout << "Enter value: ";
723: cin >> value;
724: insertForAddList(list1, value);
725: }
726: cout << "Enter the number of nodes for List 2: ";
727: cin >> count;
728: for (int i = 0; i < count; i++) {
729: cout << "Enter value: ";
730: cin >> value;
731: insertForAddList(list2, value);
732: }
733: node* result = addLists(list1, list2);
734: cout << "Sum of Lists: ";
735: displayForAddList(result);
736: break;
737: }
738:
739: case 17:
740: cout << "Enter position to create loop: ";
741: cin >> pos;
742: createLoop(pos);
743: break;
744:
745: case 18: {
746: node* loopStart = findLoopStart();
747: if (loopStart) {
748: cout << "Loop starts at node with value: " << loop
749: } else {
750: cout << "No loop detected." << endl;
751: }
752: break;
753: }
754:
755: case 19:
756: display();
757: break;
758:
759: case 20:
760: cout << "Enter value to push onto stack: ";
761: cin >> value;
762: push(value);
763: displayStack();
764: break;
765:
766: case 21:
767: pop();
768: displayStack();
769: break;
770:
771: case 22:
772: cout << "Enter value to enqueue: ";
773: cin >> value;
774: insertQueue(value);
775: displayQueue();
776: break;
777:
778: case 23:
779: deleteQueue();
780: displayQueue();
781: break;
782:
783: case 24:
784: cout << "Enter value to insert in Doubly Linked List:
785: cin >> value;
786: Implementing_DLL(value);
787: break;
788:
789: case 25:
790: DeletionIn_DLL();
791: break;
792:
793: case 26:
794: Display_DLL();
795: break;
796:
797: case 27:
798: Reverse_DLL();
799: Display_DLL();
800: break;
801:
802: case 28:
803: cout << "Exiting program." << endl;
804: break;
805:
806: default:
807: cout << "Invalid choice. Please try again." << endl;
808: }
809: } while (choice != 28);
810:
811: return 0;
812: }
813:
814:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy