From 9350047e66ec1c84f570f6340ce2ff65b75d9938 Mon Sep 17 00:00:00 2001 From: Xu Bai <1373953675@qq.com> Date: Wed, 3 Jul 2019 22:05:34 +0800 Subject: [PATCH] =?UTF-8?q?=E9=93=BE=E9=98=9F=E5=88=9D=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _02.栈与队列/06链队列_LinkQueue.c | 181 ++++++++++++++++++++++++++++++ _02.栈与队列/_f.链队列.c | 106 +++++++++++++++++ 2 files changed, 287 insertions(+) create mode 100644 _02.栈与队列/06链队列_LinkQueue.c create mode 100644 _02.栈与队列/_f.链队列.c diff --git a/_02.栈与队列/06链队列_LinkQueue.c b/_02.栈与队列/06链队列_LinkQueue.c new file mode 100644 index 0000000..655d55b --- /dev/null +++ b/_02.栈与队列/06链队列_LinkQueue.c @@ -0,0 +1,181 @@ +#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 QNode /* ½áµã½á¹¹ */ +{ + QElemType data; + struct QNode *next; +}QNode,*QueuePtr; + +typedef struct /* ¶ÓÁеÄÁ´±í½á¹¹ */ +{ + QueuePtr front,rear; /* ¶ÓÍ·¡¢¶ÓβָÕë */ +}LinkQueue; + +Status visit(QElemType c) +{ + printf("%d ",c); + return OK; +} + +/* ¹¹ÔìÒ»¸ö¿Õ¶ÓÁÐQ */ +Status InitQueue(LinkQueue *Q) +{ + Q->front=Q->rear=(QueuePtr)malloc(sizeof(QNode)); + if(!Q->front) + exit(OVERFLOW); + Q->front->next=NULL; + return OK; +} + +/* Ïú»Ù¶ÓÁÐQ */ +Status DestroyQueue(LinkQueue *Q) +{ + while(Q->front) + { + Q->rear=Q->front->next; + free(Q->front); + Q->front=Q->rear; + } + return OK; +} + +/* ½«QÇåΪ¿Õ¶ÓÁÐ */ +Status ClearQueue(LinkQueue *Q) +{ + QueuePtr p,q; + Q->rear=Q->front; + p=Q->front->next; + Q->front->next=NULL; + while(p) + { + q=p; + p=p->next; + free(q); + } + return OK; +} + +/* ÈôQΪ¿Õ¶ÓÁÐ,Ôò·µ»ØTRUE,·ñÔò·µ»ØFALSE */ +Status QueueEmpty(LinkQueue Q) +{ + if(Q.front==Q.rear) + return TRUE; + else + return FALSE; +} + +/* Çó¶ÓÁеij¤¶È */ +int QueueLength(LinkQueue Q) +{ + int i=0; + QueuePtr p; + p=Q.front; + while(Q.rear!=p) + { + i++; + p=p->next; + } + return i; +} + +/* Èô¶ÓÁв»¿Õ,ÔòÓÃe·µ»ØQµÄ¶ÓÍ·ÔªËØ,²¢·µ»ØOK,·ñÔò·µ»ØERROR */ +Status GetHead(LinkQueue Q,QElemType *e) +{ + QueuePtr p; + if(Q.front==Q.rear) + return ERROR; + p=Q.front->next; + *e=p->data; + return OK; +} + + +/* ²åÈëÔªËØeΪQµÄеĶÓÎ²ÔªËØ */ +Status EnQueue(LinkQueue *Q,QElemType e) +{ + QueuePtr s=(QueuePtr)malloc(sizeof(QNode)); + if(!s) /* ´æ´¢·ÖÅäʧ°Ü */ + exit(OVERFLOW); + s->data=e; + s->next=NULL; + Q->rear->next=s; /* °ÑÓµÓÐÔªËØeµÄнáµãs¸³Öµ¸øÔ­¶Óβ½áµãµÄºó¼Ì£¬¼ûͼÖТ٠*/ + Q->rear=s; /* °Ñµ±Ç°µÄsÉèÖÃΪ¶Óβ½áµã£¬rearÖ¸Ïòs£¬¼ûͼÖÐ¢Ú */ + return OK; +} + +/* Èô¶ÓÁв»¿Õ,ɾ³ýQµÄ¶ÓÍ·ÔªËØ,ÓÃe·µ»ØÆäÖµ,²¢·µ»ØOK,·ñÔò·µ»ØERROR */ +Status DeQueue(LinkQueue *Q,QElemType *e) +{ + QueuePtr p; + if(Q->front==Q->rear) + return ERROR; + p=Q->front->next; /* ½«Óûɾ³ýµÄ¶ÓÍ·½áµãÔÝ´æ¸øp£¬¼ûͼÖТ٠*/ + *e=p->data; /* ½«Óûɾ³ýµÄ¶ÓÍ·½áµãµÄÖµ¸³Öµ¸øe */ + Q->front->next=p->next;/* ½«Ô­¶ÓÍ·½áµãµÄºó¼Ìp->next¸³Öµ¸øÍ·½áµãºó¼Ì£¬¼ûͼÖÐ¢Ú */ + if(Q->rear==p) /* Èô¶ÓÍ·¾ÍÊǶÓ⣬Ôòɾ³ýºó½«rearÖ¸ÏòÍ·½áµã£¬¼ûͼÖÐ¢Û */ + Q->rear=Q->front; + free(p); + return OK; +} + +/* ´Ó¶ÓÍ·µ½¶ÓβÒÀ´Î¶Ô¶ÓÁÐQÖÐÿ¸öÔªËØÊä³ö */ +Status QueueTraverse(LinkQueue Q) +{ + QueuePtr p; + p=Q.front->next; + while(p) + { + visit(p->data); + p=p->next; + } + printf("\n"); + return OK; +} + +int main() +{ + int i; + QElemType d; + LinkQueue q; + i=InitQueue(&q); + if(i) + printf("³É¹¦µØ¹¹ÔìÁËÒ»¸ö¿Õ¶ÓÁÐ!\n"); + printf("ÊÇ·ñ¿Õ¶ÓÁУ¿%d(1:¿Õ 0:·ñ) ",QueueEmpty(q)); + printf("¶ÓÁеij¤¶ÈΪ%d\n",QueueLength(q)); + EnQueue(&q,-5); + EnQueue(&q,5); + EnQueue(&q,10); + printf("²åÈë3¸öÔªËØ(-5,5,10)ºó,¶ÓÁеij¤¶ÈΪ%d\n",QueueLength(q)); + printf("ÊÇ·ñ¿Õ¶ÓÁУ¿%d(1:¿Õ 0:·ñ) ",QueueEmpty(q)); + printf("¶ÓÁеÄÔªËØÒÀ´ÎΪ£º"); + QueueTraverse(q); + i=GetHead(q,&d); + if(i==OK) + printf("¶ÓÍ·ÔªËØÊÇ£º%d\n",d); + DeQueue(&q,&d); + printf("ɾ³ýÁ˶ÓÍ·ÔªËØ%d\n",d); + i=GetHead(q,&d); + if(i==OK) + printf("еĶÓÍ·ÔªËØÊÇ£º%d\n",d); + ClearQueue(&q); + printf("Çå¿Õ¶ÓÁкó,q.front=%u q.rear=%u q.front->next=%u\n",q.front,q.rear,q.front->next); + DestroyQueue(&q); + printf("Ïú»Ù¶ÓÁкó,q.front=%u q.rear=%u\n",q.front, q.rear); + + return 0; +} + diff --git a/_02.栈与队列/_f.链队列.c b/_02.栈与队列/_f.链队列.c new file mode 100644 index 0000000..81e0030 --- /dev/null +++ b/_02.栈与队列/_f.链队列.c @@ -0,0 +1,106 @@ +#include "stdlib.h" +#include "stdio.h" +#include "io.h" + +#define MAXSIZE 20 +#define OK 1 +#define ERROR 0 +#define TRUE 1 +#define FALSE 0 + +typedef int Status; +typedef int ElemType; + +typedef struct QNode +{ + ElemType data; + struct QNode *next; +} QNode, *QueuePtr; + +typedef struct +{ + QueuePtr front, rear; +} LinkQueue; + +Status visit(ElemType e) +{ + printf("%d ", e); + return OK; +} + +Status InitQueue(LinkQueue *Q) +{ + Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode)); + if (!Q->front) + { + exit(-1); + } + Q->front->next = NULL; + return OK; +} + +Status Destroy(LinkQueue *Q) +{ + while (Q->front) + { + // Q->rearûɶÓþÍÄÃÀ´×÷ΪÁÙʱ±äÁ¿´æ´¢ºó¼Ì½Úµã + Q->rear = Q->front->next; + free(Q->front); + Q->front = Q->rear; + } + return OK; +} + +Status ClearQueue(LinkQueue *Q) +{ + // ÓëÏú»ÙÏà±È±£ÁôÁËfront½áµã + QueuePtr p, q; + Q->rear = Q->front; + p = Q->front->next; + Q->front->next = NULL; + while (p) + { + q = p; + p = p->next; + free(q); + } + return OK; +} + +Status QueueEmpty(LinkQueue Q) +{ + if (Q.front == Q.rear) + { + return TRUE; + } + return FALSE; +} + +int QueueLength(LinkQueue Q) +{ + int i = 0; + QueuePtr p; + p = Q.front; + while (Q.rear != p) + { + i++; + p = p->next; + } + return i; +} + +Status GetHead(LinkQueue Q, ElemType *e) +{ + QueuePtr p; + if (Q.rear == Q.front) + { + return ERROR; + } + p = Q.front->next; + *e = p->data; + return OK; +} + +Status EnQueue(LinkQueue *Q,ElemType e){ + Q +} \ No newline at end of file