本章需要掌握:
- 1、顺序栈(初始化、栈空?入栈、出栈、栈顶元素)
 
- 2、顺序队列(初始化、队列空?入队、出队)
 
- 3、链队列
 
- 4、2019真题循环队列
 
 
1、顺序栈(初始化、栈空?入栈、出栈、栈顶元素)
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
   | #include <stdio.h> #include <stdlib.h>
  #define MaxSize 50 typedef int ElemType; typedef struct{     ElemType data[MaxSize];     int top; }SqStack;
  void InitStack(SqStack &S) {     S.top=-1; } bool StackEmpty(SqStack S) {     if(-1==S.top)     {         return true;     }else{         return false;     } }
  bool Push(SqStack &S,ElemType x) {          if(S.top==MaxSize-1)     {         return false;     }     S.data[++S.top]=x;     return true; }
  bool GetTop(SqStack S,ElemType &m) {     if(StackEmpty(S))     {         return false;     }     m=S.data[S.top];     return true; }
  bool Pop(SqStack &S,ElemType &m) {     if(StackEmpty(S))     {         return false;     }     m=S.data[S.top--];     return true; } int main() {     SqStack S;     InitStack(S);     bool flag;     flag=StackEmpty(S);     if(flag)     {         printf("stack is empty\n");     }     Push(S,3);     Push(S,4);     Push(S,5);     ElemType m;     flag=GetTop(S,m);     if(flag)     {         printf("get top %d\n",m);     }     flag=Pop(S,m);     if(flag)     {         printf("pop element %d\n",m);     }     return 0; }
   | 
 
2、顺序队列(初始化、队列空?入队、出队)
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
   | #include <stdio.h> #include <stdlib.h>
  #define MaxSize 5 typedef int ElemType; typedef struct{     ElemType data[MaxSize];     int front,rear; }SqQueue;
  void InitQueue(SqQueue &Q) {     Q.front=Q.rear=0; }
  bool isEmpty(SqQueue Q) {     return Q.rear==Q.front; }
  bool EnQueue(SqQueue &Q,ElemType x) {     if((Q.rear+1)%MaxSize==Q.front)     {         return false;     }     Q.data[Q.rear]=x;     Q.rear=(Q.rear+1)%MaxSize;     return true; }
  bool DeQueue(SqQueue &Q,ElemType &x) {     if(Q.rear==Q.front)     {         return false;     }     x=Q.data[Q.front];     Q.front=(Q.front+1)%MaxSize;     return true; }
  int main() {     SqQueue Q;     InitQueue(Q);     bool ret;     ret=isEmpty(Q);     if(ret)     {         printf("SqQueue is Empty\n");     }else{         printf("SqQueue is not Empty\n");     }     EnQueue(Q,3);     EnQueue(Q,4);     EnQueue(Q,5);     ret=EnQueue(Q,6);     ret=EnQueue(Q,7);     if(ret)     {         printf("EnQueue success\n");     }else{         printf("EnQueue failed\n");     }     ElemType element;     ret=DeQueue(Q,element);     if(ret)     {         printf("DeQueue success\n");     }else{         printf("DeQueue failed\n");     }     ret=EnQueue(Q,8);     if(ret)     {         printf("EnQueue success\n");     }else{         printf("EnQueue failed\n");     }     return 0; }
   | 
 
3、链队列
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
   | #include <stdio.h> #include <stdlib.h>
  typedef int ElemType; typedef struct LinkNode{ 	ElemType data; 	struct LinkNode *next; }LinkNode; typedef struct{ 	LinkNode *front,*rear; }LinkQueue;
 
  void InitQueue(LinkQueue &Q) {     Q.front=Q.rear=(LinkNode*)malloc(sizeof(LinkNode));     Q.front->next=NULL; }
  void EnQueue(LinkQueue &Q,ElemType x) {     LinkNode *pnew=(LinkNode*)malloc(sizeof(LinkNode));     pnew->data=x;     pnew->next=NULL;     Q.rear->next=pnew;     Q.rear=pnew; }
  bool DeQueue(LinkQueue &Q,ElemType &x) {     if(Q.rear==Q.front)     {         return false;     }     LinkNode* q=Q.front->next;     x=q->data;     Q.front->next=q->next;     if(Q.rear==q)     {         Q.rear=Q.front;     }     free(q);     return true; }
  int main() {     LinkQueue Q;     InitQueue(Q);     EnQueue(Q,3);     EnQueue(Q,4);     EnQueue(Q,5);     EnQueue(Q,6);     EnQueue(Q,7);     ElemType element;     bool ret;     ret=DeQueue(Q,element);     if(ret)     {         printf("DeQueue success element= %d\n",element);     }else{         printf("DeQueue failed\n");     }     DeQueue(Q,element);     ret=DeQueue(Q,element);     if(ret)     {         printf("DeQueue success element= %d\n",element);     }else{         printf("DeQueue failed\n");     }     return 0; }
 
   | 
 
4、考研真题2019——循环队列
42.(10分)请设计一个队列,要求满足:①初始时队列为空;②入队时:允许增加队列占用空间;③出队后,出队元素所占用的空间可重复使用,即整个队列所占用的空间只增不減;④入队操作和出队操作的时间复杂度始终保持为 O(1)。请回答下列问题:
(1) 该队列是应选择链式存储结构,还是应选择顺序存储结构?
(2) 画出队列的初始状态,并给出判断队空和队满的条件。
(3)画出第一个元素入队后的队列状态。
(4)给出入队操作和出队操作的基本过程。
4.1 答案
1、链式存储结构
2、

队空的判定条件∶front == rear。
队满的判定条件∶ front == rear->next。
3、插入第一个元素后的状态如下图所示。

4、操作的基本过程如下∶
(有个小问题就是入队的时候rear后插入一个新空闲节点,空闲节点指向front?可能代码有自纠错一类的)

 
4.2 王道代码
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
   | #include <stdio.h> #include <stdlib.h>
  typedef int ElemType; typedef struct LNode {     ElemType data;     struct LNode* next; }LNode, * LinkList;
 
  void EnQueue(LinkList front, LinkList& rear, ElemType val) {     LinkList pnew;     if (rear->next == front)     {                  pnew= (LinkList)malloc(sizeof(LNode));         rear->data = val;         rear->next = pnew;         pnew->next = front;         rear = pnew;     }     else {         rear->data = val;         rear = rear->next;     } }
  void DeQueue(LinkList& front, LinkList rear) {     if (front == rear)     {         printf("queue is empty\n");     }     else {         printf("dequeue %d\n", front->data);         front = front->next;     } }
  void CircleQueue(LinkList& front, LinkList& rear) {          front = (LinkList)malloc(sizeof(LNode));     rear = front;     rear->next = front;          EnQueue(front, rear, 3);     EnQueue(front, rear, 4);          DeQueue(front, rear);     DeQueue(front, rear);     DeQueue(front, rear); }
 
  int main() {     LinkList front, rear;     CircleQueue(front, rear);     return 0; }
   |