队列再来一题
来源:互联网 发布:免费顶级域名申请 编辑:程序博客网 时间:2024/04/29 21:12
//3.13 假设以带头结点的循环链表表示队列,并且只设一个指针指向队尾元素站点(注意不设头指针) ,试编写相应的置空队、判队空 、入队和出队等算法。
//先定义链队结构:
#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"
typedef struct queuenode{
int data;
struct queuenode *next;
}QueueNode;//以上是结点类型的定义
typedef struct{
QueueNode *rear;
}LinkQueue; //只设一个指向队尾元素的指针
//(1)置空队
void InitQueue(LinkQueue *Q)
{//置空队:就是使头结点成为队尾元素
QueueNode *s;
s=(QueueNode*)malloc(sizeof(QueueNode));
Q->rear=Q->rear->next;//将队尾指针指向头结点
while(Q->rear!=(Q->rear->next))//当队列非空,将队中元素逐个出队
{
s=Q->rear->next;
Q->rear->next=s->next;
free(s);//回收结点空间
}
s=NULL;
}
//(2)判队空
int EmptyQueue(LinkQueue *Q)
{//判队空
//当头结点的next指针指向自己时为空队
return Q->rear->next->next==Q->rear->next;
}
//(3)入队
void EnQueue(LinkQueue *Q,int x)
{//入队
//也就是在尾结点处插入元素
QueueNode *p;
p=(QueueNode*)malloc(sizeof(QueueNode));//申请新结点
p->data=x;p->next=Q->rear->next;//初始化新结点并链入
Q->rear->next=p;
Q->rear=p;//将尾指针移至新结点
}
//(4)出队
int DeQueue(LinkQueue *Q)
{//出队,把头结点之后的元素摘下
int x;
QueueNode *p;
if(EmptyQueue(Q))
printf("Queue underflow");
p=Q->rear->next->next;//p指向将要摘下的结点
x=p->data;//保存结点中数据
if(p==Q->rear)
{//当队列中只有一个结点时,p结点出队后,要将队尾指针指向头结点
Q->rear=Q->rear->next;Q->rear->next=p->next;
}
else
Q->rear->next->next=p->next;//摘下结点p
free(p);//释放被删结点
return x;
}
int main()
{
LinkQueue *Q;
int i,e,n;
Q=(LinkQueue*)malloc(sizeof(LinkQueue));
Q->rear->next=(QueueNode*)malloc(sizeof(QueueNode));//这个语句得注意,十分关键,弄了一下午才弄出来
InitQueue(Q);
printf("input the number of the data:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the queue:");
scanf("%d",&e);
EnQueue(Q,e);
}
printf("input the number of the datas to out:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("output first data in the queue:");
DeQueue(Q);
printf("%d\n",Q->rear->data);
}
return 0;
}
//先定义链队结构:
#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"
typedef struct queuenode{
int data;
struct queuenode *next;
}QueueNode;//以上是结点类型的定义
typedef struct{
QueueNode *rear;
}LinkQueue; //只设一个指向队尾元素的指针
//(1)置空队
void InitQueue(LinkQueue *Q)
{//置空队:就是使头结点成为队尾元素
QueueNode *s;
s=(QueueNode*)malloc(sizeof(QueueNode));
Q->rear=Q->rear->next;//将队尾指针指向头结点
while(Q->rear!=(Q->rear->next))//当队列非空,将队中元素逐个出队
{
s=Q->rear->next;
Q->rear->next=s->next;
free(s);//回收结点空间
}
s=NULL;
}
//(2)判队空
int EmptyQueue(LinkQueue *Q)
{//判队空
//当头结点的next指针指向自己时为空队
return Q->rear->next->next==Q->rear->next;
}
//(3)入队
void EnQueue(LinkQueue *Q,int x)
{//入队
//也就是在尾结点处插入元素
QueueNode *p;
p=(QueueNode*)malloc(sizeof(QueueNode));//申请新结点
p->data=x;p->next=Q->rear->next;//初始化新结点并链入
Q->rear->next=p;
Q->rear=p;//将尾指针移至新结点
}
//(4)出队
int DeQueue(LinkQueue *Q)
{//出队,把头结点之后的元素摘下
int x;
QueueNode *p;
if(EmptyQueue(Q))
printf("Queue underflow");
p=Q->rear->next->next;//p指向将要摘下的结点
x=p->data;//保存结点中数据
if(p==Q->rear)
{//当队列中只有一个结点时,p结点出队后,要将队尾指针指向头结点
Q->rear=Q->rear->next;Q->rear->next=p->next;
}
else
Q->rear->next->next=p->next;//摘下结点p
free(p);//释放被删结点
return x;
}
int main()
{
LinkQueue *Q;
int i,e,n;
Q=(LinkQueue*)malloc(sizeof(LinkQueue));
Q->rear->next=(QueueNode*)malloc(sizeof(QueueNode));//这个语句得注意,十分关键,弄了一下午才弄出来
InitQueue(Q);
printf("input the number of the data:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the queue:");
scanf("%d",&e);
EnQueue(Q,e);
}
printf("input the number of the datas to out:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("output first data in the queue:");
DeQueue(Q);
printf("%d\n",Q->rear->data);
}
return 0;
}
0 0
- 队列再来一题
- 再来一首
- 再来一幅!!!
- FILTER再来一例
- 我宁可再来一遍。
- 红枣养气,再来一颗
- 再来一道概率题
- 再来
- 再来
- 心痛的感觉 重新再来一遍
- 心痛的感觉 重新再来一遍
- 心痛的感觉 重新再来一遍
- 心痛的感觉 重新再来一遍
- 心痛的感觉 重新再来一遍
- 心痛的感觉 重新再来一遍
- 心痛的感觉 重新再来一遍
- C++ primer从头再来(一)
- js再来一遍-灵活的分号
- js li内容a-z排序和搜索 拼音
- js 函数
- nginx php, mysql ,memcached源码安装总结。
- vijos P1062 迎春舞会之交谊舞
- [学习总结][基础篇]Spring(二)
- 队列再来一题
- 虚函数如何实现多态
- 关于ognl.OgnlException: target is null for setProperty
- 动作技能的处理思路
- 正合适的咖喱
- import BeautifulSoup for Python
- linux kernel 网络协议栈之GRO(Generic receive offload)
- Sublime Text 3中文乱码解决方法以及安装包管理器方法
- Android|Java 开发常用工具类整理