Advertisement
jasonpogi1669

Sort Linked List with Memory Address and File Handling using C++

May 8th, 2021
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <utility>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8. struct Node {
  9.   // create a typical node for a doubly linked list
  10.   // extra value -> 'address', this will store the
  11.   // memory address of the current value
  12.   int value;
  13.   int *address;
  14.   Node *next;
  15.   Node *prev;
  16. } *head;
  17.  
  18. void CreateList(int a[], int n) {
  19.   // 'new_node' = for creating a new node
  20.   // 'traverse_node' = for traversing the linked list
  21.   Node *new_node, *traverse_node;
  22.   for (int i = 0; i < n; i++) {
  23.     // set the value of the node based on the current element
  24.     new_node = new Node();
  25.     new_node->value = a[i];
  26.     new_node->address = &a[i];
  27.     new_node->next = NULL;
  28.     // set the 'traverse_node' to 'head'
  29.     traverse_node = head;
  30.     if (head == NULL) {
  31.       // if 'head' is null, then set the previous pointer to null
  32.       new_node->prev = NULL;
  33.       // set 'head' to 'new_node'
  34.       head = new_node;
  35.     } else {
  36.       // otherwise, traverse the linked list to find the last element
  37.       while (traverse_node->next != NULL) {
  38.         traverse_node = traverse_node->next;
  39.       }
  40.       // set the next pointer of the last element to 'new_node'
  41.       traverse_node->next = new_node;
  42.       // set the previous pointer of 'new_node' to 'traverse_node'
  43.       new_node->prev = traverse_node;
  44.     }
  45.   }
  46. }
  47.  
  48. void BubbleSort() {
  49.   // create 'checker' to control the loop below
  50.   bool checker = false;
  51.   // create two pointers:
  52.   // 'pointer' = traverse the nodes in the linked list
  53.   Node *pointer;
  54.   // 'last_pointer' = point to null or last node
  55.   Node *last_pointer = NULL;
  56.   if (head == NULL) {
  57.     // if 'head' is null, then return nothing
  58.     return;
  59.   } else {
  60.     // create a loop that checks if 'checker' is true
  61.     do {
  62.       // set 'checker' to false
  63.       checker = false;
  64.       // set 'pointer' to head (first node)
  65.       pointer = head;
  66.       // create a loop until 'pointer' reaches 'last_pointer'
  67.       while (pointer->next != last_pointer) {
  68.         // if the currrent node is greater than the next node,
  69.         // then it means it's not yet sorted
  70.         if (pointer->value > pointer->next->value) {
  71.           // create pairs of integer and integer pointer to store the
  72.           // value and address of the current node as well as the next node
  73.           pair<int, int*> a;
  74.           a.first = pointer->value;
  75.           a.second = pointer->address;
  76.           pair<int, int*> b;
  77.           b.first = pointer->next->value;
  78.           b.second = pointer->next->address;
  79.           // swap the two pairs
  80.           swap(a, b);
  81.           // store back to the 'pointer' the values of pair 'a'
  82.           pointer->value = a.first;
  83.           pointer->address = a.second;
  84.           // store back to the 'pointer->next' the value of pair 'b'
  85.           pointer->next->value = b.first;
  86.           pointer->next->address = b.second;
  87.           // set 'checker' to true
  88.           checker = true;
  89.         }
  90.         // set 'pointer' to the next node
  91.         pointer = pointer->next;
  92.       }
  93.       // set 'last_pointer' to the last node
  94.       last_pointer = pointer;
  95.     } while (checker);
  96.   }
  97. }
  98.  
  99. void PrintList(int option) {
  100.   Node *traverse_node;
  101.   traverse_node = head;
  102.   if (head == NULL) {
  103.     return;
  104.   } else {
  105.     ofstream output_file;
  106.     // there will be two options:
  107.     // 1 - the linked list hasn't been sorted yet
  108.     // 2 - the linked list has been sorted
  109.     if (option == 1) {
  110.       output_file.open("output.txt");
  111.       output_file << "Output Data File:\n";
  112.     } else {
  113.       // use append in this option to not overwrite the former contents
  114.       output_file.open("output.txt", ios_base::app);
  115.     }
  116.     // print the values of the nodes based on the pattern given
  117.     // TERMINAL
  118.     cout << setw(10) << "\nADDRESS";
  119.     cout << setw(20) << "PREV";
  120.     cout << setw(20) << "DATA";
  121.     cout << setw(20) << "NEXT\n";
  122.     // IN FILE
  123.     output_file << setw(10) << "\nADDRESS";
  124.     output_file << setw(20) << "PREV";
  125.     output_file << setw(20) << "DATA";
  126.     output_file << setw(20) << "NEXT\n";
  127.     while (traverse_node != NULL) {
  128.       // TERMINAL
  129.       cout << setw(5) << traverse_node->address;
  130.       // INFILE
  131.       output_file << setw(5) << traverse_node->address;
  132.       if (traverse_node->prev != NULL) {
  133.         // this condition works if the current node is not the first node
  134.         // TERMINAL
  135.         cout << setw(15) << traverse_node->prev->address;
  136.         // IN FILE
  137.         output_file << setw(15) << traverse_node->prev->address;
  138.       } else {
  139.         // if the current node is the first node, then this block is executed
  140.         // TERMINAL
  141.         cout << setw(15) << "0x0";
  142.         // IN FILE
  143.         output_file << setw(15) << "0x0";
  144.       }
  145.       // print the value of the current node
  146.       // TERMINAL
  147.       cout << setw(20) << traverse_node->value;
  148.       // IN FILE
  149.       output_file << setw(20) << traverse_node->value;
  150.       if (traverse_node->next != NULL) {
  151.         // this condition works if the current node is not the last node
  152.         // TERMINAL
  153.         cout << setw(20) << traverse_node->next->address;
  154.         // IN FILE
  155.         output_file << setw(20) << traverse_node->next->address;
  156.       } else {
  157.         // if the current node is the last node, then this block is executed
  158.         // TERMINAL
  159.         cout << setw(20) << "0x0";
  160.         // IN FILE
  161.         output_file << setw(20) << "0x0";
  162.       }
  163.       cout << '\n';
  164.       output_file << '\n';
  165.       // set the 'traverse_node' to the next node
  166.       traverse_node = traverse_node->next;
  167.     }
  168.     // close the file
  169.     output_file.close();
  170.   }
  171. }
  172.  
  173. int main() {
  174.   // user input
  175.   cout << "Enter the number of elements of the array: ";
  176.   int n;
  177.   cin >> n;
  178.   cout << "Enter " << n << " elements: ";
  179.   // set 'head' pointer to null
  180.   head = NULL;
  181.   int a[n];
  182.   for (int i = 0; i < n; i++) {
  183.     // store the elements in an array
  184.     cin >> a[i];
  185.   }
  186.   // CreateList - create a linked list
  187.   CreateList(a, n);
  188.   // PrintList(1) - print the linked list (before sorting)
  189.   PrintList(1);
  190.   // BubbleSort - sort the linked list using bubble sort algorithm
  191.   BubbleSort();
  192.   // PrintList(2) - print the linked list (after sorting)
  193.   PrintList(2);
  194.   return 0;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement