ptmdmusique

CLB_CodingChallenge_6_5_2018

May 6th, 2018
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | None | 0 0
  1. /*Problem description
  2.     Given N natural number(s), separate 0's and bring them to the left side of the containers.
  3.     Time Complexity  : O(n)
  4.     Memory Complexity: O(1)
  5. */
  6. #include <iostream>
  7. using namespace std;
  8.  
  9. struct node {
  10.     int data;
  11.     node* nextNode = NULL;
  12. };
  13.  
  14. class LinkedList {
  15. public:
  16.     ~LinkedList();
  17.  
  18.     void addNode(int);              //Used to add more node
  19.     void shiftZero();               //Wrapper function
  20.     void display();                 //Used to display the whole list
  21.  
  22. private:
  23.     node * head = NULL;
  24.     node * tail = NULL;
  25.     int size = 0;
  26.  
  27.     void shiftZero(node*&);         //Main Shifter - Recursively called to shift all the zeros to the beginning of the list
  28. };
  29.  
  30. int main() {
  31.     LinkedList myList;
  32.     int numOfNum = 0;
  33.     cout << "Please enter number of numbers: ";
  34.     cin >> numOfNum;
  35.  
  36.     cout << "Please enter " << numOfNum << " numbers: ";
  37.     //Take the input and display the list
  38.     for (int indx = 0; indx < numOfNum; indx++) {
  39.         int temp;
  40.         cin >> temp;
  41.         myList.addNode(temp);
  42.     }
  43.     cout << endl;
  44.     myList.display();
  45.  
  46.     //Shift zeros then display again
  47.     myList.shiftZero();
  48.     myList.display();
  49.  
  50.     return 0;
  51. }
  52.  
  53. LinkedList::~LinkedList() {
  54.     //Clean up after you play!
  55.     node* current = head;
  56.     while (current != NULL) {
  57.         node* delNode = current;
  58.         current = current->nextNode;
  59.         delete delNode;
  60.     }
  61. }
  62.  
  63. void LinkedList::addNode(int data) {
  64.     //Append into the list
  65.     if (head == NULL) {
  66.         //Empty list
  67.         head = new node;
  68.         head->data = data;
  69.         tail = head;
  70.         size++;
  71.         return;
  72.     }
  73.  
  74.     //Create another node
  75.     node* temp = new node;
  76.     temp->data = data;
  77.     //Move the tail of the list forward
  78.     tail->nextNode = temp;
  79.     tail = temp;
  80.     //Increase the size
  81.     size++;
  82. }
  83.  
  84. void LinkedList::shiftZero() {
  85.     if (head == NULL) {
  86.         //Empty list!
  87.         cout << "List is empty!" << endl;
  88.         return;
  89.     }
  90.  
  91.     cout << "0's Shifted!" << endl;
  92.     shiftZero(head);
  93. }
  94.  
  95. void LinkedList::shiftZero(node*& curNode) {
  96.     if (curNode == NULL) {
  97.         //End condition
  98.         return;
  99.     }
  100.  
  101.     //Use double pointer technique to dynamically change the location of the node
  102.     //  without accesing its parent!
  103.  
  104.     //Traverse first
  105.     shiftZero(curNode->nextNode);
  106.  
  107.     //Then shift
  108.     if (curNode->data == 0) {
  109.         //"temp" points to the memory location itself, not to the argument pointer (which is parent->nextNode) to that location
  110.         node* temp = curNode;
  111.         //Move the argument pointer to the next location
  112.         curNode = curNode->nextNode;
  113.         //Prepend the current 0 node
  114.         temp->nextNode = head;
  115.         head = temp;
  116.     }
  117.  
  118.     //Will traverse back to the previous stack farm after this
  119. }
  120.  
  121. void LinkedList::display() {
  122.     if (head == NULL) {
  123.         //Empty list
  124.         cout << "Your list is empty!" << endl;
  125.         return;
  126.     }
  127.  
  128.     node* current = head;
  129.     cout << "Here is your list:\n\t";
  130.     while (current != NULL) {
  131.         cout << current->data << " ";
  132.         current = current->nextNode;
  133.     }
  134.     cout << endl;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment