队列的实现

一、队列的定义:

队列是一种先进先出的线性表。它只允许在表的一端进行插入,而在另一端删除元素。象日常生活中的排队,最早入队的最早离开。

在队列中,允许插入的的一端叫队尾,允许删除的一端则称为队头

抽象数据类型队列:

ADT Queue{

数据对象: D={ai| ai(-ElemSet,i=1,2,...,n,n>=0}

数据关系: R1={<ai-1,ai> | ai-1,ai(- D,i=2,...,n}

基本操作:

InitQueue(&Q) 构造一个空队列Q

Destroyqueue(&Q) 队列Q存在则销毁Q

ClearQueue(&Q) 队列Q存在则将Q清为空队列

QueueEmpty(Q) 队列Q存在,若Q为空队列则返回TRUE,否则返回FALSE

QueueLenght(Q) 队列Q存在,返回Q的元素个数,即队列的长度

GetHead(Q,&e) Q为非空队列,用e返回Q的队头元素

EnQueue(&Q,e) 队列Q存在,插入元素e为Q的队尾元素

DeQueue(&Q,&e) Q为非空队列,删除Q的队头元素,并用e返回其值

QueueTraverse(Q,vivsit()) Q存在且非空,从队头到队尾,依次对Q的每个数据元素调用函数visit()。一旦visit()失败,则操作失败

}ADT Queue

二、链队列-队列的链式表示和实现

用链表表示的队列简称为链队列。一个链队列显然需要两个分别指示队头和队尾的指针。

三、代码实现

 

#include <stdio.h>
#include <stdlib.h>

#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -1
#define OK 1

typedef int Status ;
//存储表示
struct STU{
  char name[20];
  char stuno[10];
  int age;
  int score;
};
typedef struct STU QElemType;
typedef struct QNode{
  QElemType data;
  struct QNode *next;
}QNode,*QueuePtr;

typedef struct{
  QueuePtr front;
  QueuePtr rear;
}LinkQueue;

//操作说明
Status InitQueue(LinkQueue *Q);
//构造一个空队列Q
Status Destroyqueue(LinkQueue Q);
//队列Q存在则销毁Q
Status ClearQueue(LinkQueue *Q);
//队列Q存在则将Q清为空队列
Status QueueEmpty(LinkQueue *Q);
// 队列Q存在,若Q为空队列则返回TRUE,否则返回FALSE
Status QueueLenght(LinkQueue *Q);
// 队列Q存在,返回Q的元素个数,即队列的长度
Status GetHead(LinkQueue Q,QElemType *e);
//Q为非空队列,用e返回Q的队头元素
Status EnQueue(LinkQueue *Q,QElemType e);
//队列Q存在,插入元素e为Q的队尾元素
Status DeQueue(LinkQueue Q,QElemType *e);
//Q为非空队列,删除Q的队头元素,并用e返回其值
Status QueueTraverse(LinkQueue *Q);
//Q存在且非空,从队头到队尾,依次对Q的每个数据元素调用函数visit()。一旦visit()失败,则操作失败
//操作的实现

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

Status Destroyqueue(LinkQueue Q) {
  //队列Q存在则销毁Q
  while(Q.front){
    Q.rear=Q.front->next;
    free(Q.front);
    Q.front=Q.rear;
  }
  return OK;
}

Status EnQueue(LinkQueue *Q,QElemType e) {
  QueuePtr p;    
  //队列Q存在,插入元素e为Q的队尾元素
  p=(QueuePtr)malloc(sizeof(QNode));
  if(!p) exit(OVERFLOW);
  p->data=e;p->next=NULL;
  /*printf("%s",(p->data).name);*/
  Q->rear->next=p;
  Q->rear=p;
  return OK;
}

Status DeQueue(LinkQueue Q,QElemType *e) {
  QueuePtr p;    
  //Q为非空队列,删除Q的队头元素,并用e返回其值
  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 ClearQueue(LinkQueue *Q) {
  if(Q->front==Q->rear)return ERROR;
  Q->front=Q->rear=NULL;       
}    

Status QueueEmpty(LinkQueue *Q) {
  if(Q->front==Q->rear)
    return FALSE;
  else
    return OK;      
}    

Status QueueLenght(LinkQueue *Q) {
  QueuePtr p;    
  int i;
  i=0;
  if(Q->front==Q->rear) return ERROR;
  else
  {
    p=Q->front; 
    while (p!=Q->rear) {  
      p=p->next; 
      ++i;
    } 
  }   
  return i;      
}  

Status GetHead(LinkQueue Q,QElemType *e) {
  QueuePtr p;         
  if(Q.front==Q.rear) return ERROR;
  else
  {
    p=Q.front->next;  
    *e=p->data;
  } 
}   

Status QueueTraverse(LinkQueue *Q) {
  QueuePtr p;    
  p=Q->front->next;    
  while(p!=Q->rear->next)
  { 
     printf("%s  %s  %d  %d/n",(p->data).name,(p->data).stuno,(p->data).age,(p->data).score);   
     p=p->next;
  }  
}              

int main()
{
  QElemType e;
  LinkQueue *Queue;
  int i;

  /*clrscr();*/

  printf("/n/n-------------------SqStack Demo is running...----------------/n/n");
  printf("First is IniQueue function./n");
  InitQueue(Queue);

  strcpy(e.name,"stu1");
  strcpy(e.stuno,"100001");
  e.age=80;
  e.score=1000;
  EnQueue(Queue,e);
  i=QueueLenght(Queue);
  printf("length is %d/n",i);
 
  strcpy(e.name,"stu3");
  strcpy(e.stuno,"100002");
  e.age=80;
  e.score=1000;
  EnQueue(Queue,e);
  i=QueueLenght(Queue);
  printf("length is %d/n",i);
  QueueTraverse(Queue);
  system("PAUSE"); 
  return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值