链队列的实现

 #include <stdio.h>
#include <malloc.h>
typedef struct Qnode{
 int data;
 struct Qnode *next;
}Qnode,*QueuePtr;//创建链
typedef struct {
 QueuePtr front;//队头指针
 QueuePtr rear;//队尾指针
}LinkQueue;//创建队列

void EnQueue(LinkQueue &Q,int e)
{
 QueuePtr p=(QueuePtr)malloc(sizeof(Qnode));
 p->data=e;p->next=NULL;
 Q.rear->next=p;
 Q.rear=p;
}//插入e为队尾元素

void DeQueue(LinkQueue &Q, int &e)
{
 if(Q.front==Q.rear)  printf("error/n");
 QueuePtr p=Q.front->next;
 e=p->data;
 Q.front->next=p->next;
 if(Q.rear==p) Q.rear=Q.front;
 free(p);
}//删除队头元素

int QueueEmpty(LinkQueue Q)
{
 if(Q.front==Q.rear) return 1;
 else return 0;
}//判断链栈是否为空

void InitQueue(LinkQueue &Q)
{
 Q.front=Q.rear=(QueuePtr)malloc(sizeof(Qnode));
 Q.front->next=NULL;
 printf("输入6个队列元素:");
 for(int i=0;i<6;i++)
 {
  int a;
  scanf("%d",&a);
  EnQueue(Q,a);//向队列中插元素
 }
}//初始化栈

 


void Print(LinkQueue Q)
{
 while(!QueueEmpty(Q))
 {
  int e;
  DeQueue(Q,e);
  printf("%d ",e);
 }
}//输出栈元素

void DestroyQueue(LinkQueue &Q)
{
 while(Q.front)
 {
  Q.rear=Q.front->next;
  free(Q.front);
  Q.front=Q.rear;
 }
}//销毁链队列

int QueueLength(LinkQueue Q)
{
 int j=0;
 while(Q.front->next)
 {
  Q.front=Q.front->next;
  j++;
 }
 return j;
}//求链栈长度

main()
{
   int a;//存储链队列长度
   LinkQueue Q;
   InitQueue(Q);
   a=QueueLength( Q);
   Print (Q);
   printf("链队列长%d",a);
   return 0;
}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值