Idanref

Node && Queue

Mar 4th, 2021
861
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ---------- Node.h -------------
  2.  
  3. #include <iostream>
  4. using namespace std;
  5. #pragma once
  6.  
  7. // Node.h
  8. class Node {
  9. public:
  10.     Node();
  11.     Node(string, double = 0);
  12.  
  13.    
  14.  
  15.     void set_name(string name) { _name = name; }
  16.     void set_money(double money) { _money = money; }
  17.     void reset_node();
  18.  
  19.     string get_name() { return _name; }
  20.     double get_money() { return _money; }
  21.     int get_num() { return _num; }
  22.  
  23.     void print_node();
  24.  
  25. private:
  26.     string _name;
  27.     double _money;
  28.     int _num;
  29. };
  30.  
  31.  
  32.  
  33. // ---------- Queue.h -------------
  34.  
  35. #include <iostream>
  36. using namespace std;
  37.  
  38. #pragma once
  39. #include "Node.h"
  40.  
  41. // Queue.h
  42.     // init -- initializes a new empty queue -- creates a stack
  43.     // addnode -- gets a node (client) and adds to the last index of the queue
  44.     //getnode -- returns and removes the top object from the queue.
  45.     //top node -- view inside the queue. returns the top node of the queue but the object remains
  46.     //is empty  - true if queue is empty
  47.  
  48. class Queue {
  49. public:
  50.     Queue(int = 5);
  51.     Queue();
  52.  
  53.     //void init(int);
  54.     void add_node(Node);
  55.     Node get_node();
  56.     Node top_node() { return _queue_arr[0]; }
  57.     bool is_empty() { return _counter == 0 ? 1 : 0; }
  58.     void print_queue();
  59.  
  60.     //// Getters
  61.     //int get_counter();
  62.     //int get_id();
  63.  
  64. private:
  65.     int _counter;
  66.     int _id;
  67.     int _size;
  68.     Node* _queue_arr;
  69.  
  70. };
  71.  
  72.  
  73. // ---------- Node.cpp -------------
  74.  
  75.  
  76. #include <iostream>
  77. using namespace std;
  78.  
  79. #include "Node.h"
  80.  
  81. // Class Function Creation - Node.cpp
  82.  
  83. static int account_num = 1;
  84.  
  85. Node::Node(string name1, double money1)
  86. {
  87.     _name = name1;
  88.     _money = money1;
  89.     _num = account_num++;
  90. }
  91.  
  92. Node::Node()
  93. {
  94.     _money = 0;
  95.     _name = "";
  96.     _num = 0;
  97. }
  98.  
  99. void Node::print_node()
  100. {
  101.     cout << "Name: " << _name << endl;
  102.     cout << "Money: " << _money << endl;
  103.     cout << "Accout Number: " << _num << endl;
  104.     cout << endl;
  105. }
  106.  
  107. void Node::reset_node()
  108. {
  109.     _money = 0;
  110.     _name = "";
  111.     _num = 0;
  112. }
  113.  
  114.  
  115.  
  116. // ---------- Queue.cpp -------------
  117.  
  118. #include <iostream>
  119. using namespace std;
  120.  
  121. #include "Queue.h"
  122. #include "Node.h"
  123.  
  124. // Queue
  125.  
  126. int id = 1;
  127.  
  128. Queue::Queue(int size)
  129. {
  130.     _counter = 0;
  131.     _id = id++;
  132.     _size = size;
  133.     _queue_arr = new Node[size];
  134. }
  135.  
  136. Queue::Queue()
  137. {
  138.     _counter = 0;
  139.     _size = 0;
  140.     _id = 0;
  141. }
  142.  
  143. void Queue::add_node(Node n1)
  144. {
  145.     if (_counter < _size) {
  146.         _queue_arr[_counter] = n1;
  147.         _counter++;
  148.     }
  149.  
  150.     else {
  151.         cout << endl << "Queue is full!" << endl; // TO FIX
  152.     }
  153. }
  154.  
  155. Node Queue::get_node()
  156. {
  157.     Node temp = _queue_arr[0];
  158.  
  159.     for (int i = 0; i < _size - 1; i++)
  160.     {
  161.         _queue_arr[i] = _queue_arr[i + 1];
  162.     }
  163.  
  164.     _counter--;
  165.     _queue_arr[_size - 1].~Node();
  166.     cout << endl << "Node (" << _id << "): Top-level node removed." << endl << endl << endl;
  167.  
  168.     return temp;
  169.  
  170. }
  171.  
  172. void Queue::print_queue()
  173. {
  174.     cout << "------ (Queue " << _id << ") ------" << endl << endl;
  175.     for (int i = 0; i < _counter; i++)
  176.     {
  177.         _queue_arr[i].print_node();
  178.     }
  179.  
  180.     if (Queue::is_empty())
  181.         cout << "- Queue " << _id << " is empty -" << endl << endl;
  182.     else
  183.         cout << "Queue " << _id << " Generated Successfully." << endl;
  184. }
  185.  
  186. // add init
  187.  
  188.  
  189. // ---------- Source.cpp -------------
  190.  
  191. #include <iostream>
  192. using namespace std;
  193.  
  194. #include "Queue.h"
  195. #include "Node.h"
  196.  
  197. // Source.cpp
  198. int main()
  199. {
  200.     int size;
  201.     cout << "Enter queue size: " << endl;
  202.     cin >> size;
  203.  
  204.     Queue q1(size);
  205.     Queue q2(4);
  206.     Node n1("I", 10000);
  207.     Node n2("L", 999);
  208.     Node n3("N", 1000);
  209.  
  210.     q1.add_node(n1);
  211.     q1.add_node(n2);
  212.     q1.print_queue();
  213.  
  214.     cout << endl << endl;
  215.  
  216.    
  217.    
  218.     q2.add_node(n3);
  219.     q2.print_queue();
  220.  
  221.     q2.get_node();
  222.  
  223.     q2.print_queue();
  224.  
  225.  
  226.     return 0;
  227. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×