-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.cpp
40 lines (35 loc) · 937 Bytes
/
stack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include"stack.h"
#include<iostream>
using namespace std;
Stack::Stack() {
top = NULL; //Make sure the top of stack pointer points to NULL at construction.
//cout << "Stack Initialized" << endl;
}
Stack::~Stack() {
int dummyOne, dummyTwo;
while (!isEmpty())
pop(dummyOne, dummyTwo); //While stack is not empty, pop until it is.
}
void Stack::push(int x, int y) {
node* temp = new node;
temp->x = x; //Data written to new node
temp->y = y;
temp->next = top; //new node points to old top of stack.
top = temp; //point top to the new top of stack.
}
void Stack::pop(int& x, int& y) {
if (!isEmpty()) {
x = top->x;
y = top->y;
node* toDelete = top;
top = toDelete->next;
delete toDelete;
}
else {
//Do nothing
//cout << "Nothing to pop from stack" << endl;
}
}
bool Stack::isEmpty() {
return(top == NULL); //If the top pointer is NULL, then we return true.
}