当前位置: 首页 > 工具软件 > queue-fun > 使用案例 >

queue-c链式队列

公西志文
2023-12-01

 /*@file LinkQueuetest.c*/
/*mingGW compiled ok*/
/*链式单向队列*/

typedef int ElemType;
#include "allhead.h"
#include "queue.h"
#include "fun.h"

void main()
{
   int i;
   ElemType d;
   LinkQueue q;
   printf("1.构造一个空队列!/n");
   InitQueue(&q);
   printf("2.插入3个元素(-5,5,10)");
   EnQueue(&q,-5);
   EnQueue(&q,5);
   EnQueue(&q,10);
   printf("3.为空队列?%d(1:空 0:否)/n",QueueEmpty(q));
   printf("4.队列的长度?%d/n",QueueLength(q));
   printf("5.遍历队列:");
   QueueTraverse(q,print);
   i=GetHead_Q(q,&d);
   if(i==OK) printf("6.队头元素是:%d/n",d);
   DeQueue(&q,&d);
   printf("7.删除队头元素%d/n",d);
   i=GetHead_Q(q,&d);
   if(i==OK)  printf("8.队头元素:%d/n",d);
   ClearQueue(&q);
   printf("8.清空队列后,q.front=%u q.rear=%u q.front->next=%u/n",q.front,q.rear,q.front->next);
   DestroyQueue(&q);
   printf("9.销毁队列后,q.front=%u q.rear=%u",q.front, q.rear);
 }

/**************************************************************/

运行结果

1.构造一个空队列!
2.插入3个元素(-5,5,10)3.为空队列?0(1:空 0:否)
4.队列的长度?3
5.遍历队列:-5 5 10
6.队头元素是:-5
7.删除队头元素-5
8.队头元素:5
8.清空队列后,q.front=4602864 q.rear=4602864 q.front->next=0
9.销毁队列后,q.front=0 q.rear=0

/**************************************************************/
#include "allhead.h"
#include "fun.h"
省略它们,内容详见
http://blog.csdn.net/chinayaosir/archive/2008/09/20/2956091.aspx
/**************************************************************/

/*@file Queue.h*/
/*mingGW compiled ok*/
/*链式单向队列*/

 typedef struct QNode
 {
   ElemType data;
   struct QNode *next;
 }QNode,*QueuePtr;

 typedef struct
 {
   QueuePtr front,rear; /* 队头、队尾指针 */
 }LinkQueue;


 /* 链式队列(存储结构由c3-2.h定义)的基本操作(9个) */
 void InitQueue(LinkQueue *Q)
 { /* 构造一个空队列Q */
   (*Q).front=(*Q).rear=(QueuePtr)malloc(sizeof(QNode));
   if(!(*Q).front)
     exit(OVERFLOW);
   (*Q).front->next=NULL;
 }

 void DestroyQueue(LinkQueue *Q)
 { /* 销毁队列Q(无论空否均可) */
   while((*Q).front)
   {
     (*Q).rear=(*Q).front->next;
     free((*Q).front);
     (*Q).front=(*Q).rear;
   }
 }

 void ClearQueue(LinkQueue *Q)
 { /* 将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);
   }
 }

 Status QueueEmpty(LinkQueue Q)
 { /* 若Q为空队列,则返回TRUE,否则返回FALSE */
   if(Q.front->next==NULL)
     return TRUE;
   else
     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_Q(LinkQueue Q,ElemType *e) 
 { /* 若队列不空,则用e返回Q的队头元素,并返回OK,否则返回ERROR */
   QueuePtr p;
   if(Q.front==Q.rear)
     return ERROR;
   p=Q.front->next;
   *e=p->data;
   return OK;
 }

 void EnQueue(LinkQueue *Q,ElemType e)
 { /* 插入元素e为Q的新的队尾元素 */
   QueuePtr p=(QueuePtr)malloc(sizeof(QNode));
   if(!p) /* 存储分配失败 */
     exit(OVERFLOW);
   p->data=e;
   p->next=NULL;
   (*Q).rear->next=p;
   (*Q).rear=p;
 }

 Status DeQueue(LinkQueue *Q,ElemType *e)
 { /* 若队列不空,删除Q的队头元素,用e返回其值,并返回OK,否则返回ERROR */
   QueuePtr p;
   if((*Q).front==(*Q).rear)
     return ERROR;
   p=(*Q).front->next;
   *e=p->data;
   (*Q).front->next=p->next;
   if((*Q).rear==p)
     (*Q).rear=(*Q).front;
   free(p);
   return OK;
 }

 void QueueTraverse(LinkQueue Q,void(*vi)(ElemType))
 { /* 从队头到队尾依次对队列Q中每个元素调用函数vi() */
   QueuePtr p;
   p=Q.front->next;
   while(p)
   {
     vi(p->data);
     p=p->next;
   }
   printf("/n");
 }

 

 类似资料: