gon2

Untitled

Feb 12th, 2018
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.41 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Stack {
  6.   private:
  7.     class Node { // hnútur sem geymir heiltölugögn og vísun í næsta hnút
  8.       public:
  9.         int data;
  10.         Node* next;
  11.  
  12.         Node(int data, Node* next) {
  13.           this->data = data;
  14.           this->next = next;
  15.         }
  16.     };
  17.  
  18.     Node* head;
  19.     int lengd;
  20.  
  21.    public:
  22.     Stack() { // smiður fyrir tóman stack
  23.       this->head = nullptr;
  24.       this->lengd = 0;
  25.     }
  26.  
  27.     ~Stack() { delete this->head; } // eyðum halanum
  28.  
  29.     void push(int n) { // búum til hnút með gögnunum og setjum hann sem hausinn:
  30.       Node* p = new Node(n, this->head);
  31.       this->head = p;
  32.       this->lengd++;
  33.     }
  34.  
  35.     int pop() { // skilum efsta gildinu og eyðum því:
  36.         if (this->head == nullptr) {
  37.             throw underflow_error("Reynt var að fjarlægja efsta stak af tómum hlaða");
  38.  
  39.         } else {
  40.  
  41.           int skilagildi = this->head->data;
  42.           // stillum næsta stak sem haus og eyðum hausnum:
  43.           Node* temp = this->head;
  44.           this->head = this->head->next;
  45.           delete temp;
  46.  
  47.           this->lengd--;
  48.           return skilagildi;
  49.         }
  50.     }
  51.  
  52.     int peek() { // skilum efsta gildinu, eyðum því ekki:
  53.         if (this->head == nullptr) {
  54.             throw underflow_error("Reynt var að kíkja á efsta stak tóms hlaða");
  55.         } else return this->head->data;
  56.     }
  57.  
  58.     bool isEmpty() { // skoðum hvort hlaðinn sé tómur:
  59.       if (this->head == nullptr) return true;
  60.       else return false;
  61.     }
  62. };
  63.  
  64. int main() {
  65.     Stack s;
  66.     cout << "Setjum tölurnar 1, 2 og 4 á hlaðann s." << endl;
  67.     s.push(1);
  68.     s.push(2);
  69.     s.push(4);
  70.     cout << "Stelum efsta stakinu, " << s.pop() << ", af s." << endl;
  71.     cout << "Setjum tölurnar 8, 16 og 32 á s." << endl;
  72.     s.push(8);
  73.     s.push(16);
  74.     s.push(32);
  75.  
  76.     cout << "Kíkjum á efsta stak s, það er " << s.peek() << "." << endl;
  77.  
  78.     cout << "Fjarlægjum nú tölurnar af s: " << endl;
  79.     while (!s.isEmpty()) {
  80.         cout << s.pop() << " ";
  81.     }
  82.     cout << endl;
  83.  
  84.     try {
  85.         s.peek();
  86.     } catch (underflow_error e) {
  87.         cout << "Ekki tókst að kíkja á s, enda tómur" << endl;
  88.     }
  89.  
  90.     try {
  91.         s.pop();
  92.     } catch (underflow_error e) {
  93.         cout << "Ekki tókst að fjarlægja stak af s, enda tómur" << endl;
  94.     }
  95.  
  96.     return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment