用链表实现队列--链式队列

/*
  一、关于链式队列的约定(目的是在C语言中描述方便)
  1.定义:
  (1)队列是一种先进先出的线性表;
  (2)它只允许在表的一端进行入队,在另一端进行出队操作。在队列中,允许插入的一端
       叫队尾,允许删除的一端较队头,即入队只能从队尾入,出队只能从队头出;
  (3)运算受限的非循环单链表,头尾结点都有,链表中的插入删除操作在这里只能在队头
       队尾进行。
  (4)对头指针指向有效数据,并且可移动;队尾指针指向的结点不存有效数据,指针域
       指向空NULL,永不移动。
  
  2.创建链式队列时:需定义两个结构,一个用于描述结点(此结构跟链表结点对应的结构类
    似),一个用于描述式哪个队列(此结构包括队头和队尾指针);与创建一个空链表类似。
    队头指针和队尾指针都指向不存放有效数据的头结点,此头结点的指针域指向NULL。

  3.入队,出队,编历,判断是否为空(链式队列不存在满问题),清空队列等操作。
  
  4.在C语言中不能使用动态分配的一维数组来实现循环队列。如果用户程序中有循环队列,则必须为它设定一个最大队列长度,若用户无法预估所用队列最大长度,则宜采用链队列。
*/

#include "LinkedQueue.h"

void linkedQueueCreat(struct linkedQueue *qQ)
{
	qQ->pFront = (struct linkedQueueNode *)malloc(sizeof(struct linkedQueueNode));
	if( NULL == qQ->pFront )
	{
		printf("linkedQueueCreat(): malloc failed!\n");
		exit(-1);	
	} 
	qQ->pRear = qQ->pFront;
	qQ->pRear->pNext = NULL;
	return;
}

//入队,链式队列不存在满的情况
void enLinkedQueue(struct linkedQueue *qQ,int val)
{
	struct linkedQueueNode * pNew = (struct linkedQueueNode *)malloc(sizeof(struct linkedQueueNode));
	if( NULL == pNew )
	{
		printf("enLinkedQueue(): malloc failed!\n");
		exit(-1);	
	} 

	pNew->data = val;
	qQ->pRear->pNext = pNew;//从队尾入,把pNew指向的结点挂到队列尾部
	pNew->pNext = NULL;
	qQ->pRear = pNew;//尾指针上移

	return;
	
}

int linkedQueueEmpty(struct linkedQueue *qQ)
{
	if( qQ->pFront == qQ->pRear)
	    return 1;
	else
	    return 0;
}

//出队,要判断是否为空
void outLinkedQueue(struct linkedQueue *qQ,int *pVal)
{
	if( linkedQueueEmpty(qQ) )
	{
		printf("linkedQueue is Empty!\n");
		return;
	}
	
	struct linkedQueueNode * pTemp = qQ->pFront->pNext;//从队头出队,pHead指向的结点不出队,这个结点没有实际意义。队首结点是pHead->pNext指向的结点。
	*pVal = pTemp->data;
	qQ->pFront->pNext = pTemp->pNext;
	free(pTemp);
	
	if( NULL == qQ->pFront->pNext )
	{
		qQ->pFront = qQ->pRear;//当队列最后一个元素被删除后,队列尾指针也丢失了,因此需对队尾指针重新赋值,即指向头结点。
	}

	return;
}

//遍历队列,并返回队列的长度
void traverseLinkedQueue(struct linkedQueue *qQ,int *linkedQueueLen)
{
	int i=0;
	struct linkedQueueNode * pTemp = qQ->pFront->pNext;
	
	while( pTemp != NULL )
	{
		i++;
		printf("data-%d is %d\n",i,pTemp->data);
		pTemp = pTemp->pNext;
	}
	
	*linkedQueueLen = i;
	
	return;
}

//清空队列,使其变为空队列,框架还在
int clearLinkedQueue(struct linkedQueue *qQ)
{
	int pVal=0;

	while( !(linkedQueueEmpty(qQ)) )
           outLinkedQueue(qQ,&pVal);	
	
	return;
}


相关头文件:


#ifndef LINKEDQUEUE_H
#define LINKEDQUEUE_H

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

struct linkedQueueNode 
{
	int data;
	struct linkedQueueNode *pNext;
};

struct linkedQueue
{
	struct linkedQueueNode * pFront;
	struct linkedQueueNode * pRear;
};

void linkedQueueCreat(struct linkedQueue *qQ);
void enLinkedQueue(struct linkedQueue *qQ,int val);
int linkedQueueEmpty(struct linkedQueue *qQ);
void outLinkedQueue(struct linkedQueue *qQ,int *pVal);
void traverseLinkedQueue(struct linkedQueue *qQ,int *linkedQueueLen);
int clearLinkedQueue(struct linkedQueue *qQ);

#endif



  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用链表实现队列主要有两种类型:单向链表和循环链表。 1. 单向链表:单向链表是一种基本的链结构,每个节点只包含一个指向下一个节点的指针。使用单向链表实现队列时,入队操作是在链表尾部添加一个节点,出队操作是从链表头部删除一个节点。 2. 循环链表:循环链表在单向链表的基础上,将链表的尾节点指向了头节点,形成了一个环形结构。使用循环链表实现队列时,入队操作同样是在链表尾部添加一个节点,但是出队操作是从链表头部删除一个节点,并将头节点的指针指向下一个节点。如果队列为空,头节点和尾节点指向同一个节点。 下面是使用Python实现单向链表和循环链表队列的代码示例: 单向链表队列: ```python class Node: def __init__(self, value=None, next=None): self.value = value self.next = next class Queue: def __init__(self): self.head = None self.tail = None def is_empty(self): return not bool(self.head) def enqueue(self, value): node = Node(value) if not self.head: self.head = node self.tail = node else: self.tail.next = node self.tail = node def dequeue(self): if self.is_empty(): return None node = self.head self.head = self.head.next if not self.head: self.tail = None return node.value ``` 循环链表队列: ```python class Node: def __init__(self, value=None, next=None): self.value = value self.next = next class Queue: def __init__(self): self.tail = None def is_empty(self): return not bool(self.tail) def enqueue(self, value): node = Node(value) if self.is_empty(): node.next = node else: node.next = self.tail.next self.tail.next = node self.tail = node def dequeue(self): if self.is_empty(): return None node = self.tail.next if node == self.tail: self.tail = None else: self.tail.next = node.next return node.value ``` 需要注意的是,由于循环链表队列的存在,当队列为空时,头节点和尾节点指向同一个节点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值