数据结构之链式队列

//PreDef.h: 预定义常量和类型
//===================head.h====================================
//head.h
#ifndef _hhlisst_
#define _hhlisst_

#define TRUE        
#define FALSE        0
#define OK           1 //成功
#define ERROR        0 //失败(错误)
#define INFEASIBLE  -1 //不可行的
#define OVERFLOW -2 //(内存)溢出
#define UNDERFLOW   -4 //(数组下标)下溢

#define ENDELEM -9999 //序列结束元素(为方便演示而设)

typedef int QElemType;       //元素类型ElemType为int
typedef int Status;       //元素类型ElemType为int
#define LIST_INIT_SIZE 100  //顺序表初始容量(能容纳的元素个数) 
#define LISTINCREMENT   10  //容量增量



typedef struct QNode
 {
   QElemType data;
   QNode *next;
 }*QueuePtr;
 struct LinkQueue
 {
   QueuePtr front,rear; // 队头、队尾指针,没有*号
 };

#endif


//===============================================================


#include
#include
#include "headd.h"

 Status InitQueue(LinkQueue &Q)
 
// 构造一个空队列Q
if(!(Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode))))
exit(OVERFLOW);
Q.front->next=NULL;
return OK;
 }



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



 Status 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);
}
return OK;
 }



 Status QueueEmpty(LinkQueue Q)
 
// 若Q为空队列,则返回TRUE,否则返回FALSE
if(Q.front==Q.rear)
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(LinkQueue Q,QElemType &e)
 
// 若队列不空,则用e返回Q的队头元素,并返回OK,否则返回ERROR
QueuePtr p;
if(Q.front==Q.rear)
return ERROR;
p=Q.front->next;  //结构体嵌套
e=p->data;
return OK;
 }



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



 Status DeQueue(LinkQueue &Q,QElemType &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;
 }



 Status QueueTraverse(LinkQueue Q,void(*vi)(QElemType))
 { // 从队头到队尾依次对队列Q中每个元素调用函数vi()。一旦vi失败,则操作失败
   QueuePtr p;
   p=Q.front->next;
   while(p)
   {
     vi(p->data);
     p=p->next;
   }
   printf("\n");
   return OK;
 }
 



 void visit(QElemType i)
 {
printf("%d ",i);
 }


 
 void main()
 {
   int i;
   QElemType d;
   LinkQueue q;
   i=InitQueue(q);
   if(i)
     printf("成功地构造了一个空队列!\n");
   printf("是否空队列?%d(1:空 0:否)  ",QueueEmpty(q));
   printf("队列的长度为%d\n",QueueLength(q));
   EnQueue(q,-5);
   EnQueue(q,5);
   EnQueue(q,10);
   printf("插入3个元素(-5,5,10)后,队列的长度为%d\n",QueueLength(q));
   printf("是否空队列?%d(1:空 0:否)  ",QueueEmpty(q));
   printf("队列的元素依次为:");
   QueueTraverse(q,visit);
   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);
 }
posted @ 2014-05-08 10:19  dreamsyeah  阅读(335)  评论(0编辑  收藏  举报