数据结构——队列(c语言版)

41 篇文章 0 订阅
17 篇文章 0 订阅

队列基本概念

  • 队列是一种先进先出(FIFO)特殊的线性表,仅在线性表的两端操作,不允许在中间操作。
  • 对头(front):取出元素的一端
  • 队尾(rear):插入元素的一端

线性队列例子:

  • 依据单链表改造即可。
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<stdlib.h>
#include<windows.h>

typedef struct Node
{
	//定义连接链表的节点:
	Node* next;
}node;

typedef struct Common_list
{
	//定义一个空间域
	node head;
	int length;
}common_list;


//1. 建立空链表:
common_list* creat_list()
{
	common_list*temp_creat;
	temp_creat = (common_list*)malloc(sizeof(common_list));
	if (temp_creat == nullptr)
	{
		printf("空间开辟错误!!!\n");
		return nullptr;
	}
	printf("已成功创建空列表!!1\n");
	temp_creat->head.next = nullptr;//双向链表
	temp_creat->length = 0;
	printf("空列表初始化成功!!1\n");
	return temp_creat;
}

//2. 开始在old中的pos位置处插入元素input:
common_list* insert_list(common_list**old, node*input, int pos)
{
	common_list*temp_insert = *old;
	if (temp_insert == nullptr)
	{
		printf("传入空间错误!!!\n\n");
		return nullptr;
	}
	else if (pos < 0)
	{
		printf("位置号不能小于0");
		return nullptr;
	}

	else if (pos > temp_insert->length)
	{
		pos = temp_insert->length;
	}
	//定义一个辅助指针:
	node*current = &temp_insert->head;
	for (int i = 0; i < pos; i++)
	{
		//开始移动到插入的位置:
		current = current->next;
	}
	//进行插入操作:
	input->next = current->next;
	current->next = input;
	temp_insert->length++;
	printf("号码入队成功!!!\n\n");
	return temp_insert;
}

//查找pos处的元素:
node* find_list(common_list*old, int pos)
{
	common_list*temp_find = old;
	if (temp_find == nullptr)
	{
		printf("传入空间错误!!!\n\n");
		return nullptr;
	}
	else if (pos < 0 || pos >= temp_find->length)
	{
		printf("该位置处无号码!!!\n\n");
		return nullptr;
	}

	//定义一个辅助指针便于寻找:
	node* current = temp_find->head.next;
	for (int i = 0; i < pos; i++)
	{
		current = current->next;
	}
	printf("找到号码啦!!!\n\n");
	return current;
}

//定义删除函数:
node* delete_list(common_list**old, int pos)
{
	common_list*temp_del = *old;
	if (temp_del == nullptr)
	{
		printf("传入空间错误!!!\n\n");
		return NULL;
	}
	else if (pos < 0 || pos >= temp_del->length)
	{
		printf("该位置处无号码!!!\n\n");
		return NULL;
	}

	//定义一个辅助指针定位当前位置:
	node* current = &temp_del->head;
	for (int i = 0; i < pos; i++)
	{
		current = current->next;
	}
	//定义一个辅助指针用于缓存要删除的节点:
	node*del = current->next;
	current->next = del->next;
	temp_del->length--;
	printf("数据出队成功!!!\n\n");
	return del;
}

void clear_list(common_list**old)
{
	common_list* temp_cls = *old;
	if (temp_cls == nullptr)
	{
		printf("传入空间错误!!!\n\n");
		return;
	}
	free(temp_cls);
	temp_cls = nullptr;
	printf("数据已经清空!!!\n\n");
}

int length_list(common_list*old)
{
	return old->length;
}

typedef struct user
{
	node head;
	void* user_data;
}user;

common_list* creat_queue()
{
	common_list* old_queue = creat_list();
	return old_queue;
}

int length_queue(common_list*old_queue)
{
	return old_queue->length;
}

void insert_queue_rear(common_list**old_queue,void*user_data)
{
	common_list*temp_insert = *old_queue;
	if (temp_insert == nullptr || user_data == nullptr)
	{
		printf("原队列为空!!!或用户数据为空!!!\n\n");
		return;
	}

	user* input = (user*)malloc(sizeof(user));
	if (input == nullptr)
	{
		printf("用户空间开辟错误!!!\n");
	}
	input->user_data = user_data;

	common_list*temp = insert_list(&temp_insert,(node*)input,length_queue(temp_insert));
	if (temp == nullptr)
	{
		printf("插入错误!!!\n\n");
		return;
	}
	printf("元素【%d】入队成功!!!\n\n", *(int*)input->user_data);
}

user* find_queue_front(common_list*old_queue)
{
	printf("正在查找队头元素!!!\n\n");
	common_list*temp_front = old_queue;
	user*find = (user*)find_list(temp_front,0);
	if (find == nullptr)
	{
		printf("查找失败!!!\n\n");
		return nullptr;
	}
	return find;
}

void delete_queue_front(common_list**old_queue)
{
	common_list*temp_del = *old_queue;
	if (temp_del == nullptr )
	{
		printf("原队列为空!!!\n\n");
		return;
	}

	user*temp = (user*)delete_list(&temp_del,0);
	void*buf = temp->user_data;
	if (buf == nullptr)
	{
		printf("数据为空!!!\n\n");
		return ;
	}
	printf("元素【%d】出队成功!!!\n\n",*(int*)buf);
	
}

void cls_queue(common_list**old_queue)
{
	common_list*temp_cls = *old_queue;
	if (temp_cls == nullptr)
	{
		printf("原队列为空!!!\n\n");
		return;
	}
	while (length_queue(temp_cls)>0)
	{
		printf("队列中有元素——正在出队!!!\n\n");
		delete_queue_front(&temp_cls);
	}
	printf("元素出队完成,队列为空!!!\n\n");
}

void auto_show_queue(common_list*old_queue)
{
	user*u1;
	for (int i = 0; i < length_queue(old_queue); i++)
	{
		u1 = find_queue_front(old_queue);
		printf("号码为:【%d】\n\n", *(int*)u1->user_data);
	}
}

int main()
{
	common_list* old_queue = creat_queue();

	int i = 111;
	insert_queue_rear(&old_queue, &i);

	int i1 = 222;
	insert_queue_rear(&old_queue, &i1);

	int i2 =333 ;
	insert_queue_rear(&old_queue, &i2);

	int i4 = 444;
	insert_queue_rear(&old_queue, &i4);
	auto_show_queue(old_queue);
	
	delete_queue_front(&old_queue);
	auto_show_queue(old_queue);
	
	cls_queue(&old_queue);
	auto_show_queue(old_queue);

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值