2020考研-王道数据结构-栈和队列-栈

心已赠人 2022-01-31 00:55 353阅读 0赞

第三题

题目简述

假设以I和O分别表示入栈和出栈操作,栈的初试状态和终止状态均为空,判断序列是否合法。

代码
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. bool judge(string str)
  5. {
  6. int cnt = 0;
  7. for (int i = 0; i < str.size(); i++)
  8. {
  9. switch (str[i])
  10. {
  11. case 'I': cnt++; break;
  12. case 'O': cnt--; break;
  13. default:
  14. break;
  15. }
  16. if (cnt < 0)
  17. return false;
  18. }
  19. return cnt == 0;
  20. }
  21. int main()
  22. {
  23. if (judge("IOIIOIOO")) cout << "合法" << endl; else cout << "不合法" << endl;
  24. if (judge("IOOIOIIO")) cout << "合法" << endl; else cout << "不合法" << endl;
  25. if (judge("IIIOIOIO")) cout << "合法" << endl; else cout << "不合法" << endl;
  26. if (judge("IIIOOIOO")) cout << "合法" << endl; else cout << "不合法" << endl;
  27. }

第四题

题目简述

设单链表的表头指针为L,判断链表的n个字符是否中心对称。

代码
  1. // 本题说判断单链表中的元素是否中心对称
  2. // 为减少代码量,单链表改成字符串,和单链表的原理是一样的
  3. #include <iostream>
  4. #include <string>
  5. using namespace std;
  6. bool judge(string str)
  7. {
  8. int size = 0;
  9. char* stk = new char[str.size()];
  10. int mid = str.size() >> 1;
  11. for (int i = 0; i < mid; i++) stk[size++] = str[i];
  12. if (str.size() & 1) mid ++;
  13. for (int i = mid; i < str.size(); i++)
  14. if (str[i] != stk[--size])
  15. return false;
  16. return true;
  17. }
  18. int main()
  19. {
  20. cout << judge("xyxyx") << endl;
  21. cout << judge("xyyx") << endl;
  22. cout << judge("xyx") << endl;
  23. cout << judge("xxxy") << endl;
  24. return 0;
  25. }

第五题

题目简述

设有两个栈s1,s2共享一个存储区,为了尽量利用空间,减少溢出的可能,可采用栈顶相向,迎面增长的存储方式,设置入栈、和出栈的操作算法。

代码
  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4. #define MaxSize 100
  5. typedef int Elemtype;
  6. class SqStack
  7. {
  8. private:
  9. Elemtype data[MaxSize];
  10. int top[2];
  11. public:
  12. SqStack(){
  13. top[0] = -1; top[1] = MaxSize; };
  14. void push(int id, int x); // 向指定Id的栈push
  15. Elemtype pop(int id); // 弹出指定Id的栈的栈顶元素,并将值返回
  16. bool isEmpty(int id); // 判断栈是不是空
  17. };
  18. void SqStack::push(int id, int x)
  19. {
  20. if (id < 0 || id > 1) {
  21. cerr << "ID不合法!" << endl; return; }
  22. if (top[1] - top[0] == 1) {
  23. cerr << "栈满, push失败!" << endl; return; }
  24. if (id) data[--top[1]] = x;
  25. else data[++top[0]] = x;
  26. }
  27. Elemtype SqStack::pop(int id)
  28. {
  29. if (id < 0 || id > 1) {
  30. cerr << "ID不合法!" << endl; return (Elemtype)-1; }
  31. if (top[id] == -1 || top[id] == MaxSize){
  32. cerr << "栈为空! pop失败!" << endl; return (Elemtype)-1; }
  33. if (id) return data[top[1]++];
  34. return data[top[0]--];
  35. }
  36. bool SqStack::isEmpty(int id){
  37. if (id == 0) return top[0] == -1;
  38. return top[1] == MaxSize;
  39. }
  40. int main()
  41. {
  42. SqStack stk;
  43. for (int i = 0; i < 105; i++){
  44. if (i & 1) stk.push(0, i);
  45. else stk.push(1, i);
  46. }
  47. // 0栈出
  48. while (!stk.isEmpty(0)){
  49. cout << stk.pop(0) << " ";
  50. }
  51. cout << endl;
  52. while (!stk.isEmpty(1)){
  53. cout << stk.pop(1) << " ";
  54. }
  55. cout << endl;
  56. }

发表评论

表情:
评论列表 (有 0 条评论,353人围观)

还没有评论,来说两句吧...

相关阅读