title: 【锐格】数据结构-栈和队列
tags:

  • 锐格
  • 数据结构
    abbrlink: 11bfbc93
    date: 2021-10-22 14:23:01

欢迎加入NEFU计算机编程交流群:523539085

顺序栈

4909

  1. #include<iostream>
  2. using namespace std;
  3. struct Node {
  4. int data;
  5. Node *next;
  6. };
  7. class SeqStack {
  8. private:
  9. int *array;
  10. int maxSize;
  11. int top;
  12. void stackFull();
  13. public:
  14. SeqStack(int sz=0);
  15. void Push(const int &x);
  16. bool Pop(int &x);
  17. bool getTop(int &x);
  18. bool isEmpty() const
  19. {
  20. return (top==-1)?true:false;
  21. }
  22. bool isFull() const
  23. {
  24. return (top==maxSize-1)?true:false;
  25. }
  26. int getSize() const
  27. {
  28. return top+1;
  29. }
  30. };
  31. SeqStack::SeqStack(int sz):top(-1),maxSize(sz)
  32. {
  33. array = new int[maxSize];
  34. }
  35. void SeqStack::Push(const int &x)
  36. {
  37. if (this->isFull())
  38. this->stackFull();
  39. array[++top] = x;
  40. }
  41. bool SeqStack::Pop(int &x)
  42. {
  43. if (this->isEmpty())
  44. return false;
  45. x = array[top--];
  46. return true;
  47. }
  48. bool SeqStack::getTop(int &x)
  49. {
  50. if (this->isEmpty())
  51. return false;
  52. x = array[top];
  53. return true;
  54. }
  55. void SeqStack::stackFull()
  56. {
  57. maxSize = maxSize*2;
  58. int *temp = new int[maxSize];
  59. int i;
  60. for (i=0;i<=top;i++)
  61. temp[i] = array[i];
  62. delete []array;
  63. array = temp;
  64. }
  65. class LinkList {
  66. private:
  67. Node *first;
  68. public:
  69. LinkList();
  70. void Insert(const int &x);
  71. int Length();
  72. void Reverse();
  73. void output();
  74. };
  75. LinkList::LinkList()
  76. {
  77. first = new Node();
  78. first->next = NULL;
  79. }
  80. void LinkList::Insert(const int &x)
  81. {
  82. Node *t = first;
  83. while (t->next!=NULL)
  84. t = t->next;
  85. Node *n = new Node();
  86. n->data = x;
  87. n->next = t->next;
  88. t->next = n;
  89. }
  90. int LinkList::Length()
  91. {
  92. int count=0;
  93. Node *t = first;
  94. while (t->next!=NULL) {
  95. t = t->next;
  96. count++;
  97. }
  98. return count;
  99. }
  100. void LinkList::Reverse()
  101. {
  102. //write your code here
  103. SeqStack S(Length());
  104. Node *p=first->next;
  105. int x;
  106. //output();
  107. first->next=NULL;
  108. //output();
  109. while(p!=NULL)
  110. {
  111. S.Push(p->data);
  112. p=p->next;
  113. }
  114. //output();
  115. while(!S.isEmpty())
  116. {
  117. S.Pop(x);
  118. Insert(x);
  119. }
  120. }
  121. void LinkList::output()
  122. {
  123. Node *t = first;
  124. while (t->next!=NULL) {
  125. t=t->next;
  126. cout << t->data << " ";
  127. }
  128. cout << endl;
  129. }
  130. int main()
  131. {
  132. LinkList l;
  133. int x;
  134. cin >> x;
  135. while (x!=-1) {
  136. l.Insert(x);
  137. cin >> x;
  138. }
  139. l.output();
  140. l.Reverse();
  141. l.output();
  142. return 0;
  143. }

4908

从上一道题cv就行

  1. #include<iostream>
  2. using namespace std;
  3. class SeqStack {
  4. private:
  5. int *array;
  6. int maxSize;
  7. int top;
  8. void stackFull();
  9. public:
  10. SeqStack(int sz=0);
  11. void Push(const int &x);
  12. bool Pop(int &x);
  13. bool getTop(int &x);
  14. bool isEmpty() const
  15. {
  16. return (top==-1)?true:false;
  17. }
  18. bool isFull() const
  19. {
  20. return (top==maxSize-1)?true:false;
  21. }
  22. int getSize() const
  23. {
  24. return top+1;
  25. }
  26. int getMaxSize() const
  27. {
  28. return maxSize;
  29. }
  30. void MakeEmpty()
  31. {
  32. top = -1;
  33. }
  34. };
  35. SeqStack::SeqStack(int sz):top(-1),maxSize(sz)
  36. {
  37. //write your code here
  38. array=new int[maxSize];
  39. }
  40. void SeqStack::Push(const int &x)
  41. {
  42. //write your code here
  43. if(this->isFull())
  44. this->stackFull();
  45. array[++top]=x;
  46. }
  47. bool SeqStack::Pop(int &x)
  48. {
  49. //write your code here
  50. if(this->isEmpty())
  51. return false;
  52. x=array[top--];
  53. return true;
  54. }
  55. bool SeqStack::getTop(int &x)
  56. {
  57. //write your code here
  58. if(this->isEmpty())
  59. return false;
  60. x=array[top];
  61. return true;
  62. }
  63. void SeqStack::stackFull()
  64. {
  65. //write your code here
  66. maxSize*=2;
  67. int *tmp=new int[maxSize];
  68. for(int i=0;i<=top;i++)
  69. tmp[i]=array[i];
  70. delete []array;
  71. array=tmp;
  72. }
  73. int main()
  74. {
  75. int x;
  76. SeqStack ss(4);
  77. ss.Push(1);
  78. ss.Push(2);
  79. ss.Push(3);
  80. ss.Push(4);
  81. ss.Push(5);
  82. ss.Pop(x);
  83. cout << x << endl;
  84. cout << ss.getSize() << endl;
  85. cout << ss.getMaxSize() << endl;
  86. return 0;
  87. }

4873待解决

4870

  1. #include<stdio.h>
  2. #define maxsize 10000
  3. typedef struct
  4. {
  5. int data[maxsize]; //存放栈中元素
  6. int top; //栈顶指针
  7. }SqStack;
  8. //初始化栈
  9. void initStack(SqStack &st)
  10. {
  11. st.top = -1;
  12. }
  13. //进栈算法
  14. int Push(SqStack &st, int x)
  15. {
  16. if(st.top == maxsize - 1)
  17. return 0;
  18. ++(st.top); //先移动指针再进栈
  19. st.data[st.top] = x;
  20. return 1;
  21. }
  22. //出栈算法
  23. int Pop(SqStack &st , int &x)
  24. {
  25. if(st.top == -1)
  26. return 0;
  27. x = st.data[st.top];
  28. --(st.top);
  29. return 1;
  30. }
  31. void isEmpty(SqStack st)
  32. {
  33. //write your own codes
  34. if(st.top==-1)
  35. printf("Stack is empty\n");
  36. else
  37. printf("Stack is not empty\n");
  38. }
  39. int main()
  40. {
  41. int n , i , e;
  42. SqStack S;
  43. initStack(S);
  44. isEmpty(S); //需要写的函数
  45. scanf("%d",&n);
  46. for(i = 0 ; i < n; ++i)
  47. {
  48. scanf("%d",&e);
  49. Push(S,e);
  50. }
  51. isEmpty(S);
  52. scanf("%d",&n);
  53. for(int i = 0 ;i < n ; ++i)
  54. Pop(S,e);
  55. isEmpty(S);
  56. return 0;
  57. }

4869

cv大法好

  1. #include<stdio.h>
  2. #define maxsize 10000
  3. typedef struct
  4. {
  5. int data[maxsize]; //存放栈中元素
  6. int top; //栈顶指针
  7. }SqStack;
  8. //初始化栈
  9. void initStack(SqStack &st)
  10. {
  11. st.top = -1;
  12. }
  13. //进栈算法
  14. int Push(SqStack &st, int x)
  15. {
  16. if(st.top == maxsize - 1)
  17. return 0;
  18. ++(st.top); //先移动指针再进栈
  19. st.data[st.top] = x;
  20. return 1;
  21. }
  22. //出栈算法
  23. int Pop(SqStack &st , int &x)
  24. {
  25. if(st.top == -1)
  26. return 0;
  27. x = st.data[st.top];
  28. --(st.top);
  29. return 1;
  30. }
  31. bool isEmpty(SqStack st)
  32. {
  33. //write your own codes
  34. if(st.top==-1)
  35. return true;
  36. return false;
  37. }
  38. //输出栈中的元素
  39. int print_S(SqStack st)
  40. {
  41. if( isEmpty(st) )
  42. {
  43. printf("Stack is Empty");
  44. return 0;
  45. }
  46. int iPointer = st.top;
  47. while(iPointer != -1)
  48. {
  49. printf("%d ",st.data[iPointer]);
  50. --iPointer;
  51. }
  52. printf("\n");
  53. return 1;
  54. }
  55. int main()
  56. {
  57. int n , i , e;
  58. SqStack S;
  59. initStack(S);
  60. scanf("%d",&n);
  61. for(i = 0 ; i < n; ++i)
  62. {
  63. scanf("%d",&e);
  64. Push(S,e); //要写的函数:进栈操作
  65. }
  66. print_S(S);
  67. scanf("%d",&n);
  68. for(int i = 0 ;i < n ; ++i)
  69. {
  70. Pop(S,e); //要写的函数:出栈操作
  71. printf("%d ",e);
  72. }
  73. printf("\n");
  74. print_S(S);
  75. return 0;
  76. }

4868

cvcv

  1. #include<stdio.h>
  2. #define maxsize 10000
  3. typedef struct
  4. {
  5. int data[maxsize]; //存放栈中元素
  6. int top; //栈顶指针
  7. }SqStack;
  8. //初始化栈
  9. void initStack(SqStack &st)
  10. {
  11. st.top = -1;
  12. }
  13. //进栈算法
  14. int Push(SqStack &st, int x)
  15. {
  16. if(st.top == maxsize - 1)
  17. return 0;
  18. ++(st.top); //先移动指针再进栈
  19. st.data[st.top] = x;
  20. return 1;
  21. }
  22. bool isEmpty(SqStack st)
  23. {
  24. //write your own codes
  25. if(st.top==-1)
  26. return true;
  27. return false;
  28. }
  29. //输出栈中的元素
  30. int print_S(SqStack st)
  31. {
  32. if( isEmpty(st) )
  33. {
  34. printf("Stack is Empty");
  35. return 0;
  36. }
  37. int iPointer = st.top;
  38. while(iPointer != -1)
  39. {
  40. printf("%d ",st.data[iPointer]);
  41. --iPointer;
  42. }
  43. printf("\n");
  44. return 1;
  45. }
  46. int main()
  47. {
  48. int n , i , e;
  49. SqStack S;
  50. initStack(S);
  51. scanf("%d",&n);
  52. for(i = 0 ; i < n; ++i)
  53. {
  54. scanf("%d",&e);
  55. Push(S,e);
  56. }
  57. print_S(S); //需要写出的函数:打印栈
  58. return 0;
  59. }

链式栈

4225

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<malloc.h>
  4. #define TRUE 1
  5. #define FALSE 0
  6. #define s top
  7. typedef int Status;
  8. typedef struct Node
  9. {
  10. char data;
  11. struct Node *next;
  12. }Node;
  13. Node *top;
  14. const Status isEmpty()
  15. {
  16. return (top==NULL)?TRUE:FALSE;
  17. }
  18. void Push(const char x)
  19. {
  20. Node *n = (struct Node *)malloc(sizeof(struct Node));
  21. n->data = x;
  22. n->next = top;
  23. top = n;
  24. }
  25. Status Pop(char *x)
  26. {
  27. if (isEmpty())
  28. return FALSE;
  29. *x = top->data;
  30. Node *t = top;
  31. top = top->next;
  32. free(t);
  33. return TRUE;
  34. }
  35. Status getTop(char *x)
  36. {
  37. if (isEmpty())
  38. return FALSE;
  39. *x = top->data;
  40. return TRUE;
  41. }
  42. Status match(char f[]) {
  43. char* p = f;
  44. char ch;
  45. while (*p != '\0') {
  46. if (*p == 39) {//单引号
  47. ++p;
  48. while (*p != 39)//小括号
  49. ++p;
  50. ++p;
  51. }
  52. else if (*p == 34) {//双引号
  53. ++p;
  54. while (*p != 34)//小括号
  55. ++p;
  56. ++p;
  57. }
  58. else {
  59. switch (*p) {
  60. case '(':
  61. case '{':
  62. case '[':Push(*p); break;
  63. case ')':getTop(&ch);
  64. if (ch == '(')
  65. Pop(&ch);
  66. else return 0;
  67. break;
  68. case '}':getTop(&ch);
  69. if (ch == '{')
  70. Pop(&ch);
  71. else return 0;
  72. break;
  73. case ']':getTop(&ch);
  74. if (ch == '[')
  75. Pop(&ch);
  76. else return 0;
  77. }
  78. ++p;
  79. }
  80. }
  81. if (isEmpty())//如果栈为空,表示括号都已经匹配
  82. return 1;
  83. else
  84. return 0;
  85. }
  86. int main()
  87. {
  88. top = NULL;
  89. char str[100];
  90. gets(str);
  91. if (match(str))
  92. printf("YES\n");
  93. else
  94. printf("NO\n");
  95. return 0;
  96. }

栈与递归

4227

  1. #include<stdio.h>
  2. // write your code here
  3. int sum(int a[],int x)
  4. {
  5. if(x<1) return 0;
  6. return a[x-1]+sum(a,--x);
  7. }
  8. int main()
  9. {
  10. int a[6],i;
  11. for (i=0;i<6;i++)
  12. scanf("%d", a+i);
  13. printf("%d\n", sum(a, 6));
  14. return 0;
  15. }

4226

  1. #include<stdio.h>
  2. // write your code here
  3. int max(int a[], int x)
  4. {
  5. if(x<1) return -1;
  6. int tmp=max(a, x-1);
  7. return a[x-1]>tmp?a[x-1]:tmp;
  8. }
  9. int main()
  10. {
  11. int a[6],i;
  12. for (i=0;i<6;i++)
  13. scanf("%d", a+i);
  14. printf("%d\n", max(a, 6));
  15. return 0;
  16. }

4228

  1. #include <stdio.h>
  2. // write your code here
  3. float average(float a[],int n,int y)
  4. {
  5. if (n == 1)
  6. return a[0];
  7. else
  8. return ((n - 1) * average(a, n - 1, y) + a[n - 1]) / n;
  9. }
  10. int main()
  11. {
  12. float a[6];
  13. int i;
  14. for (i=0;i<6;i++)
  15. scanf("%f", a+i);
  16. printf("%.2f\n", average(a, 6, 6));
  17. return 0;
  18. }

队列

4236

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<malloc.h>
  4. #define TRUE 1
  5. #define FALSE 0
  6. typedef int Status;
  7. typedef struct Node {
  8. int data;
  9. struct Node *next;
  10. }Node, *pNode;
  11. pNode p;
  12. void InitQueue()
  13. {
  14. p = (struct Node *)malloc(sizeof(struct Node));
  15. p->next = p;
  16. }
  17. void EnQueue(const int x)
  18. {
  19. // write your code here
  20. Node *s;
  21. s = (Node *)malloc(sizeof(Node));
  22. s->data = x;
  23. if (p == NULL)
  24. {
  25. p = s;
  26. p->next = p;
  27. }
  28. else
  29. {
  30. s->next = p->next; //将s作为*p之后的结点
  31. p->next = s;
  32. p = s; //*p指向*s
  33. }
  34. }
  35. const Status isEmpty()
  36. {
  37. // write your code here
  38. }
  39. Status DeQueue(int *x)
  40. {
  41. // write your code here
  42. Node *s;
  43. if (p == NULL)
  44. return 0;
  45. if (p->next == NULL)
  46. { //只有一个结点的情况
  47. *x = p->data;
  48. free(p);
  49. p = NULL;
  50. }
  51. else
  52. { //将*p之后结点的data域值赋给x,然后删除之
  53. s = p->next;
  54. *x = s->data;
  55. p->next = s->next;
  56. free(s);
  57. }
  58. return 1;
  59. }
  60. const void Print()
  61. {
  62. Node *t = p->next;
  63. while (t->next!=p->next) {
  64. printf("%d ", t->next->data);
  65. t = t->next;
  66. }
  67. printf("\n");
  68. }
  69. int main()
  70. {
  71. InitQueue();
  72. int x;
  73. EnQueue(1);
  74. EnQueue(2);
  75. EnQueue(3);
  76. DeQueue(&x);
  77. EnQueue(4);
  78. DeQueue(&x);
  79. DeQueue(&x);
  80. DeQueue(&x);
  81. EnQueue(5);
  82. Print();
  83. return 0;
  84. }

4915

  1. #include<iostream>
  2. using namespace std;
  3. struct Node {
  4. int data;
  5. Node *next;
  6. };
  7. class LinkList {
  8. private:
  9. Node *first;
  10. public:
  11. LinkList();
  12. void Insert(const int &x);
  13. Node *First();
  14. int Length(Node *n);
  15. };
  16. LinkList::LinkList()
  17. {
  18. first = new Node();
  19. first->next = NULL;
  20. }
  21. void LinkList::Insert(const int &x)
  22. {
  23. Node *t = first;
  24. while (t->next!=NULL)
  25. t = t->next;
  26. Node *n = new Node();
  27. n->data = x;
  28. n->next = t->next;
  29. t->next = n;
  30. }
  31. Node *LinkList::First()
  32. {
  33. return first;
  34. }
  35. //write your code here
  36. int LinkList::Length(Node *n)
  37. {
  38. if(n==NULL) return -1;
  39. return 1+Length(n->next);
  40. }
  41. int main()
  42. {
  43. LinkList l;
  44. int x;
  45. cin >> x;
  46. while (x!=-1) {
  47. l.Insert(x);
  48. cin >> x;
  49. }
  50. //write your code here
  51. printf("%d\n", l.Length(l.First()));
  52. return 0;
  53. }

4872

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct QNode
  4. {
  5. int data;
  6. struct QNode *next;
  7. }QNode,*QueuePtr;
  8. typedef struct{
  9. QueuePtr front;
  10. QueuePtr rear;
  11. }LinkQueue;
  12. int InitQueue(LinkQueue &Q)
  13. {
  14. Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));
  15. if( !Q.front) exit(0);
  16. Q.front->next = NULL;
  17. return 1;
  18. }
  19. int EnQueue(LinkQueue &Q, int e)
  20. {
  21. QNode* p=(QueuePtr)malloc(sizeof(QNode));
  22. //printf("--\n");
  23. p->data=e;
  24. p->next=NULL;
  25. Q.rear->next=p;
  26. Q.rear=p;
  27. return 1;
  28. }
  29. int DeQueue(LinkQueue &Q,int &e)
  30. {
  31. QNode *p=Q.front->next;
  32. Q.front->next=p->next;
  33. if(p==Q.rear) Q.rear==Q.front;
  34. e=p->data;
  35. free(p);
  36. return 1;
  37. }
  38. int main()
  39. {
  40. int m,n;
  41. int i,e;
  42. LinkQueue Q;
  43. InitQueue(Q);
  44. scanf("%d",&n);
  45. for(i = 0 ; i < n ; ++i)
  46. {
  47. scanf("%d",&e);
  48. EnQueue(Q,e);
  49. }
  50. scanf("%d",&m);
  51. for(i = 0 ; i < m ; ++i)
  52. {
  53. DeQueue(Q,e);
  54. printf("%d ",e);
  55. }
  56. printf("\n");
  57. return 0;
  58. }

4235

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<malloc.h>
  4. #define TRUE 1
  5. #define FALSE 0
  6. typedef int Status;
  7. int *q;
  8. int maxSize;
  9. int front;
  10. int rear;
  11. int tag;
  12. const Status isFull()
  13. {
  14. // write your code here
  15. if(rear%maxSize==front&&tag==1)
  16. return 1;
  17. return 0;
  18. }
  19. const Status isEmpty()
  20. {
  21. // write your code here
  22. if(rear==front&&tag==0)
  23. return 1;
  24. return 0;
  25. }
  26. void InitStack(int sz)
  27. {
  28. maxSize = sz;
  29. q = (int *)malloc(maxSize*sizeof(int));
  30. front = 0;
  31. rear = 0;
  32. tag = 0;
  33. }
  34. void output()
  35. {
  36. int i;
  37. for (i=0;i<maxSize;i++)
  38. printf("%d ", q[i]);
  39. }
  40. Status EnQueue(int x)
  41. {
  42. // write your code here
  43. if(isFull()) return 0;
  44. q[rear]=x;
  45. rear=(rear+1)%maxSize;
  46. if(rear==front) tag=1;
  47. return 1;
  48. }
  49. Status DeQueue(int x)
  50. {
  51. // write your code here
  52. if(isEmpty()) return 0;
  53. x=q[front];
  54. front=(front+1)%maxSize;
  55. if(rear==front) tag==0;
  56. return 1;
  57. }
  58. int main()
  59. {
  60. InitStack(4);
  61. int m;
  62. EnQueue(1);
  63. DeQueue(m);
  64. DeQueue(m);
  65. EnQueue(2);
  66. EnQueue(0);
  67. EnQueue(4);
  68. EnQueue(5);
  69. DeQueue(m);
  70. DeQueue(m);
  71. EnQueue(6);
  72. output();
  73. return 0;
  74. }