博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
链队列的实现
阅读量:4137 次
发布时间:2019-05-25

本文共 1243 字,大约阅读时间需要 4 分钟。

 #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;
}

 

 

转载地址:http://pcmvi.baihongyu.com/

你可能感兴趣的文章
php程序员看过来,这老外是在吐糟你吗?看看你中了几点!
查看>>
为什么说程序员是“培训班出来的”就是鄙视呢?
查看>>
码农吐糟同事:写代码低调点不行么?空格回车键与你有仇吗?
查看>>
阿里p8程序员四年提交6000次代码的确有功,但一次错误让人唏嘘!
查看>>
一道技术问题引起的遐想,最后得出结论技术的本质是多么的朴实!
查看>>
985硕士:非科班自学编程感觉还不如培训班出来的,硕士白读了?
查看>>
你准备写代码到多少岁?程序员们是这么回答的!
查看>>
码农:和产品对一天需求,产品经理的需求是对完了,可我代码呢?
查看>>
程序员过年回家该怎么给亲戚朋友解释自己的职业?
查看>>
技术架构师的日常工作是什么?网友:搭框架,写公共方法?
查看>>
第四章 微信飞机大战
查看>>
九度:题目1008:最短路径问题
查看>>
九度Online Judge
查看>>
九度:题目1027:欧拉回路
查看>>
九度:题目1012:畅通工程
查看>>
九度:题目1017:还是畅通工程
查看>>
九度:题目1034:寻找大富翁
查看>>
第六章 背包问题——01背包
查看>>
第七章 背包问题——完全背包
查看>>
51nod 分类
查看>>