#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)
struct Qnode {
int data;
struct Qnode* next;
};
struct Queue {
struct Qnode* front;
struct Qnode* rear;
};
void Init(struct Queue* Q); //初始化队列
void EnQueue(struct Queue* Q,int e); //入队
int DeQueue(struct Queue* Q); //出队
void Print(struct Queue Q); //遍历
void Size(struct Queue Q); //求队列长度
int main()
{
int e = 0,data=0;
struct Queue Q;
Init(&Q);
while (e!=-1) //输入数据为-1,结束入队
{
printf("请输入入队数据\n");
scanf("%d", &e);
if (e != -1)
{
EnQueue(&Q, e);
}
}
data = DeQueue(&Q); //出队
printf("出队元素为==== %d \n",data);
Print(Q);
Size(Q);
return 0;
}
void Init(struct Queue* Q)
{
struct Qnode* p = NULL;
p = (struct Qnode*)malloc(sizeof(struct Qnode));
p->next = NULL;
Q->front = p;
Q->rear = p;
printf("队列初始化成功\n");
}
void EnQueue(struct Queue* Q,int e)
{
struct Qnode* p=NULL;
p = (struct Qnode*)malloc(sizeof(struct Qnode));
p->data = e;
p->next = NULL;
Q->rear->next = p;
Q->rear = p;
printf("入队成功\n");
}
int DeQueue(struct Queue* Q)
{
int e = 0;
struct Qnode* p = NULL;
if (Q->front == Q->rear)
{
printf("队列为空\n");
}
else
{
p = Q->front->next;
Q->front->next = p->next; //注意这个
if (Q->rear == p) //如果p指向了最后一个元素
{
Q->rear = Q->front; //删除最后一个元素
}
e = p->data;
free(p);
}
return e;
}
void Print(struct Queue Q)
{
struct Qnode* p = Q.front->next;
if (Q.front == Q.rear)
{
printf("链队为空\n");
}
else
{
while (p)
{
printf("队中元素==== %d \n", p->data);
p = p->next;
}
}
}
void Size(struct Queue Q)
{
int count = 0;
struct Qnode* p = Q.front->next;
while (p)
{
count++;
p = p->next;
}
printf("队列长度==== %d \n", count);
}```
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)
struct Qnode {
int data;
struct Qnode* next;
};
struct Queue {
struct Qnode* front;
struct Qnode* rear;
};
void Init(struct Queue* Q); //初始化队列
void EnQueue(struct Queue* Q,int e); //入队
int DeQueue(struct Queue* Q); //出队
void Print(struct Queue Q); //遍历
void Size(struct Queue Q); //求队列长度
int main()
{
int e = 0,data=0;
struct Queue Q;
Init(&Q);
while (e!=-1) //输入数据为-1,结束入队
{
printf("请输入入队数据\n");
scanf("%d", &e);
if (e != -1)
{
EnQueue(&Q, e);
}
}
data = DeQueue(&Q); //出队
printf("出队元素为==== %d \n",data);
Print(Q);
Size(Q);
return 0;
}
void Init(struct Queue* Q)
{
struct Qnode* p = NULL;
p = (struct Qnode*)malloc(sizeof(struct Qnode));
p->next = NULL;
Q->front = p;
Q->rear = p;
printf("队列初始化成功\n");
}
void EnQueue(struct Queue* Q,int e)
{
struct Qnode* p=NULL;
p = (struct Qnode*)malloc(sizeof(struct Qnode));
p->data = e;
p->next = NULL;
Q->rear->next = p;
Q->rear = p;
printf("入队成功\n");
}
int DeQueue(struct Queue* Q)
{
int e = 0;
struct Qnode* p = NULL;
if (Q->front == Q->rear)
{
printf("队列为空\n");
}
else
{
p = Q->front->next;
Q->front->next = p->next; //注意这个
if (Q->rear == p) //如果p指向了最后一个元素
{
Q->rear = Q->front; //删除最后一个元素
}
e = p->data;
free(p);
}
return e;
}
void Print(struct Queue Q)
{
struct Qnode* p = Q.front->next;
if (Q.front == Q.rear)
{
printf("链队为空\n");
}
else
{
while (p)
{
printf("队中元素==== %d \n", p->data);
p = p->next;
}
}
}
void Size(struct Queue Q)
{
int count = 0;
struct Qnode* p = Q.front->next;
while (p)
{
count++;
p = p->next;
}
printf("队列长度==== %d \n", count);
}
链队操作—C语言实现
最新推荐文章于 2024-09-01 17:30:24 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)