链队操作—C语言实现

该代码示例展示了如何使用C语言创建一个链式队列结构,并实现初始化、入队、出队、遍历和计算队列长度的功能。用户可以输入数据直到输入-1为止,程序会进行相应的队列操作并打印结果。
摘要由CSDN通过智能技术生成
#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);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值