C语言《数据结构》——链式队列

系列文章目录

c语言中链式队列的概念和代码实现;

提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

链式队列----用链表实现,链式队列就是一个操作受限的单向链表;

提示:以下是本篇文章正文内容,下面案例可供参考

一、队列初始化;

LinkQueue* Init_LinkQueue()
{
LinkQueue* ps = (LinkQueue*)malloc(sizeof(LinkQueue));
ps->frontNode = ps->tailNode = NULL;
ps->cursize = 0;
return ps;
}

二、代码实现;

1.入队

代码如下(示例):
在这里插入图片描述

void pushQueue(LinkQueue* ps, elemstyle data)
{
	assert(ps != NULL);
	Node* newnode = creatNode(data);
	if (ps->cursize == 0)
	{
		ps->frontNode = ps->tailNode=newnode;
	}
	else
	{
		ps->tailNode->next = newnode;
		ps->tailNode == newnode;
	}
	ps->cursize++;
}

2.出队

代码如下(示例):
在这里插入图片描述

int popLinkQueue(LinkQueue* ps)
{
	assert(ps != NULL);
	if (ps->cursize == 0)
	{
		printf("队列为空,出队失败;\n");
	}
	else
	{
		Node* nextNode = ps->frontNode->next;
		free(ps->frontNode);
		ps->frontNode = nextNode;
		ps->cursize--;
		//return *item;
	}
}
//获取队头元素;
int getfrontQueue(LinkQueue* ps)
{
	assert(ps != NULL);
	elemstyle item;
	if (ps->frontNode != NULL)
	{
		item = ps->frontNode->data;
		printf("%5d\n", item);
	}
	//获取队头元素是不用删除结点的;
}

3.源代码实现:

该处使用的url网络请求的数据。

//链式队列;
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<Windows.h>
#include<assert.h>
#define maxsize 5
#define true 2
#define false -2
#define ok 1
#define erro -1
typedef int elemstyle;
typedef struct node 
{
	elemstyle data;
	struct node* next;
}Node;
typedef struct queue 
{
	Node* frontNode;
	Node* tailNode;
	int cursize;
}LinkQueue;
//创建结点;
Node* creatNode(elemstyle data)
{
	Node* newnode = (Node*)malloc(sizeof(Node));
	newnode->data = data;
	newnode->next = NULL;
	return newnode;
}
//链表的初始化;
LinkQueue* Init_LinkQueue()
{
	LinkQueue* ps = (LinkQueue*)malloc(sizeof(LinkQueue));
	ps->frontNode = ps->tailNode = NULL;
	ps->cursize = 0;
	return ps;
}
//入队;
void pushQueue(LinkQueue* ps, elemstyle data)
{
	assert(ps != NULL);
	Node* newnode = creatNode(data);
	if (ps->cursize == 0)
	{
		ps->frontNode = ps->tailNode=newnode;
	}
	else
	{
		ps->tailNode->next = newnode;
		ps->tailNode == newnode;
	}
	ps->cursize++;
}
//出队;
int popLinkQueue(LinkQueue* ps)
{
	assert(ps != NULL);
	if (ps->cursize == 0)
	{
		printf("队列为空,出队失败;\n");
	}
	else
	{
		Node* nextNode = ps->frontNode->next;
		free(ps->frontNode);
		ps->frontNode = nextNode;
		ps->cursize--;
		//return *item;
	}
}
//判空;
int emptyLinkQueue(LinkQueue* ps)
{
	assert(ps != NULL);
	if (ps->cursize == 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}
//获取队头元素;
int getfrontQueue(LinkQueue* ps)
{
	assert(ps != NULL);
	elemstyle item;
	if (ps->frontNode != NULL)
	{
		item = ps->frontNode->data;
		printf("%5d\n", item);
	}
	//获取队头元素是不用删除结点的;
}
//获取队列长度;
void lengthQueue(LinkQueue* ps)
{
	assert(ps != NULL);
	printf("队列的长度为:%5d\n", ps->cursize);
}
//队列打印函数;
void printQueue(LinkQueue* ps)
{
	assert(ps != NULL);
	//elemstyle item;
	if (ps->cursize = 0)
	{
		printf("链表为空,无法打印;\n");
		return;
	}
	while (ps->cursize != 0)
	{
		//获取队头元素;
		getfrontQueue(ps);
		//出队;
		popLinkQueue(ps);
	}
}
//调试函数;
int main(void)
{
	LinkQueue *ps;
	elemstyle item;
	//队列初始化;
	ps=Init_LinkQueue();
	int data;
	for (int i = 0; i < maxsize; i++)
	{
		scanf("%d", &data);
		pushQueue(ps, data);
	}
	//获取队头元素;
	printf("队头元素为:");
	getfrontQueue(ps);
	//获取队列长度;
	lengthQueue(ps);
	//队列打印函数;
     printQueue(ps);
}

总结

提示:这里对文章进行总结:

队列和链表结构相似,作为一种数据存储结构,要熟练掌握他的性质,先进先出的特点在很多的方面会有应用。下一期我会更新顺序队列,有需要的可以留意一下,c语言上有问题的可以留言也可加V,虽然我才大一,但是在我能力范围内的我都会回答的。我们共同进步;
V:16655560651;

  • 12
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 14
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心随而动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值