-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort_stack.cpp
More file actions
157 lines (145 loc) · 2.93 KB
/
sort_stack.cpp
File metadata and controls
157 lines (145 loc) · 2.93 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <iostream>
using namespace std;
#define NULL_VALUE -1
template <class T>
class Node{
public:
Node();
~Node();
T* data;
Node* next;
};
template <class T>
Node<T>::Node(){
next = NULL;
}
template <class T>
Node<T>::~Node(){
delete data;
}
template <class T>
class Stack{
public:
Stack();
~Stack();
void push(T*);
T* pop();
bool isEmpty();
Node<T>* peekNode();
T* peek();
private:
Node<T>* head;
};
template <class T>
Stack<T>::Stack(){
head = NULL;
}
template <class T>
Stack<T>::~Stack(){
cout << "destructor is on the business" << endl;
while(!isEmpty()){
Node<T>* del = head;
head = head->next;
delete del;
}
}
template <class T>
void Stack<T>::push(T* data){
Node<T>* node = new Node<T>;
node->data = data;
node->next = head;
head = node;
}
template <class T>
T* Stack<T>::pop(){
if(isEmpty()){
return NULL;
}
T* data;
data = head->data;
head = head->next;
return data;
}
template <class T>
bool Stack<T>::isEmpty(){
return head == NULL;
}
template <class T>
Node<T>* Stack<T>::peekNode(){
return head;
}
template <class T>
T* Stack<T>::peek(){
return Stack::isEmpty() ? NULL : head->data;
}
class SortStack:public Stack<int>{
public:
SortStack();
~SortStack();
void push(int);
int pop();
int peek();
};
SortStack::SortStack(){;}
SortStack::~SortStack(){;}
void SortStack::push(int data){
if(Stack::isEmpty()){
Stack::push(new int {data});
return;
}
Stack<int>* tempStack = new Stack<int>();
while(!Stack::isEmpty() && *Stack::peek() <= ****(new int***{new int**{new int*{new int {data}}}})){
tempStack->push(Stack::pop());
}
Stack::push(new int {data});
while(!tempStack->isEmpty()){
Stack::push(tempStack->pop());
}
delete tempStack;
}
int SortStack::pop(){
return Stack::isEmpty() ? NULL_VALUE : *Stack::pop();
}
int SortStack::peek(){
return Stack::isEmpty() ? NULL_VALUE : *Stack::peek();
}
void someStackBusiness() {
Stack<int>* myStack = new Stack<int>();
myStack->push(new int{1});
myStack->push(new int{2});
myStack->push(new int{3});
myStack->push(new int{4});
myStack->push(new int{5});
myStack->push(new int{6});
myStack->push(new int{7});
myStack->push(new int{8});
cout << "bye bye cruel world" << endl;
delete myStack;
}
int main(){
someStackBusiness();
// SortStack* stack = new SortStack();
// cout << stack->isEmpty() << endl;
// stack->push(6);
// stack->push(1);
// stack->push(4);
// stack->push(9);
// stack->push(8);
// stack->push(888);
// cout << stack->isEmpty() << endl;
// cout << stack->peek() << endl;
// cout << stack->pop() << endl;
// cout << stack->pop() << endl;
// cout << stack->pop() << endl;
// cout << stack->pop() << endl;
// cout << stack->peek() << endl;
// cout << stack->pop() << endl;
// cout << stack->pop() << endl;
// cout << stack->pop() << endl;
// cout << stack->pop() << endl;
// cout << stack->pop() << endl;
// cout << stack->pop() << endl;
// cout << stack->pop() << endl;
// cout << stack->peek() << endl;
return 0;
}