数据结构---------循环队列与链式队列

本文介绍了循环队列在数据结构中的应用,特别强调了如何判断循环队列的空与满。提出了两种处理方法:通过额外变量记录元素个数,或利用(Q->front + 1)%MaxSize与Q->rear的关系进行判断。注意,使用后者可能导致最大空间利用率仅为MaxSize - 1。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.循环队列有几个初学者需要注意的地方:

(1)如何判断循环队列为空或者已满?

其实在程序设计的过程中是有很多方法来处理同一个问题的,就看编写代码者如何去处理。一般情况下,有两种方法来处理循环队列的这个问题:

a.在循环队列的定义过程中,用一个变量来记录当前队列中元素的个数;

b.利用 (Q->front + 1)%MaxSize == Q->rear来判断是否已满;利用Q->front == Q->rear来判断是否为空.因为(Q->front + 1)%MaxSize == Q->rear的缘故,这种方法最大使用队列空间的MaxSize - 1。


//
//
//   循环队列的实现
//
//   包括出队,入队等操作
//
//


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

#define MaxSize  20  // 队列的最大容量

typedef struct SeqQueue
{
	int front   ;        // 队列头
	int rear    ;        // 队列尾
	int data[MaxSize] ;  // 存储队列中的元素
} SeqQueue ;

void InitSeqQueue( SeqQueue * S )
{
	S->front = 0 ;
	S->rear = 0  ;
} 

// 判断队列是否已满
bool is_full( SeqQueue * S )
{
	if( (S->rear + 1) % MaxSize == S->front )
	{
		return true ;
	}
	else
	{
		return false ;
	}
}

// 判断对列是否为空
bool is_empty( SeqQueue * S )
{
	if( S->front == S->rear )
	{
		return true ;
	}
	else
	{
		return false ;
	}
}

//入队
void EnQueue( SeqQueue * S , int elem )
{
	if( !is_full( S ) )
	{
		S->data[S->rear] = elem ;
		S->rear = ( S->rear + 1 ) % MaxSize ;
	}
	else
	{
		printf("队列已满,不能进行入队操作!") ;
		exit(0) ;
	}
}

// 出队操作
void DeQueue( SeqQueue * S , int * elem )
{
	if( !is_empty( S ) )
	{
		*elem = S->data[S->front] ;
		S->front = (S->front + 1 ) % MaxSize ;
	}
	else
	{
		printf("队列为空,不能进行出队操作!") ;
		exit(0) ;
	}
}

void Print( SeqQueue * S )
{
	int i = S->front ;

	while( i != S->rear )
	{
		printf("%d ",S->data[i] ) ;
		i = ( i + 1 ) % MaxSize ;
	}
	printf("\n") ;
}

int main()
{
	SeqQueue S ;
	int elem ;

	InitSeqQueue( &S ) ;

	EnQueue( &S , 8 ) ;
	EnQueue( &S , 4 ) ;
	EnQueue( &S , 11 ) ;
	EnQueue( &S , 7 ) ;

	Print( &S ) ;

	DeQueue( &S , &elem ) ;
	printf("%d出队\n", elem ) ;

	DeQueue( &S , &elem ) ;
	printf("%d出队\n", elem ) ;

	DeQueue( &S , &elem ) ;
	printf("%d出队\n", elem ) ;

	Print( &S ) ;

	return 0 ;
}





2.链式队列

//
//
//   链式队列的实现 , 包括入队,出队等操作
//
//
///

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

typedef struct Node 
{
	int elem ;
	struct Node * next ;
} Node ;

typedef struct LinkQueue
{
	Node * front ;
	Node * rear ;
} LinkQueue ;

void InitQueue( LinkQueue * L )
{
	L->front = NULL ;
	L->rear = NULL ;
}

void EnQueue( LinkQueue * L , int elem )
{
	Node * tmp = (Node*)malloc(sizeof(Node)) ;
	if( !tmp )
	{
		printf("申请内存失败!") ;
		exit(0) ;
	}
	tmp->elem = elem ;
	tmp->next = NULL ;

	if( L->front == L->rear && L->front == NULL )
	{
		L->front = tmp ;
		L->rear = tmp ;
	}
	else
	{
		tmp->next = L->rear ;
		L->rear = tmp ;
	}
}

void DeQueue( LinkQueue * L , int * elem )
{
	Node * p = L->rear ;
	Node * q = p ;

	while( p != L->front )
	{
		q = p ;
		p = p->next ;
	}

	q->next = NULL ;
	L->front = q ;

	*elem = p->elem ;
	free( p ) ;
}

void Print( LinkQueue * L )
{
	Node * p = L->rear ;
	while( p != L->front )
	{
		printf("%d ",p->elem ) ;
		p = p->next ;
	}
	printf("%d\n" , p->elem ) ;
}

int main()
{
	LinkQueue L ;
	int elem ;

	InitQueue( &L ) ;   // 初始化

	EnQueue( &L , 3 ) ;
	EnQueue( &L , 6 ) ;
	EnQueue( &L , 2 ) ;
	EnQueue( &L , 9 ) ;

	Print( &L ) ;

	DeQueue( &L , &elem ) ;
	printf("%d出队\n", elem ) ;

	DeQueue( &L , &elem ) ;
	printf("%d出队\n", elem ) ;

	Print( &L ) ;

	return 0 ;
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值