diff --git a/_02.栈与队列/05顺序队列_Queue.c b/_02.栈与队列/05顺序队列_Queue.c new file mode 100644 index 0000000..1639e84 --- /dev/null +++ b/_02.栈与队列/05顺序队列_Queue.c @@ -0,0 +1,156 @@ +#include "stdio.h" +#include "stdlib.h" +#include "io.h" +#include "math.h" +#include "time.h" + +#define OK 1 +#define ERROR 0 +#define TRUE 1 +#define FALSE 0 +#define MAXSIZE 20 /* 洢ռʼ */ + +typedef int Status; +typedef int QElemType; /* QElemType͸ʵΪint */ + +/* ѭе˳洢ṹ */ +typedef struct +{ + QElemType data[MAXSIZE]; + int front; /* ͷָ */ + int rear; /* βָ룬вգָβԪصһλ */ +}SqQueue; + +Status visit(QElemType c) +{ + printf("%d ",c); + return OK; +} + +/* ʼһնQ */ +Status InitQueue(SqQueue *Q) +{ + Q->front=0; + Q->rear=0; + return OK; +} + +/* QΪն */ +Status ClearQueue(SqQueue *Q) +{ + Q->front=Q->rear=0; + return OK; +} + +/* QΪն,򷵻TRUE,򷵻FALSE */ +Status QueueEmpty(SqQueue Q) +{ + if(Q.front==Q.rear) /* пյı־ */ + return TRUE; + else + return FALSE; +} + +/* QԪظҲǶеĵǰ */ +int QueueLength(SqQueue Q) +{ + return (Q.rear-Q.front+MAXSIZE)%MAXSIZE; +} + +/* в,eQĶͷԪ,OK,򷵻ERROR */ +Status GetHead(SqQueue Q,QElemType *e) +{ + if(Q.front==Q.rear) /* п */ + return ERROR; + *e=Q.data[Q.front]; + return OK; +} + +/* δԪeΪQµĶβԪ */ +Status EnQueue(SqQueue *Q,QElemType e) +{ + if ((Q->rear+1)%MAXSIZE == Q->front) /* ж */ + return ERROR; + Q->data[Q->rear]=e; /* Ԫeֵβ */ + Q->rear=(Q->rear+1)%MAXSIZE;/* rearָһλã */ + /* תͷ */ + return OK; +} + +/* вգɾQжͷԪأeֵ */ +Status DeQueue(SqQueue *Q,QElemType *e) +{ + if (Q->front == Q->rear) /* пյж */ + return ERROR; + *e=Q->data[Q->front]; /* ͷԪظֵe */ + Q->front=(Q->front+1)%MAXSIZE; /* frontָһλã */ + /* תͷ */ + return OK; +} + +/* ӶͷβζԶQÿԪ */ +Status QueueTraverse(SqQueue Q) +{ + int i; + i=Q.front; + while((i+Q.front)!=Q.rear) + { + visit(Q.data[i]); + i=(i+1)%MAXSIZE; + } + printf("\n"); + return OK; +} + +int main() +{ + Status j; + int i=0,l; + QElemType d; + SqQueue Q; + InitQueue(&Q); + printf("ʼк󣬶пշ%u(1: 0:)\n",QueueEmpty(Q)); + + printf("ͶԪ(%d),-1Ϊǰ: ",MAXSIZE-1); + do + { + /* scanf("%d",&d); */ + d=i+100; + if(d==-1) + break; + i++; + EnQueue(&Q,d); + }while(i0) + printf("ɶͷɾ%dԪ:\n",l-2); + while(QueueLength(Q)>2) + { + DeQueue(&Q,&d); + printf("ɾԪֵΪ%d\n",d); + } + + j=GetHead(Q,&d); + if(j) + printf("ڶͷԪΪ: %d\n",d); + ClearQueue(&Q); + printf("նк, пշ%u(1: 0:)\n",QueueEmpty(Q)); + return 0; +} + diff --git a/_02.栈与队列/_e.顺序队列.c b/_02.栈与队列/_e.顺序队列.c new file mode 100644 index 0000000..f093e2a --- /dev/null +++ b/_02.栈与队列/_e.顺序队列.c @@ -0,0 +1,141 @@ +/* + * @Author: Xu Bai + * @Date: 2019-07-02 22:06:31 + * @LastEditors: Xu Bai + * @LastEditTime: 2019-07-02 22:32:59 + */ +#include "stdlib.h" +#include "io.h" +#include "stdio.h" + +#define MAXSIZE 20 +#define OK 1 +#define ERROR 0 +#define TRUE 1 +#define FALSE 0 + +typedef int Status; +typedef int ElemType; + +typedef struct +{ + ElemType data[MAXSIZE]; + int front; + int rear; +} SqQueue; + +Status visit(ElemType e) +{ + printf("%d ", e); + return OK; +} + +Status InitQueue(SqQueue *Q) +{ + Q->front = 0; + Q->rear = 0; + return OK; +} + +Status CleatQueue(SqQueue *Q) +{ + Q->front = Q->rear = 0; + return OK; +} + +Status QuequEmpty(SqQueue Q) +{ + if (Q.front == Q.rear) + { + return TRUE; + } + else + { + return FALSE; + } +} + +int QueueLength(SqQueue Q) +{ + return (Q.rear - Q.front + MAXSIZE) % MAXSIZE; +} + +Status GetHead(SqQueue Q, ElemType *e) +{ + if (Q.front = Q.rear) + { + return ERROR; + } + else + { + *e = Q.data[Q.front]; + return OK; + } +} + +Status EnQueue(SqQueue *Q, ElemType e) +{ + if ((Q->rear + 1) % MAXSIZE == Q->front) + { + // + return ERROR; + } + else + { + Q->data[Q->rear] = e; + Q->rear = (Q->rear + 1) % MAXSIZE; + return OK; + } +} + +Status DeQueue(SqQueue *Q, ElemType *e) +{ + if (Q->front == Q->rear) + { + return ERROR; + } + *e = Q->data[Q->front]; + Q->front = (Q->front + 1) % MAXSIZE; + return OK; +} + +Status QueueTraverse(SqQueue Q) +{ + int i = Q.front; + while ((i + Q.front) != Q.rear) + { + visit(Q.data[i]); + i = (i + 1) % MAXSIZE; + } + printf("\n"); + return OK; +} + +int main(){ + ElemType e; + SqQueue q; + InitQueue(&q); + int i ; + printf("EnQueue!\n"); + for ( i = 0; i < MAXSIZE; i++) + { + EnQueue(&q,i); + } + + QueueTraverse(q); + printf("DeQueue!\n"); + for ( i = 10; i < MAXSIZE; i++) + { + DeQueue(&q,&e); + } + QueueTraverse(q); + CleatQueue(&q); + if (QuequEmpty(q)) + { + printf("empty!\n"); + } + getchar(); + return OK; + + +} diff --git a/a.out b/a.out index 7846ee7..21391a1 100644 Binary files a/a.out and b/a.out differ