Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "Node.h"
- #include "Stack.h"
- //acts like a queue
- using namespace std;
- Stack::Stack() : size(0){
- }
- void Stack::push(int x){
- Node* temp = new Node();
- temp->data = x;
- if(size == 0){
- head = temp;
- tail = temp;
- size++;
- }
- else {
- size++;
- }
- //assigning new element as the "new" tail
- tail -> next = temp;
- tail = temp;
- tail -> next = NULL;
- }
- int Stack::pop(){
- int result = 0;
- if (size == 1) {
- result = head -> data;
- delete head;
- head = NULL;
- size--;
- }
- if (size > 1) {
- Node* temp = head;
- result = head -> data;
- delete head;
- size--;
- head = temp->next;
- }
- return result;
- }
- int Stack::getSize() {
- return size;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement