我是靠谱客的博主 追寻鸵鸟,这篇文章主要介绍数据结构7-两栈共享空间,现在分享给大家,希望可以做个参考。

复制代码
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
158
159
160
161
162
163
164
165
166
167
168
169
#include <iostream> using namespace std; const int MAXSIZE = 100; struct People { int id; string name; int age; }; /*实现两栈共享存储空间的构造,析构,入栈,出栈, 取栈顶元素,判断栈空,菜单*/ template <class Element> class DoubleSeqStack { public: DoubleSeqStack(); ~DoubleSeqStack() {}; void push(Element x,int num); Element pop(int num); Element getTop(int num); bool ifEmpty(int num); void menu(); private: Element DoubleSStack[MAXSIZE]; int top1,top2; }; template <class Element> DoubleSeqStack<Element>::DoubleSeqStack() { top1 = -1; top2 = MAXSIZE; } template <class Element> void DoubleSeqStack<Element>::push(Element x,int num) { if (top1 == top2 - 1) throw"上溢"; if (num == 1) { DoubleSStack[++top1] = x; } else if (num == 2) { DoubleSStack[--top2] = x; } } template <class Element> Element DoubleSeqStack<Element>::pop(int num) { Element x; if (num == 1) { if (top1 == -1)throw "下溢"; x = DoubleSStack[top1--]; } else if (num == 2) { if (top2 == MAXSIZE)throw "下溢"; x = DoubleSStack[top2++]; } return x; } template <class Element> Element DoubleSeqStack<Element>::getTop(int num) { Element x; if (num == 1) { if (top1 == -1)throw "此栈为空"; x = DoubleSStack[top1]; } else if (num == 2) { if (top2 == MAXSIZE)throw "此栈为空"; x = DoubleSStack[top2]; } return x; } template <class Element> bool DoubleSeqStack<Element>::ifEmpty(int num) { if (num == 1) { if (top1 == -1)return 1; else return 0; } else if (num == 2) { if (top2 == MAXSIZE)return 1; else return 0; } } template <class Element> void DoubleSeqStack<Element>::menu() { int choice=0; int pId = 0; string pName; int pAge = 0; int pos = 0; People p1 = { pId, pName, pAge }; Element x; int num = 0; /*实现顺序栈的构造,析构,入栈,出栈, 取栈顶元素,判断栈空,菜单*/ cout <<"-----菜单-----"<< endl; cout <<"1.取栈顶元素t2.判断是否为空"<< endl; cout <<"3.入栈t4.出栈"<< endl; cout <<"请输入你要实现的操作:"; cin >> choice; cout << endl; switch (choice) { case 1: cout << "请输入操作的对象(栈1输入1,栈2输入2):"; cin >> num; x = getTop(num); cout<<"栈顶元素为:"<<x.id<<" "<<x.name<<" "<<x.age<<endl; break; case 2: cout << "请输入操作的对象(栈1输入1,栈2输入2):"; cin >> num; if (ifEmpty(num)) { cout << "此栈为空" << endl; } else { cout << "此栈非空" << endl; } break; case 3: pId=0; pAge=0; pos=0; cout << "请输入操作的对象(栈1输入1,栈2输入2):"; cin >> num; cout << "请输入你要入栈的人物的id:"; cin >> pId; cout << endl; cout << "请输入你要入栈的人物的name:"; cin >> pName; cout << endl; cout << "请输入你要入栈的人物的age:"; cin >> pAge; cout << endl; p1={ pId, pName, pAge }; push(p1,num); cout << endl; break; case 4: cout << "请输入操作的对象(栈1输入1,栈2输入2):"; cin >> num; x = pop(num); cout << "出栈元素为:" << x.id << " " <<x.name << " " << x.age << endl; break; default: cout << "你输入的数字有误,请重新输入" << endl; break; } } int main() { DoubleSeqStack<People> s1; while (1) { s1.menu(); } return 0; }

最后

以上就是追寻鸵鸟最近收集整理的关于数据结构7-两栈共享空间的全部内容,更多相关数据结构7-两栈共享空间内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(68)

评论列表共有 0 条评论

立即
投稿
返回
顶部