当前位置:   article > 正文

数据结构栈和队列_头歌java 数据结构之栈、队列

头歌java 数据结构之栈、队列

第四关

  1. #include<iostream>
  2. #include<fstream>
  3. using namespace std;
  4. #define OK 1
  5. #define ERROR 0
  6. #define OVERFLOW -2
  7. #define MAXSIZE 5 //顺序栈存储空间的初始分配量
  8. typedef int Status;
  9. typedef char SElemType;
  10. typedef struct
  11. {int top[2],bot[2]; //栈顶和栈底指针
  12. SElemType *V; //栈数组
  13. int m; //栈最大可容纳元素个数
  14. }DblStack;
  15. //栈初始化
  16. Status InitDblStack(DblStack &S,int m)
  17. {
  18. // ###### Begin ######
  19. S.m = MAXSIZE;
  20. S.V = new SElemType[MAXSIZE];
  21. if(!S.V) return ERROR;
  22. S.bot[0] = 0,S.bot[1] = m - 1;
  23. S.top[0] = -1,S.top[1] = m;
  24. return OK;
  25. //###### End ######
  26. }
  27. Status DblPush(DblStack &S,int i,SElemType x)
  28. {
  29. // ###### Begin ######
  30. if(S.top[0] + 1 == S.top[1]) return ERROR;
  31. if(i == 0) S.V[S.top[0] ++ ] = x;
  32. else S.V[S.top[1] -- ] = x;
  33. return OK;
  34. //###### End ######
  35. }
  36. Status DblPop(DblStack &S,int i,SElemType &x)
  37. {
  38. // ###### Begin ######
  39. if(i == 0)
  40. {
  41. if(S.top[0] == -1) return ERROR;
  42. x = S.V[S.top[0] -- ];
  43. }
  44. else
  45. {
  46. if(S.top[1] == S.m) return ERROR;
  47. x = S.V[S.top[1] ++ ];
  48. }
  49. return OK;
  50. //###### End ######
  51. }
  52. int IsEmpty(DblStack S,int i)
  53. {
  54. // ###### Begin ######
  55. if(i == 0 && S.top[0] == -1 || i == 1 && S.top[1] == S.m) return true;
  56. return false;
  57. //###### End ######
  58. }
  59. int IsFull(DblStack S)
  60. {
  61. // ###### Begin ######
  62. if(S.top[0] + 1 == S.top[1]) return true;
  63. return false;
  64. //###### End ######
  65. }
  66. int main() {
  67. DblStack s;
  68. int choose, flag = 0;
  69. int i,m;
  70. SElemType j, e, t;
  71. choose = -1;
  72. while (choose != 0) {
  73. cin >> choose;
  74. switch (choose) {
  75. case 1://栈的初始化
  76. {
  77. cin >> m;
  78. if(InitDblStack(s,m)){
  79. flag=1;
  80. cout << "初始化栈成功!" << endl;}
  81. else
  82. cout << "初始化栈失败!" << endl;
  83. break;
  84. }
  85. case 2: {//入栈
  86. if(flag){
  87. cin >> i >> e;
  88. if(DblPush(s,i,e))
  89. cout << e << "入栈成功" << endl;
  90. else
  91. cout << "栈已满,入栈失败" << endl;
  92. }
  93. else
  94. cout << "栈未建立,请重新选择\n\n";
  95. break;
  96. }
  97. case 3:{//出栈
  98. if(flag != -1 && flag != 0){
  99. cin >> i;
  100. if(DblPop(s,i,e))
  101. cout << e << "出栈成功!" << endl;
  102. else
  103. cout <<"栈中无元素!" << endl;
  104. }
  105. else
  106. cout << "栈中无元素\n" << endl;
  107. break;
  108. }
  109. case 4:{//判空
  110. cin >> i;
  111. if(IsEmpty(s,i))
  112. cout << i << "号栈为空栈"<< endl;
  113. else
  114. cout << i << "号栈不为空栈" << endl;
  115. break;
  116. }
  117. case 5:{//判满
  118. if(IsFull(s))
  119. cout << "栈满" << endl;
  120. else
  121. cout << "栈不满" << endl;
  122. break;
  123. }
  124. default:exit(0);
  125. }
  126. }
  127. }

第五关

  1. //顺序栈定义
  2. #include<iostream>
  3. #include<cstring>
  4. using namespace std;
  5. #define OK 1
  6. #define ERROR 0
  7. #define OVERFLOW -2
  8. #define MAXSIZE 100 //顺序栈存储空间的初始分配量
  9. typedef int Status;
  10. typedef char SElemType;
  11. typedef struct {
  12. SElemType *base;//栈底指针
  13. SElemType *top;//栈顶指针
  14. int stacksize;//栈可用的最大容量
  15. } SqStack;
  16. //算法3.1 顺序栈的初始化
  17. Status InitStack(SqStack &S) {
  18. //构造一个空栈S
  19. S.base = new SElemType[MAXSIZE];//为顺序栈动态分配一个最大容量为MAXSIZE的数组空间
  20. if (!S.base)
  21. exit(OVERFLOW); //存储分配失败
  22. S.top = S.base; //top初始为base,空栈
  23. S.stacksize = MAXSIZE; //stacksize置为栈的最大容量MAXSIZE
  24. return OK;
  25. }
  26. //算法3.2 顺序栈的入栈
  27. Status Push(SqStack &S, SElemType e) {
  28. // 插入元素e为新的栈顶元素
  29. if (S.top - S.base == S.stacksize)
  30. return ERROR; //栈满
  31. *(S.top++) = e; //元素e压入栈顶,栈顶指针加1
  32. return OK;
  33. }
  34. //算法3.3 顺序栈的出栈
  35. Status Pop(SqStack &S, SElemType &e) {
  36. //删除S的栈顶元素,用e返回其值
  37. if (S.base == S.top)
  38. return ERROR;//栈空
  39. e = *(--S.top); //栈顶指针减1,将栈顶元素赋给e
  40. return OK;
  41. }
  42. int EmptyStack(SqStack S){
  43. return S.base==S.top;
  44. }
  45. int IsPalindrome(char *t)
  46. {
  47. //###### Begin ######
  48. int len = strlen(t);
  49. int i = 0,j = len - 1;
  50. while(i < j)
  51. {
  52. if(t[i] != t[j]) return 0;
  53. i ++ ,j -- ;
  54. }
  55. return 1;
  56. // ###### End ######
  57. }
  58. int main(){
  59. char str[MAXSIZE];
  60. cin>>str;
  61. if(IsPalindrome(str))
  62. cout<<"是回文"<<endl;
  63. else
  64. cout<<"不是回文"<<endl;
  65. }

第六关

  1. #include<iostream>
  2. using namespace std;
  3. //顺序栈定义
  4. #define OK 1
  5. #define ERROR 0
  6. #define OVERFLOW -2
  7. #define MAXSIZE 100//顺序栈存储空间的初始分配量
  8. typedef int Status;
  9. void InoutS(int s[]){
  10. //###### Begin ######
  11. int top = 0;
  12. int x;
  13. while(cin >> x)
  14. {
  15. if(x != -1)
  16. {
  17. if(top == MAXSIZE - 1) cout << "栈满" << endl;
  18. else s[top ++ ] = x;
  19. }
  20. else
  21. {
  22. if(top == 0) cout << "栈空" << endl;
  23. else cout << "出栈元素是" << s[ -- top] << endl;
  24. }
  25. }
  26. // ###### End ######
  27. }
  28. int main(){
  29. int s[MAXSIZE];
  30. InoutS(s);
  31. }

第七关

  1. #include<iostream>
  2. #include<stdlib.h>
  3. using namespace std;
  4. //顺序栈定义
  5. #define OK 1
  6. #define ERROR 0
  7. #define OVERFLOW -2
  8. #define MAXSIZE 100//顺序栈存储空间的初始分配量
  9. typedef int Status;
  10. typedef double SElemType;
  11. typedef struct {
  12. SElemType *base;//栈底指针
  13. SElemType *top;//栈顶指针
  14. int stacksize;//栈可用的最大容量
  15. } SqStack;
  16. //算法3.1 顺序栈的初始化
  17. Status InitStack(SqStack &S) {
  18. //构造一个空栈S
  19. S.base = new SElemType[MAXSIZE];//为顺序栈动态分配一个最大容量为MAXSIZE的数组空间
  20. if (!S.base)
  21. exit(OVERFLOW); //存储分配失败
  22. S.top = S.base; //top初始为base,空栈
  23. S.stacksize = MAXSIZE; //stacksize置为栈的最大容量MAXSIZE
  24. return OK;
  25. }
  26. //算法3.2 顺序栈的入栈
  27. Status Push(SqStack &S, SElemType e) {
  28. // 插入元素e为新的栈顶元素
  29. if (S.top - S.base == S.stacksize)
  30. return ERROR; //栈满
  31. *(S.top++) = e; //元素e压入栈顶,栈顶指针加1
  32. return OK;
  33. }
  34. //算法3.3 顺序栈的出栈
  35. Status Pop(SqStack &S, SElemType &e) {
  36. //删除S的栈顶元素,用e返回其值
  37. if (S.base == S.top)
  38. return ERROR;//栈空
  39. e = *(--S.top); //栈顶指针减1,将栈顶元素赋给e
  40. return OK;
  41. }
  42. //算法3.4 取顺序栈的栈顶元素
  43. double GetTop(SqStack S) {//返回S的栈顶元素,不修改栈顶指针
  44. if (S.top != S.base) //栈非空
  45. return *(S.top - 1); //返回栈顶元素的值,栈顶指针不变
  46. }
  47. int get(char op,int a,int b)
  48. {
  49. if(op == '+') return a + b;
  50. else if(op == '-') return a - b;
  51. else if(op == '*') return a * b;
  52. else return a / b;
  53. }
  54. double Postfix(){
  55. //###### Begin ######
  56. int *s = new int [MAXSIZE];
  57. char op;
  58. int t = 0,top = 0;
  59. while(op = getchar(),op != '$')
  60. {
  61. if(op == ' ')
  62. {
  63. if(t) s[top ++ ] = t;
  64. t = 0;
  65. continue;
  66. }
  67. if(op == '+' || op == '-' || op == '*' || op == '/')
  68. {
  69. if(t)
  70. {
  71. s[top ++ ] = t;
  72. t = 0;
  73. }
  74. int y = s[-- top],x = s[-- top];
  75. s[top ++ ] = get(op,x,y);
  76. }
  77. else t = t * 10 + (op - '0');
  78. }
  79. int k = top - 1;
  80. return s[k];
  81. // ###### End ######
  82. }
  83. int main()
  84. {
  85. double x;
  86. x=Postfix();
  87. cout << x;
  88. }

第八关

  1. #include<iostream>
  2. using namespace std;
  3. #define MAXSIZE 100
  4. bool Judge(char A[])
  5. {
  6. //###### Begin ######
  7. int k = 0;
  8. bool flag = true;
  9. for(int i = 0;A[i] != '\0';i ++ )
  10. {
  11. if(A[i] == 'I') k ++ ;
  12. else
  13. {
  14. if(!k)
  15. {
  16. flag = false;
  17. break;
  18. }
  19. k -- ;
  20. }
  21. }
  22. if(flag) cout << "序列合法" << endl;
  23. else cout << "序列非法" << endl;
  24. // ###### End ######
  25. }
  26. main()
  27. {
  28. char b[MAXSIZE];
  29. int i;
  30. cin >> b;
  31. i=Judge(b);
  32. }

第九关

  1. #include<iostream>
  2. using namespace std;
  3. #define OK 1
  4. #define ERROR 0
  5. #define OVERFLOW -2
  6. typedef char QElemType;
  7. typedef int Status;
  8. typedef struct QNode{
  9. QElemType data;
  10. struct QNode *next;
  11. } QNode, *QueuePtr;
  12. typedef struct {
  13. QueuePtr rear; //只设队尾指针
  14. } LinkQueue;
  15. void InitQueue(LinkQueue &Q) {//构造一个空队列Q
  16. //###### Begin ######
  17. Q.rear = new QNode;
  18. Q.rear -> next = Q.rear;
  19. // ###### End ######
  20. }
  21. int EmptyQueue(LinkQueue Q)
  22. {
  23. //###### Begin ######
  24. if(Q.rear -> next == Q.rear) return OK;
  25. return ERROR;
  26. // ###### End ######
  27. }
  28. Status EnQueue(LinkQueue &Q,QElemType e)
  29. {
  30. //###### Begin ######
  31. QueuePtr t = new QNode;
  32. t -> data = e;
  33. t -> next = Q.rear -> next;
  34. Q.rear -> next = t;
  35. Q.rear = t;
  36. return OK;
  37. // ###### End ######
  38. }
  39. Status DeQueue(LinkQueue &Q,QElemType &e)
  40. {
  41. //###### Begin ######
  42. if(EmptyQueue(Q)) return ERROR;
  43. QueuePtr p = Q.rear -> next;
  44. QueuePtr t = p -> next;
  45. e = t -> data;
  46. p -> next = t -> next;
  47. if(p -> next == p) Q.rear = p;
  48. delete t;
  49. return OK;
  50. // ###### End ######
  51. }
  52. main()
  53. {
  54. int choose, flag = 0;
  55. LinkQueue q;
  56. QElemType j;
  57. choose = -1;
  58. while (choose != 0) {
  59. cin >> choose;
  60. switch (choose) {
  61. case 1:InitQueue(q);flag=1;cout<<"初始化成功\n";break;
  62. case 2:
  63. if (flag) {
  64. flag = 1;
  65. cin >> j;
  66. EnQueue(q, j);
  67. cout << j<<"已入队\n";
  68. }
  69. else
  70. cout << "队列未建立,请重新选择\n\n";
  71. break;
  72. case 3:
  73. if (flag){
  74. flag=1;
  75. if(DeQueue(q, j))
  76. cout << j<<"已出队\n";
  77. else
  78. cout <<"队列已空,无可出队元素\n";
  79. }
  80. else
  81. cout << "队列未建立,请重新选择\n\n";
  82. break;
  83. case 4:
  84. if(EmptyQueue(q))
  85. cout <<"此对为空队\n";
  86. else
  87. cout<<"此对不空\n";
  88. break;
  89. }
  90. }
  91. }

第十关

  1. #include<iostream>
  2. using namespace std;
  3. #define MAXSIZE 5
  4. #define OK 1
  5. #define ERROR 0
  6. #define OVERFLOW -2
  7. typedef char ElemType;
  8. typedef int Status;
  9. typedef struct{
  10. ElemType *base;
  11. int front,rear,tag;
  12. }SqQueue;
  13. Status InitQueue(SqQueue &Q)
  14. {
  15. //###### Begin ######
  16. Q.base = new ElemType [MAXSIZE];
  17. Q.front = Q.rear = 0;
  18. Q.tag = 0;
  19. return OK;
  20. // ###### End ######
  21. }
  22. Status EnQueue(SqQueue &Q, ElemType e) {
  23. //###### Begin ######
  24. if(Q.tag == 1) return ERROR;
  25. Q.base[Q.rear] = e;
  26. Q.rear = (Q.rear + 1) % MAXSIZE;
  27. if(Q.front == Q.rear) Q.tag = 1;
  28. else Q.tag = 2;
  29. return OK;
  30. // ###### End ######
  31. }
  32. Status DeQueue(SqQueue &Q, ElemType &e)
  33. {
  34. //###### Begin ######
  35. if(Q.tag == 0) return ERROR;
  36. e = Q.base[Q.front];
  37. Q.front = (Q.front + 1) % MAXSIZE;
  38. if(Q.front == Q.rear) Q.tag = 0;
  39. return OK;
  40. // ###### End ######
  41. }
  42. main()
  43. {
  44. SqQueue q;
  45. int choose,flag;
  46. ElemType j;
  47. choose = -1;
  48. while (choose != 0) {
  49. cin >> choose;
  50. switch (choose) {
  51. case 1:
  52. if (InitQueue(q)) {
  53. flag = 1;
  54. cout << "成功对队列进行初始化\n";
  55. } else
  56. cout << "初始化队列失败\n";
  57. break;
  58. case 2:
  59. if (flag) {
  60. flag = 1;
  61. cin >> j;
  62. if(EnQueue(q, j))
  63. cout << j<<"已入队\n";
  64. else
  65. cout <<"队列已满,不可入队\n";
  66. }
  67. else
  68. cout << "队列未建立,请重新选择\n";
  69. break;
  70. case 3:
  71. if (flag){
  72. flag=1;
  73. if(DeQueue(q, j))
  74. cout << j<<"已出队\n";
  75. else
  76. cout <<"队列已空,无可出队元素\n";
  77. }
  78. else
  79. cout << "队列未建立,请重新选择\n";
  80. break;
  81. }
  82. }
  83. }

第十一关

  1. #include<iostream>
  2. using namespace std;
  3. #define OK 1
  4. #define ERROR 0
  5. #define OVERFLOW -2
  6. #define MAXSIZE 10
  7. typedef char QElemType;
  8. typedef int Status;
  9. typedef struct{
  10. //###### Begin ######
  11. QElemType *base;
  12. int front,rear;
  13. // ###### End ######
  14. }SqQueue;
  15. Status InitQueue(SqQueue &Q)
  16. {
  17. //###### Begin ######
  18. Q.base = new QElemType [MAXSIZE];
  19. Q.front = Q.rear = 0;
  20. return OK;
  21. // ###### End ######
  22. }
  23. Status EnQueue(SqQueue &Q,QElemType e)
  24. {
  25. //###### Begin ######
  26. if((Q.front + 1) % MAXSIZE == Q.rear) return ERROR;
  27. Q.base[Q.front] = e;
  28. Q.front = (Q.front + 1) % MAXSIZE;
  29. return OK;
  30. // ###### End ######
  31. }
  32. Status DeQueue(SqQueue &Q,QElemType &e)
  33. {
  34. //###### Begin ######
  35. if(Q.front == Q.rear) return ERROR;
  36. e = Q.base[Q.rear];
  37. Q.rear = (Q.rear + 1) % MAXSIZE;
  38. return OK;
  39. // ###### End ######
  40. }
  41. main()
  42. {
  43. SqQueue q;
  44. int choose,flag;
  45. QElemType j;
  46. choose = -1;
  47. while (choose != 0) {
  48. cin >> choose;
  49. switch (choose) {
  50. case 1:
  51. if (InitQueue(q)) {
  52. flag = 1;
  53. cout << "成功对队列进行初始化\n";
  54. } else
  55. cout << "初始化队列失败\n";
  56. break;
  57. case 2:
  58. if (flag) {
  59. flag = 1;
  60. cin >> j;
  61. if(EnQueue(q, j))
  62. cout << j<<"已入队\n";
  63. else
  64. cout <<"队列已满,不可入队\n";
  65. }
  66. else
  67. cout << "队列未建立,请重新选择\n";
  68. break;
  69. case 3:
  70. if (flag){
  71. flag=1;
  72. if(DeQueue(q, j))
  73. cout << j<<"已出队\n";
  74. else
  75. cout <<"队列已空,无可出队元素\n";
  76. }
  77. else
  78. cout << "队列未建立,请重新选择\n";
  79. break;
  80. }
  81. }
  82. }

第十二关

  1. #include<iostream>
  2. #include<stdlib.h>
  3. #define M 100
  4. #define N 100
  5. using namespace std;
  6. int Ack(int m,int n)//递归算法
  7. {
  8. //###### Begin ######
  9. if(!m) return n + 1;
  10. else if(!n) return Ack(m - 1,1);
  11. else return Ack(m - 1,Ack(m,n - 1));
  12. // ###### End ######
  13. }
  14. main(){
  15. int m,n;
  16. int x,y;
  17. cin>>m>>n;
  18. x=Ack(m,n);
  19. cout << x <<endl;
  20. }

第十三关

  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4. typedef struct LNode {
  5. int data; //结点的数据域
  6. struct LNode *next; //结点的指针域
  7. } LNode, *LinkList; //LinkList为指向结构体LNode的指针类型
  8. void InitList(LinkList &f) //创建链表
  9. {
  10. f = new LNode;
  11. f->next = NULL;
  12. }
  13. void ListInput(LinkList &f,int n)
  14. {
  15. LinkList r,p;
  16. int i=1;
  17. r=p=f;
  18. cin >> p->data;
  19. while(i<n)
  20. {
  21. p = new LNode;
  22. cin >> p->data;
  23. p->next = NULL;
  24. r->next = p;
  25. r = p;
  26. i++;
  27. }
  28. cout<<"完成输入\n";
  29. }
  30. int GetMax(LinkList p)
  31. {
  32. //###### Begin ######
  33. LinkList t = p;
  34. int maxv = 0;
  35. while(t)
  36. {
  37. maxv = max(maxv,t -> data);
  38. t = t -> next;
  39. }
  40. return maxv;
  41. // ###### End ######
  42. }
  43. int GetLength(LinkList p)
  44. {
  45. //###### Begin ######
  46. LinkList t = p;
  47. int length = 0;
  48. while(t)
  49. {
  50. length ++ ;
  51. t = t -> next;
  52. }
  53. return length;
  54. // ###### End ######
  55. }
  56. double GetAverage(LinkList p,int n)
  57. {
  58. //###### Begin ######
  59. LinkList t = p;
  60. double sum = 0;
  61. while(t)
  62. {
  63. sum += t -> data;
  64. t = t -> next;
  65. }
  66. return sum / n;
  67. // ###### End ######
  68. }
  69. main()
  70. {
  71. LinkList L;
  72. int choose,tag=0,n;
  73. int max,len;
  74. double ave;
  75. choose=-1;
  76. while (choose != 0) {
  77. cin >> choose;
  78. switch (choose) {
  79. case 1: InitList(L);tag=1;cout<<"初始化完成\n";break;
  80. case 2:
  81. {
  82. if(tag==1)
  83. {
  84. cin>>n;
  85. ListInput(L,n);
  86. }
  87. break;
  88. }
  89. case 3:
  90. {
  91. if(tag==1)
  92. {
  93. max=GetMax(L);
  94. cout<<"max="<<max<<endl;
  95. }
  96. break;
  97. }
  98. case 4:
  99. {
  100. if(tag==1)
  101. {
  102. len=GetLength(L);
  103. cout<<"len="<<len<<endl;
  104. }
  105. break;
  106. }
  107. case 5:
  108. {
  109. if(tag==1)
  110. {
  111. ave=GetAverage(L,len);
  112. cout<<"ave="<<ave<<endl;
  113. }
  114. break;
  115. }
  116. }
  117. }
  118. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/640373
推荐阅读
相关标签
  

闽ICP备14008679号