队列(单链队列和循环队列)

和线性表类似,队列也有两种存储表示:

链队列:为操作方便,给链队列添加一个头结点

队列的链式存储结构:

typedef struct Qnode{
    int data;
    struct Qnode *next;
}Qnode, *queueptr;
typedef struct {
    queueptr front;  队头指针
    queueptr rear;队尾指针
}Linkqueue;
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<conio.h>
 4 typedef struct Qnode{
 5     int data;
 6     struct Qnode *next;
 7 }Qnode, *queueptr;
 8 typedef struct {
 9     queueptr front;
10     queueptr rear;
11 }Linkqueue;
12 
13 void initqueue(Linkqueue &Q){
14     Q.front=Q.rear=(queueptr)malloc(sizeof(Qnode));
15     if(!Q.front)  exit(0);
16     Q.front->next=NULL;
17 }//初始化队列
18 void destroy(Linkqueue &Q){
19     while(Q.front){
20         Q.rear=Q.front->next;
21         free(Q.front);
22         Q.front=Q.rear;
23     }
24 }// 销毁队列
25 void enter(Linkqueue &Q,int value){
26     queueptr p=(queueptr)malloc(sizeof(Qnode));
27     p->data=value;
28     p->next=NULL;
29     Q.rear->next=p;     // 表示有头结点
30     Q.rear=p;
31 }
32 void dequeue(Linkqueue &Q,int &value){
33     if(Q.front==Q.rear)   exit(0);
34     queueptr p;
35     p=Q.front->next;
36     value=p->data;
37     Q.front->next=p->next;
38     if(Q.rear==p)   Q.rear=Q.front;
39     free(p);
40 }   //出队列
41 //队列元素个数
42 void getlength (Linkqueue Q,int &length){
43     length=0;
44     queueptr p=Q.front;
45     while(p!=Q.rear){
46         p=p->next;
47         length++;
48     }
49 }
50 int main(){
51     Linkqueue Q;
52     initqueue(Q);
53     int e;
54     int n;
55     scanf("%d",&n);
56     while(n){
57         scanf("%d",&e);
58         enter(Q,e);
59         n--;}
60     while(Q.rear!=Q.front){
61         dequeue(Q,e);
62         printf("%d  ",e);}
63     destroy(Q);
64 }

循环队列:附设两个指针front和rear分别指示队列头元素及尾元素的位置,每当插入新的队尾元素是,尾指针加1;每当删除队列头元素是,头指针加1             如果用循环队列,则必须设定一个最大队列长度;若无法确定最大长度,则宜采用链队列。

循环队列类型:

typedef struct {
  int *base;
  int front;
  int rear;
}sqqueue;

#include<stdio.h>
#include<stdlib.h>
typedef struct {
  int *base;
  int front;
  int rear;
}sqqueue;

void initqueue(sqqueue &Q){
 Q.base=(int *)malloc(100*sizeof(int ));
 if(!Q.base)  exit(0);
 Q.front=Q.rear=0;
 }
 void enqueue(sqqueue &Q,int elem){
   // 队列为空时 1%100==1, 队列满时(99+1)%100==0, 最多容纳99个元素
   if((Q.rear+1)%100==(Q.rear) )  exit;
   Q.base[Q.rear]=elem;
   Q.rear=(Q.rear+1)%100;  // rear始终在0-100中循环

 }
 void outqueue(sqqueue &Q,int &e){
   if(Q.front==Q.rear)  exit(0);
   e=Q.base[Q.front];
   Q.front=(Q.front+1)%100;
 }
 void printqueue(sqqueue Q){
 printf("the queue is:\n");
  for(int i=Q.front;i<Q.rear;i++)   printf("%d",Q.base[i]);
 }
 int main(){
   sqqueue Q;
   int e,i;
   initqueue(Q);
   printf("input the number:");
   scanf("%d",&i);

   while(i){  enqueue(Q,e);
   i--;}
   printqueue(Q);
   printf("\noutput:");  // 输入要输出队列的个数
   scanf("%d",&i);
   while(i!=0){
    outqueue(Q,e);
    printf("%d",e);
    i--;
   }
   printqueue(Q);
 }

 



 

转载于:https://www.cnblogs.com/xiaoqiz/p/10680051.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值