-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstacktype.cpp
More file actions
103 lines (88 loc) · 1.66 KB
/
stacktype.cpp
File metadata and controls
103 lines (88 loc) · 1.66 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
#include "stackType.h"
Stacktype::Stacktype()
{
top = -1;
}
void Stacktype::Push(int n)
{
if (isFull())
{
cout << "Error. Stack is full." << endl;
return;
}
top++;
arr[top] = n;
}
void Stacktype::Pop()
{
if (isEmpty())
{
cout << "Error. Stack is empty." << endl;
return;
}
top--;
}
int Stacktype::Top()
{
return arr[top];
}
bool Stacktype::isEmpty() {
if(top < 0)
{
return true;
}
return false;
}
bool Stacktype::isFull() {
if(top >= 20)
{
return true;
}
return false;
}
void Stacktype::Print() {
Stacktype stack2;
while(!isEmpty()) {
cout << Top() << " " ;
stack2.Push(Top());
Pop();
}
cout << endl;
while(!stack2.isEmpty()) {
Push(stack2.Top());
stack2.Pop();
}
}
void Stacktype::sort() {
Stacktype stack2, stack3;
stack3.Push(Top());
Pop();
while(!isEmpty()) {
if(Top() < stack3.Top()) {
stack3.Push(Top());
Pop();
} else {
while(stack3.Top() < Top()) {
stack2.Push(stack3.Top());
stack3.Pop();
if(stack3.isEmpty()) {
break;
}
}
stack3.Push(Top());
Pop();
while(!stack2.isEmpty()) {
stack3.Push(stack2.Top());
stack2.Pop();
}
}
}
while(!stack3.isEmpty()) {
stack2.Push(stack3.Top());
stack3.Pop();
}
while(!stack2.isEmpty()) {
Push(stack2.Top());
stack2.Pop();
}
}