-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.cpp
56 lines (54 loc) · 802 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "Node.h"
#include <iostream>
template<typename T>
class Stack {
public:
Stack(){
head = NULL;
length = 0;
}
~Stack(){
makeEmpty();
}
void push(const T data){
Node<T>* p = new Node<T>(data);
p->next = head;
head = p;
length++;
}
void pop(){
if(head == NULL)
return;
Node<T>* p = head;
head = head->next;
delete p;
length--;
}
T top(){
if(head == NULL)
return NULL;
return head->data;
}
void makeEmpty(){
Node<T>* p = head;
Node<T>* q = head;
while(p != NULL){
p = p->next;
delete q;
q = p;
}
head = NULL;
}
bool isEmpty(){return head == NULL;}
void print(){
Node<T>* it = head;
while(it != NULL){
std::cout << it->data << " ";
it = it->next;
}
std::cout << std::endl;
}
private:
Node<T>* head;
int length;
};