studious_gamer

Linked List

Sep 3rd, 2025
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.34 KB | Source Code | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Node {
  5.     int value;
  6.     Node *next;
  7. public:
  8.     Node(int val=0, Node *n=0) {
  9.         value = val;
  10.         next = n;
  11.     }
  12.     friend class LinkedList;
  13. };
  14.  
  15. class LinkedList {
  16.     Node *head;
  17. public:
  18.     LinkedList() {
  19.         head = 0;
  20.     }
  21.  
  22.     bool isempty() {
  23.         return head == 0;
  24.     }
  25.  
  26.     void print() {
  27.         Node *p;
  28.         p = head;
  29.         while (p)
  30.         {
  31.             cout << p->value;
  32.             if (p->next) {
  33.                 cout << " -> ";
  34.             }
  35.             p = p->next;
  36.         }
  37.         cout << "\n";
  38.         return;
  39.     }
  40.  
  41.     void add_start(int val) {
  42.         Node *p;
  43.         p = new Node(val);
  44.         if (head == 0) {
  45.             head = p;
  46.             return;
  47.         }
  48.         p->next = head;
  49.         head = p;
  50.         return;
  51.     }
  52.  
  53.     void add_end(int val) {
  54.         Node *p;
  55.         p = new Node(val);
  56.         if (head == 0) {
  57.             head = p;
  58.             return;
  59.         }
  60.         Node *q;
  61.         q = head;
  62.         while (q->next)
  63.         {
  64.             q = q->next;
  65.         }
  66.         q->next = p;
  67.         return;
  68.     }
  69.  
  70.     void add_ith(int val, int i) {
  71.         Node *p;
  72.         p = new Node(val);
  73.         if (head == 0) {
  74.             head = p;
  75.             return;
  76.         }
  77.         Node *q;
  78.         q = head;
  79.         int j = 0;
  80.         while (j<i-1)
  81.         {
  82.             q = q->next;
  83.             j++;
  84.         }
  85.         p->next = q->next;
  86.         q->next = p;
  87.         return;
  88.     }
  89.  
  90.     void delete_start(){
  91.         Node *p;
  92.         p = head;
  93.         head = head->next;
  94.         delete(p);
  95.         return;
  96.     }
  97.  
  98.     void delete_end(){
  99.         Node *p;
  100.         p = head;
  101.         while (p->next->next)
  102.         {
  103.             p = p->next;
  104.         }
  105.         Node *q;
  106.         q = p->next;
  107.         p->next = 0;
  108.         delete(q);
  109.         return;
  110.     }
  111.  
  112.     void delete_ith(int i){
  113.         Node *p;
  114.         p = head;
  115.         int j = 0;
  116.         while (j < i-1)
  117.         {
  118.             p = p->next;
  119.             j++;
  120.         }
  121.         Node *q;
  122.         q = p->next;
  123.         p->next = q->next;
  124.         delete(q);
  125.     }
  126.  
  127.     void reverse() {
  128.         Node *p;
  129.         Node *q;
  130.         Node *r;
  131.         p = head;
  132.         q = p->next;
  133.         r = q->next;
  134.         p->next = 0;
  135.         while (r)
  136.         {
  137.             q->next = p;
  138.             p = q;
  139.             q = r;
  140.             r = r->next;
  141.         }
  142.         q->next = p;
  143.         head = q;
  144.         return;
  145.     }
  146.  
  147.     int search(int val) {
  148.         Node *q;
  149.         q = head;
  150.         int j = 0;
  151.         while (q) {
  152.             if (q->value == val)
  153.                 return j;
  154.             q = q->next;
  155.             j++;
  156.         }
  157.         return -1;
  158.        
  159.     }
  160.  
  161.     void concat(LinkedList ll) {
  162.         Node *q;
  163.         q = head;
  164.         while (q->next)
  165.         {
  166.             q = q->next;
  167.         }
  168.         q->next = ll.head;
  169.         return;
  170.     }
  171.  
  172.     LinkedList merge(LinkedList ll) {
  173.         LinkedList new_list;
  174.         Node *h1;
  175.         Node *h2;
  176.         h1 = head;
  177.         h2 = ll.head;
  178.         while (h1 && h2) {
  179.             if (h1->value < h2->value) {
  180.                 new_list.add_end(h1->value);
  181.                 h1 = h1->next;
  182.             } else {
  183.                 new_list.add_end(h2->value);
  184.                 h2 = h2->next;
  185.             }
  186.         }
  187.         Node *val;
  188.         if (h1) {
  189.             val = h1;
  190.         } else {
  191.             val = h2;
  192.         }
  193.         Node *p;
  194.         while (p->next)
  195.         {
  196.             p = p->next;
  197.         }
  198.         p->next = val;
  199.         return new_list;
  200.     }
  201. };
  202.  
  203. int main() {
  204.     LinkedList list;
  205.     int nums[10] = {1, 3, 5, 7, 9, 2, 4, 6, 8, 0};
  206.     for (int i=0; i<10; i++) {
  207.         list.add_end(nums[i]);
  208.     }
  209.     list.add_ith(69, 2);
  210.  
  211.     list.delete_start();
  212.     list.delete_end();
  213.     list.delete_ith(2);
  214.    
  215.     list.print();
  216.    
  217.     list.reverse();
  218.     list.print();
  219.    
  220.     cout << list.search(7) << endl;
  221.    
  222.     LinkedList list2;
  223.     int nums2[5] = {2, 4, 6, 8, 10};
  224.     for (int i = 0; i < 5; i++)
  225.     {
  226.         list2.add_end(nums2[i]);
  227.     }
  228.     list2.print();
  229.     // list.concat(list2);
  230.     // list.print();
  231.     LinkedList new_list;
  232.     new_list = list.merge(list2);
  233.     new_list.print();
  234.     return 0;
  235. }
Advertisement
Add Comment
Please, Sign In to add comment