-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyStack.cpp
More file actions
127 lines (125 loc) · 3.25 KB
/
MyStack.cpp
File metadata and controls
127 lines (125 loc) · 3.25 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
#include<iostream>
#include<string>
using namespace std;
template<class T>
class node{
template<typename U>
friend class stack;
private:
node* next;
T data;
public:
node<T> *GetNext(){
return next;
}
T GetData(){
return data;
}
void SetNext(node<T> *a){
next=a;
}
void SetData(T a){
data=a;
}
};
template<typename A>
class stack{
private:
node<A>* top;
node<A>* current;
int count;
public:
stack<A>(){
top=NULL;
current=NULL;
count=0;
}
void insert(A value){
node<A>* temp=new node<A>;
temp->SetData(value);
if(top==NULL){
temp->SetNext(NULL);
top=temp;
}
else{
temp->SetNext(top);
top=temp;
}
count++;
}
void del(){
current=top->next;
delete top;
top=current;
count--;
}
bool IsEmpty(){
if(top==NULL)
return true;
else
return false;
}
void print(){
if(IsEmpty())
cout<<"";
else{
current=top;
cout<<"|";
while(current->next!=NULL){
cout<<current->data<<"|"<<endl<<"|";
current=current->next;
}
cout<<current->data<<"|"<<endl;
}
}
int size(){
return count;
}
A gettop(){
return top->GetData();
}
void Display_Options(){
abc:
cout<<"Select from the following options:\n1) Insert data.\n2) Print Stack\n3) Delete Data\n4) Quit"<<endl;
int option;
cin>>option;
while(option!=4){
switch (option){
case 1:{
A num;
char op='y';
while(true){
cout<<"Enter Data into the Stack:\n";
cin>>num;
insert(num);
print();
cout<<"\n:";
cout<<endl<<"Continue?(Y/N)?: ";
cin>>op;
if(op=='N'||op=='n')
break;
}
};
break;
case 2:
if(!IsEmpty()){
cout<<"-----------"<<endl;
print();
cout<<"-----------"<<endl;
}
else
cout<<"The Stack is Empty Broooooooo!"<<endl;
break;
case 3:
if(!IsEmpty())
del();
else
cout<<"The Stack is Empty Brooooooo!"<<endl;
break;
default:
cout<< "Your selection must be between 1 and 4!\n";
}
goto abc;
}
}
};