链式队列的基本操作,初始化,销毁,清空,检查是否为空,获取对头元素,入队,出队

  • Post author:
  • Post category:其他




链式队列的基本操作

//InitQueue 初始化一个队列

//DestroyQueue 销毁一个队列

//ClearQueue 清空一个队列

//CheckQueueEmpty 检查一个队列是否为空

//GetFront 获取队头的元素 但是不会出队列

//InQueue 入队

//OutQueue 出队


main.c

#include"QueueList.h"

int main()
{
	struct queue * head = InitQueue();//初始化
	int data;
	printf("请输入数据,输入-1结束\n");
	while(1)
	{
		scanf("%d",&data);
		if(data == -1)
		{
			break;
		}
		InQueue(head,data);//入队
	}
	GetFront(head);//得到对头的元素
	DestroyQueue(head);//销毁
	return 0;
}


QueueList.h

#ifndef __QUEUELIST_H__
#define __QUEUELIST_H__

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

struct node 
{
	int data;
	struct node * next;
};
struct queue
{
	int num;
	struct node * front;
	struct node * rear;
};
//InitQueue 初始化一个队列
struct queue * InitQueue();
//DestroyQueue 销毁一个队列
void DestroyQueue(struct queue * head);
//ClearQueue 清空一个队列
void ClearQueue(struct queue * head);
//CheckQueueEmpty 检查一个队列是否为空
bool CheckQueueEmpty(struct queue * head);
//GetFront 获取队头的元素 但是不会出队列
void GetFront(struct queue * head);
//InQueue 入队	
void InQueue(struct queue * head,int data);
//OutQueue 出队
void OutQueue(struct queue * head);

#endif


QueueList.c

#include"QueueList.h"

//InitQueue 初始化一个队列
struct queue * InitQueue()
{
	struct queue * head = malloc(sizeof(*head));
	head -> front = head -> rear = NULL;
	head -> num = 0;
	return head;
}
//DestroyQueue 销毁一个队列
void DestroyQueue(struct queue * head)
{
	if(!head)
	{
		printf("队列未初始化\n");
		return;
	}
	if(CheckQueueEmpty(head))
	{
		printf("空链表,不需要销毁\n");
		free(head);
		return;
	}
	printf("开始销毁\n");
	ClearQueue(head);
	printf("结束销毁\n");
	printf("队列元素还剩:%d\n",head -> num);
	free(head);
}
//ClearQueue 清空一个队列
void ClearQueue(struct queue * head)
{
	if(!head)
	{
		printf("队列未初始化\n");
		return;
	}
	if(CheckQueueEmpty(head))
	{
		printf("空链表,不用清空\n");
		free(head);
		return;
	}
	while(!CheckQueueEmpty(head))
	{
		OutQueue(head);		
	}
}
//CheckQueueEmpty 检查一个队列是否为空
bool CheckQueueEmpty(struct queue * head)
{
	if(!head)
	{
		return true;
	}
	if(head -> num == 0)
	{
		return true;
	}
	return false;
}
//GetFront 获取队头的元素 但是不会出队列
void GetFront(struct queue * head)
{
	if(!head)
	{
		printf("队列未初始化\n");
		return;
	}
	if(CheckQueueEmpty(head))
	{
		printf("空链表,没有头结点\n");
		return;
	}
	printf("队头元素为:%d\n",head -> front -> data);
}
//InQueue 入队	
void InQueue(struct queue * head,int data)
{
	if(!head)
	{
		printf("队列未初始化\n");
		return;
	}
	struct node * pnew = malloc(sizeof(pnew));
	pnew -> data = data;
	pnew -> next = NULL;
	if(CheckQueueEmpty(head))//没有节点
	{
		head -> front = head -> rear = pnew;
		head -> num++;
		return;
	}
	head -> rear -> next = pnew;
	head -> rear = pnew;
	head -> num++;
}
//OutQueue 出队
void OutQueue(struct queue * head)
{
	if(!head)
	{
		printf("队列未初始化\n");
		return;
	}
	if(CheckQueueEmpty(head))
	{
		printf("空链表,不能出队\n");
		return;
	}
	struct node *p = head -> front;
	printf("%d\n",head -> front -> data);
	head -> front = p -> next;
	p -> next = NULL;
	head -> num--;
	free(p);
}



版权声明:本文为qq_43440796原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。