-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStack.h
69 lines (63 loc) · 1.67 KB
/
Stack.h
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
57
58
59
60
61
62
63
64
65
66
67
68
69
//
// Created by ge yao on 2017/7/31.
// Stack, based on double linked list
//
#ifndef LINEARLIST_STACK_H
#define LINEARLIST_STACK_H
#include <iostream>
#include "DoubleLinkedLinearList.h"
using namespace std;
template <class T>
class Stack{
private:
DoubleLinkedLinearList<T>* list = new DoubleLinkedLinearList<T>();
protected:
public:
virtual bool isEmpty(){
if (list == nullptr){
list = new DoubleLinkedLinearList<T>();
return true;
} else{
return list->empty();
}
}
virtual int size(){
if (list == nullptr){
list = new DoubleLinkedLinearList<T>();
return 0;
} else{
return list->getSize();
}
}
//push a node to the tail of list
virtual bool push(T & data){
if (list == nullptr){
list = new DoubleLinkedLinearList<T>();
}
if (data == (T)NULL){
return false;
}
DNode<T>* node = new DNode<T>();
node->setDataNode(data);
list->add(node);
return true;
}
//Get the tail of the list, and remove it from list
//Notice: If you want to return an element whose type is reference, you need a reference parameter
virtual T& pop(T& result){
if (isEmpty()){
result = (T)NULL;
return result;
} else{
result = list->getTailNode()->getDataNode();
list->remove(list->getTailNode());
return result;
}
}
virtual void info(){
cout << "isEmpty ? " << isEmpty() << endl;
cout << "size() ? " << size() << endl;
list->info();
}
};
#endif //LINEARLIST_STACK_H