数据结构-队列 C语言

【数据结构-堆/栈 C语言实现】

其他数据结构实现:【数据结构-堆/栈 C语言实现】

数据结构链接汇总:

【数据结构-堆/栈 C语言实现】

【数据结构-链表/顺序表 C语言】

【数据结构-队列 C语言】

【数据结构-二叉树遍历 C语言】

1.链式队列

链式队列
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct gma
{
	int data;
	struct gma* next;
}gmas;//队列数据结点
typedef struct head
{
	int length;
	gmas* front;
	gmas* rear;
}phead; //队列指针信息
phead* create()//创建一个链式队列
{
	phead* p = (phead*)malloc(sizeof(phead));
	p->front = NULL;
	p->rear = NULL;
	p->length = 0;
	return  p;
}
int isempty(phead* p)
{
	if (p->length == 0) { return 1; }
	return 0;
}
phead* insert(phead* p, int element)//入队
{
	gmas* node = (gmas*)malloc(sizeof(gmas));
	node->data=element;
	node->next = NULL;
	if (isempty(p))
	{
		p->front = node;
		p->rear = node;
		p->length++;
	}
	p->rear->next = node;
	p->rear = node;
	p->length++;
	return p;
}
int pop(phead* p)//出队
{
	int temp = 0;
	if (isempty(p)) {
		printf("队列为空!"); return -1000;
	}
	gmas* node = p->front;
	p->front = p->front->next;
	p->length--;
	temp = node->data;
	free(node);
	return temp;
}
void clear(phead* p)//清理队列
{
	p->front = NULL;
	p->rear=NULL;
	p->length = 0;
	printf("\n队列已经清空!");
}
int main()
{
	phead* temp = create();
	printf("请输入你想要入队的元素:");
	insert(temp, 9);
	insert(temp, 129); 
	insert(temp, 913);
	insert(temp, 9234);
	insert(temp, 95678);
	insert(temp, 4567);
	printf("此时队列的元素为:\n");
	gmas* node = temp->front;
	while (node != NULL)
	{
		printf("%d ", node->data);
		node = node->next;
	}
	printf("\n出队两个元素后的队列为:\n");
	pop(temp);
	pop(temp);
	gmas* node1 = temp->front;
	while (node1 != NULL)
	{
		printf("%d ", node1->data);
		node1 = node1->next;
	}
    	clear(temp);
	return 0;
}

2.顺序队列

顺序队列
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct queue {
	int front;
	int rear;
	int data[100];
}queues;  //顺序队列
queues* create()//创建队列
{
	queues* p = (queues*)malloc(sizeof(queues));
	p->front = p->rear = 0;
	memset(p->data, 0, 100 * sizeof(int));
	return p;
}
int getlengthqueue(queues* p)//返回队列的长度
{
	return p->rear - p->front;
}
int isempty(queues* p)//判断队列是否为空
{
	if (p->front == p->rear)
	{
		return 1;
	}
	return 0;
}
queues* insert(queues* p, int element)//入队
{
	p->data[p->rear] = element;
	p->rear++;
	return p;
}
int pop(queues* p)//出队
{
	int temp = p->data[p->front];
	p->front++;
	return temp;
}
int gethead(queues* p)//获取队头元素
{
	if (isempty(p)) { printf("error"); return 10000; }
	return p->data[p->front];
}
void clear(queues* p)//清空队列
{
	p->front = p->rear = -1;
}
void destory(queues* p)//销毁队列
{
	free(p);
}

int main()
{
	queues* temp = create();
	printf("请输入你想要入队的元素:");
	insert(temp, 9);
	insert(temp, 129); 
	insert(temp, 913);
	insert(temp, 9234);
	insert(temp, 95678);
	insert(temp, 4567);
	printf("此时队列的元素为:\n");
	for (int i = temp->front; i < temp->rear; i++)
	{
		printf("%d ", temp->data[i]);
	}
	printf("\n出队两个元素后的队列为:\n");
	pop(temp);
	pop(temp);
	for (int i = temp->front; i <temp->rear; i++)
	{
		printf("%d ", temp->data[i]);
	}
	clear(temp);
	destory(temp);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_53284122/article/details/129407801