队列

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

#define ok 1
#define error 0
#define true 1
#define false 0

typedef struct qnode
{
 int data;
 struct qnode *next;
}qnode,*linkqueptr;
typedef struct
{
 linkqueptr front,rear;
}linkque;
int initque(linkque *q)
{
 q->front=q->rear=(linkqueptr)malloc(sizeof(qnode));
 if(!q->front)
  {
  return error;
  }
 else
  q->front->next=NULL;
 return ok;
}
int linkqueempty(linkque q)
{
 if(q.front==q.rear)
  return true;
 else
  return false;
}
int linkquelength(linkque q)
{
 int i=0;
 linkqueptr p;
 p=q.front;
 while(q.rear!=p)
 {
  i++;
  p=p->next;
 }
 return i;
}
int enlinkque(linkque *q,int e)
{
 linkqueptr s=(linkqueptr)malloc(sizeof(qnode));
 if(!s)
 {
  return error;
 }
 else
 {
  s->data=e;
  s->next=NULL;
  q->rear->next=s;
  q->rear=s;
  return ok;
 }
}
int linkquetraverse(linkque q)
{
 linkqueptr p;
 p=q.front->next;
 while(p)
 {
  visit(p->data);
  p=p->next;
 }
 printf("\n");
 return ok;
}
int visit(int e)
{
 printf("%3d",e);
 return ok;
}
int gethead(linkque q,int *e)
{
 linkqueptr p;
 if(q.front==q.rear)
 {
  return error;
 }
 else
 {
 p=q.front->next;
 *e=p->data;
 return ok; 
 }
}
int outlinkque(linkque *q,int *e)   //(*q).front等于q->front  结构体变量.成员名
         //q.front 错误,因为q是指向结构体的指针,而非结构体变量
{
 linkqueptr p;
 if(q->front==q->rear)
 {
  return error;
 }
 else
 {
 p=q->front->next;
 *e=p->data;
 q->front->next=p->next;   //将原队头结点的后继p->next(即第一个结点的后继)赋给头结点的后继
         //头结点即链队列第一点,但无数据,只是链队列的起始指针。
 if(q->rear==p)           //若队头结点的下一个元素就是队尾,即原队除了头结点只有一个有效结点。
  {
   q->rear=q->front;    //让队尾指向对头,即都指向链队列的头结点。
  }
 free(p);
 return ok; 
 }
}
int clearlinkque(linkque *q)
{
 linkqueptr x,y;
 q->rear=q->front;
 x=q->front->next;
 q->front->next=NULL;
 while(x)
 {
  y=x;
  x=x->next;
  free(y);
 }
 return ok;
  
}
int destroylinkque(linkque *q)    //销毁队列,即收回头结点的内存
{
 while(q->front)              
 {
  q->rear=q->front->next;   //让队列的队尾指针指向NULL。
  free(q->front);           //释放队列的头结点内存。
  q->front=q->rear;         //让队列的队头指向空。
 }
 return ok;
 
}
int main(int argc, char *argv[])
{
 int i;
 int d;
 linkque q;
 i=initque(&q);
 if(i)
 {
  printf("成功构造了一个空队列!\n");
 }
 printf("是否空队列?%d(1;空,0:否)\n",linkqueempty(q));
 printf("队列的长度为%d\n",linkquelength(q));
 enlinkque(&q,5);
 enlinkque(&q,10);
 enlinkque(&q,15);
 printf("插入3个元素(5,10,15)后,队列的长度为%d\n",linkquelength(q));
 printf("是否空队列?%d(1:空 0:否) \n ",linkqueempty(q));
 printf("队列的元素依次为:");
 linkquetraverse(q);
 i=gethead(q,&d);
 if(i==ok)
 printf("队头元素是:%d\n",d);
 outlinkque(&q,&d);
 printf("删除了队头元素%d\n",d);
 i=gethead(q,&d);
 if(i==ok)
 printf("新的队头元素是:%d\n",d);
 clearlinkque(&q);
 printf("清空队列后,q.front=%u q.rear=%u q.front->next=%u\n",q.front,q.rear,q.front->next); //%u为无符号短整型
 destroylinkque(&q);
 printf("销毁队列后,q.front=%u q.rear=%u\n",q.front, q.rear);
 return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值