-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack.cpp
137 lines (135 loc) · 2.06 KB
/
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
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
#include<iostream>
#include<vector>
using namespace std;
struct node{
node *next;
int data;
node(){
next = NULL;
cout << "what";
}
node(int X){
data = X;
next = NULL;
}
};
struct Stack{
node *head;
Stack(){
head = NULL;
}
void push(node *x){
node *tmp;
if(head == NULL){
head = x;
}else{
tmp = head;
head = x;
head -> next = tmp;
}
}
void pop(){
head = head -> next;
}
int top(){
return head->data;
}
void printStack(){
node *tmp;
tmp = head;
while(tmp != NULL){
cout << tmp -> data << " ";
tmp = tmp -> next;
}
cout << endl;
}
int getSize(){
int ans = 0;
node *tmp;
tmp = head;
while(tmp != NULL){
ans ++;
tmp = tmp -> next;
}
return ans;
}
int isEmpty(){
return head == NULL;
}
};
struct StackSet{
int maximum;
int insertIn;
vector<Stack>myset;
StackSet(){
Stack st;
myset.push_back(st);
insertIn = 0;
}
StackSet(int f){
Stack st;
myset.push_back(st);
f = maximum;
insertIn = 0;
}
int sizeOfStack(int idx){
return myset[idx].getSize();
}
void push(int idx){
if(sizeOfStack(insertIn) == maximum){
insertIn++;
Stack st;
myset.push_back(st);
}
myset[insertIn].push(new node(idx));
}
void pop(){
if(sizeOfStack(insertIn) == 0){
if(insertIn == 0){
cout << "VACIA" << endl;
}else{
insertIn--;
myset[insertIn].pop();
}
}
}
};
Stack sortStack(Stack toSort){
Stack ans;
while(toSort.getSize() > 0){
int u = toSort.top(), v;
toSort.pop();
while(ans.getSize() > 0 and (v = ans.top()) > u){
toSort.push( new node(v));
ans.pop();
}
ans.push(new node(u));
}
return ans;
}
int main(){
Stack st;
node *a = new node(90);
node *b = new node(-8);
node *c = new node(7);
node *d = new node(-6);
node *e = new node(5);
node *f = new node(14);
node *g = new node(-3);
node *h = new node(2);
node *i = new node(1);
node *j = new node(-8);
st.push(a);
st.push(b);
st.push(c);
st.push(d);
st.push(e);
st.push(f);
st.push(g);
st.push(h);
st.push(i);
st.push(j);
Stack ordered = sortStack(st);
ordered.printStack();
return 0;
}