Advertisement
Guest User

Untitled

a guest
Jun 25th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. #include <iostream>
  2. #include "Node.h"
  3. #include "Stack.h"
  4.  
  5. //acts like a queue
  6.  
  7.  
  8. using namespace std;
  9.  
  10. Stack::Stack() : size(0){
  11.  
  12. }
  13.  
  14. void Stack::push(int x){
  15.  
  16.     Node* temp = new Node();
  17.     temp->data = x;
  18.  
  19.  
  20.     if(size == 0){     
  21.         head = temp;
  22.         tail = temp;
  23.         size++;
  24.     }
  25.     else {
  26.         size++;
  27.     }
  28.  
  29.     //assigning new element as the "new" tail
  30.     tail -> next = temp;
  31.     tail = temp;
  32.     tail -> next = NULL;
  33. }
  34.  
  35.  
  36. int Stack::pop(){
  37.  
  38.     int result = 0;
  39.  
  40.     if (size == 1)  {
  41.         result = head -> data;
  42.         delete head;
  43.         head = NULL;
  44.         size--;
  45.     }  
  46.  
  47.     if (size > 1)  {
  48.         Node* temp = head;
  49.         result = head -> data;
  50.         delete head;
  51.         size--;
  52.         head = temp->next;
  53.     }
  54.     return result;
  55. }
  56.  
  57. int Stack::getSize()  {
  58.     return size;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement