BlackWolfy

Find common elements between two lists (Linked List)

Nov 2nd, 2023
1,006
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | None | 0 0
  1. #include <iostream>
  2. //struct for each separate node
  3. struct Node {
  4.     int data;
  5.     Node* next;
  6.     //constructor
  7.     Node(int value) {
  8.         data = value;
  9.         next = nullptr;
  10.     }
  11. };
  12. //struct for the linked list
  13. struct LinkedList {
  14.     Node* head;
  15.     //constructor
  16.     LinkedList() {
  17.         head = nullptr;
  18.     }
  19.     void insert(int value) {
  20.         Node* newNode = new Node(value);
  21.         if (head == nullptr) {
  22.             head = newNode; //create head node
  23.         }
  24.         else {
  25.             Node* current = head;
  26.             //while the next element in list exists
  27.             while (current->next != nullptr) {
  28.                 current = current->next;
  29.             }
  30.             current->next = newNode;    //updates the next pointer to the current node
  31.         }
  32.     }
  33.     void display() {
  34.         Node* current = head;
  35.         while (current != nullptr) {
  36.             std::cout << current->data << " -> ";
  37.             current = current->next;
  38.         }
  39.         std::cout << "nullptr" << std::endl;
  40.     }
  41.     void findCommonElements(LinkedList& otherList) {
  42.         Node* current1 = head;
  43.  
  44.         while (current1 != nullptr) {
  45.             int element = current1->data;
  46.             Node* current2 = otherList.head;
  47.  
  48.             while (current2 != nullptr) {
  49.                 if (element == current2->data) {
  50.                     std::cout << "Common Element: " << element << std::endl;
  51.                 }
  52.                 current2 = current2->next;
  53.             }
  54.  
  55.             current1 = current1->next;
  56.         }
  57.     }
  58. };
  59. int main()
  60. {
  61.     LinkedList myList1, myList2;
  62.     int n;
  63.     std::cout << "Enter the number of values to insert: ";
  64.     std::cin >> n;
  65.     std::cout << "List 1\n";
  66.     for (int i = 0; i < n; i++) {
  67.         int value;
  68.         std::cout << "Enter a value: ";
  69.         std::cin >> value;
  70.         myList1.insert(value); //Call the insert function
  71.     }
  72.     std::cout << "List 2\n";
  73.     for (int i = 0; i < n; i++) {
  74.         int value;
  75.         std::cout << "Enter a value: ";
  76.         std::cin >> value;
  77.         myList2.insert(value); //Call the insert function
  78.     }
  79.     for (int i = 0; i < n; i++) {
  80.        
  81.     }
  82.     std::cout << "List 1: ";
  83.     myList1.display();   // Call the display function
  84.     std::cout << "List 2: ";
  85.     myList2.display();
  86.     std::cout << "Common elements between the two lists: " << std::endl;
  87.     myList1.findCommonElements(myList2);
  88. }
Advertisement
Add Comment
Please, Sign In to add comment