数据结构(c语言版)-------链队列的初始化、入队、出队、队列的遍历等操作

链队列的基本操作

链队列结构

typedef struct node {
	int data;
	struct node *next;
}Node;
typedef struct queue {
	Node *front;
	Node *rear;
}Queue;//链队列的结构

初始化

bool InitQueue(Queue *q) {
	q->front = (Node *)malloc(sizeof(Node));
	
	if (q==NULL||q->front==NULL)
	{
		return false;
	}
	q->rear = q->front;//初始化时,将尾指针和头指针指向同一处
	q->front->next = NULL;
	return true;
}

入队

bool PushQueue(Queue *q,int data) {
	Node *p = (Node *)malloc(sizeof(Node));
	if (p==NULL)
	{
		return false;
	}
	//模仿链表的尾插法进行入队
	p->data = data;
	p->next = NULL;
	q->rear->next = p;
	q->rear = p;
	return true;
}

出队

void PopQueue(Queue *q, int *data) {
	if (q->front==q->rear)
	{
		return;
	}
	Node *p;
	p = q->front->next;
	*data = p->data;
	q->front->next = p->next;//将头节点的下一个指向当前节点的下一个
	if (q->rear==p)
	{
		q->rear = q->front;//将队列置为空
	}
	free(p);//释放节点

}

获取队头元素

int GetHead(Queue *q) {
	if (q->front!=q->rear)
	{
		return q->front->next->data;//返回队头
	}
	return -1;
}

队列的长度

int Length(Queue *q) {
	int len = 0;
	Node *p = q->front->next;
	while (p)
	{
		
		len++;
		p = p->next;//获取队列的长度
	}
	return len;
}

获取队尾元素

int GetTail(Queue *q) {
	if (q->front!=q->rear)
	{
		return q->rear->data;
			//返回队尾元素
	}
	return -1;
}

遍历队列

void printfQueue(Queue *q) {
	Node *p = q->front->next;
	while (p) {
		printf("%d ", p->data);
		p = p->next;//依次遍历队列
	}
	printf("\n");
}

测试代码

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<time.h>
typedef struct node {
	int data;
	struct node *next;
}Node;
typedef struct queue {
	Node *front;
	Node *rear;
}Queue;//链队列的结构
bool InitQueue(Queue *q);//初始化
bool PushQueue(Queue *q,int data);//进队
void PopQueue(Queue *q, int *data);//出队
int Length(Queue *q);//队列的长度
int GetHead(Queue *q);//获取队头
int GetTail(Queue *q);//获取队尾
void printfQueue(Queue *q);//遍历队列
int main() {
	bool flag = true;
	int choice;
	int data;
	int i = 1;
	Queue *q = (Queue *)malloc(sizeof(Queue));
	while (flag)
	{
		printf("\n====链队列的基本操作====\n");
		printf("1.链队列的初始化\n");
		printf("2.入队\n");
		printf("3.出队\n");
		printf("4.获取队的长度\n");
		printf("5.返回队头元素\n");
		printf("6.返回队尾元素\n");
		printf("7.遍历队中的元素\n");
		printf("===退出程序按任意值===\n");
		printf("\n输入你的选择:");
		scanf("%d", &choice);
		switch (choice)
		{
		case 1:
			if (InitQueue(q))
			{
				printf("\n初始化成功\n");
			}
			else {
				printf("\n初始化失败\n");
			}
			break;
		case 2:
			printf("data = ");
			scanf("%d", &data);
			if (PushQueue(q,data))
			{
				printf("\n进队成功\n");
			}
			else {
				printf("\n进队失败\n");
			}
			break;
		case 3:
	
			PopQueue(q, &data);
			printf("%d \n", data);
			break;
		case 4:
			
				printf("\n队的长度为:%d\n", Length(q));
			break;
		case 5:
			if (GetHead(q)!=-1)
			{
				printf("\n队头元素为:%d\n", GetHead(q));
			}
			else {
				printf("\n队列为空\n");
			}
			break;
		case 6:
			if (GetTail(q)!=-1)
			{
				printf("\n队尾元素为:%d\n", GetTail(q));
			}
			else {
				printf("\n队列为空\n");
			}
			break;
		case 7:
			printfQueue(q);
			break;
		default:
			flag = false;
			break;
		}

	}
}
bool InitQueue(Queue *q) {
	q->front = (Node *)malloc(sizeof(Node));
	
	if (q==NULL||q->front==NULL)
	{
		return false;
	}
	q->rear = q->front;//初始化时,将尾指针和头指针指向同一处
	q->front->next = NULL;
	return true;
}
bool PushQueue(Queue *q,int data) {
	Node *p = (Node *)malloc(sizeof(Node));
	if (p==NULL)
	{
		return false;
	}
	//模仿链表的尾插法进行入队
	p->data = data;
	p->next = NULL;
	q->rear->next = p;
	q->rear = p;
	return true;
}
void PopQueue(Queue *q, int *data) {
	if (q->front==q->rear)
	{
		return;
	}
	Node *p;
	p = q->front->next;
	*data = p->data;
	q->front->next = p->next;//将头节点的下一个指向当前节点的下一个
	if (q->rear==p)
	{
		q->rear = q->front;//将队列置为空
	}
	free(p);//释放节点

}
int Length(Queue *q) {
	int len = 0;
	Node *p = q->front->next;
	while (p)
	{
		
		len++;
		p = p->next;//获取队列的长度
	}
	return len;
}
int GetHead(Queue *q) {
	if (q->front!=q->rear)
	{
		return q->front->next->data;//返回队头
	}
	return -1;
}
int GetTail(Queue *q) {
	if (q->front!=q->rear)
	{
		return q->rear->data;
			//返回队尾元素
	}
	return -1;
}
void printfQueue(Queue *q) {
	Node *p = q->front->next;
	while (p) {
		printf("%d ", p->data);
		p = p->next;//依次遍历队列
	}
	printf("\n");
}
  • 3
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
下面是 C 语言实现链队列的代码,包括建立队列入队出队遍历队列操作。 ```c #include <stdio.h> #include <stdlib.h> // 队列结点的结构体 typedef struct Node { int data; struct Node *next; } Node; // 队列结构体 typedef struct Queue { Node *front; // 队头指针 Node *rear; // 队尾指针 } Queue; // 初始化队列 void initQueue(Queue *q) { q->front = q->rear = NULL; } // 入队 void enQueue(Queue *q, int data) { Node *newNode = (Node *)malloc(sizeof(Node)); newNode->data = data; newNode->next = NULL; if (q->rear == NULL) { q->front = q->rear = newNode; } else { q->rear->next = newNode; q->rear = newNode; } } // 出队 int deQueue(Queue *q) { if (q->front == NULL) { printf("Queue is empty.\n"); return -1; } Node *p = q->front; int data = p->data; if (q->front == q->rear) { q->front = q->rear = NULL; } else { q->front = q->front->next; } free(p); return data; } // 遍历队列 void traverseQueue(Queue *q) { if (q->front == NULL) { printf("Queue is empty.\n"); return; } Node *p = q->front; while (p != NULL) { printf("%d ", p->data); p = p->next; } printf("\n"); } int main() { Queue q; initQueue(&q); // 入队 enQueue(&q, 1); enQueue(&q, 2); enQueue(&q, 3); enQueue(&q, 4); // 遍历队列 traverseQueue(&q); // 出队 printf("deQueue: %d\n", deQueue(&q)); printf("deQueue: %d\n", deQueue(&q)); // 遍历队列 traverseQueue(&q); return 0; } ``` 以上代码中,我们定义了 `Node` 结构体表示队列的结点,其中包含一个整型数据成员和一个指向下一个结点的指针。我们还定义了 `Queue` 结构体表示队列,其中包含指向队头和队尾的指针。接下来,我们分别实现初始化队列入队出队遍历队列操作。 在 `enQueue()` 函数中,我们先创建一个新的结点,然后判断队列是否为空。如果队列为空,将新结点同时设为队头和队尾;否则将新结点插入到队尾,并更新队尾指针。 在 `deQueue()` 函数中,我们先判断队列是否为空。如果队列为空,输出错误信息并返回 -1;否则从队头删除一个结点,并返回其数据值。如果队列中只有一个结点,同时更新队头和队尾指针;否则只更新队头指针。 在 `traverseQueue()` 函数中,我们先判断队列是否为空。如果队列为空,输出错误信息;否则从队头开始遍历整个队列,输出每个结点的数据值。 最后,在 `main()` 函数中,我们首先调用 `initQueue()` 函数初始化队列,然后调用 `enQueue()` 函数入队四个元素。接着调用 `traverseQueue()` 函数遍历队列,并分别调用 `deQueue()` 函数出队两个元素。最后再调用 `traverseQueue()` 函数遍历队列一次,以验证入队出队操作的正确性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

何一平?

你的收获就是我学习的动力

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

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

打赏作者

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

抵扣说明:

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

余额充值