数据结构——队列

队列(Queue)

1.队列的定义

1.只能在一端插入,而在另一端删除的有序序列,这样的数据组织方式称为“队列”(头删尾加)
2.设一个队列Q=(a1,a2……an),则
a1:队头元素
an:队尾元素
3.队列中先插入的数据先被删除,故队列又被称为“先进先出”表(Fist In First Out,FIFO)

2.抽象数据类型

类型名称:队列(Queue)
数据对象集:一个有0个或多个元素的有穷线性表
操作集:
1.Queue CreateQueue( int MaxSize ):生成空队列,其最大长度为MaxSize
2.bool IsFull( Queue Q ):判断队列Q是否已满
3.bool AddQ( Queue Q, ElementType X )将元素压入队列Q
4.bool IsEmpty( Queue Q ):判断队列Q是否为空
5.ElementType DeleteQ( Queue Q ):删除并返回队列头元素,若此时队列为空返回ERROR

3.C语言实现循环队列

3.1顺序存储

#include<stdio.h>
#include<stdlib.h>
#define ERROR -1
typedef int Position;
struct QNode {
    int *Data;     /* 存储元素的数组 */
    Position Front, Rear;  /* 队列的头、尾指针 */
    int MaxSize;           /* 队列最大容量 */
};
typedef struct QNode *Queue;
 
Queue CreateQueue( int MaxSize ); 
bool IsFull( Queue Q ); 
bool AddQ( Queue Q, int X );
bool IsEmpty( Queue Q );
int DeleteQ( Queue Q );//返回队列头元素,如删除后队列为空返回ERROR 
void p(Queue Q);
int main(){
	int i; 
	Queue Q;
	Q=CreateQueue(5);
	p(Q); 
	AddQ(Q,1);p(Q); 
	AddQ(Q,2);p(Q); 
	AddQ(Q,3);p(Q); 
	i=DeleteQ(Q);p(Q); 
	printf("删除元素后,队列头元素:%d(若为-1表示此时队列为空)\n",i);
	i=DeleteQ(Q);p(Q); 
	printf("删除元素后,队列头元素:%d(若为-1表示此时队列为空)\n",i);
	AddQ(Q,4);p(Q); 
	i=DeleteQ(Q);p(Q); 
	printf("删除元素后,队列头元素:%d(若为-1表示此时队列为空)\n",i);
	i=DeleteQ(Q);p(Q); 
	printf("删除元素后,队列头元素:%d(若为-1表示此时队列为空)\n",i);
	
}

void p(Queue Q){
	int i;
	printf("队列数据:");
	
	if(IsEmpty(Q)){
		printf("无    ");	
	}
	else{
		i=Q->Front;
		while(Q->Rear>i){
			printf(" %d ",Q->Data[i]);
			i=i+1;
		}
	}
	printf("打印完成\n");
}


Queue CreateQueue( int MaxSize )
{
    Queue Q = (Queue)malloc(sizeof(struct QNode));
    Q->Data = (int *)malloc(MaxSize * sizeof(int));
    Q->Front = Q->Rear = 0;
    Q->MaxSize = MaxSize;
    return Q;
}
 
bool IsFull( Queue Q )
{
    return ((Q->Rear+1)%Q->MaxSize == Q->Front);
}
 
bool AddQ( Queue Q, int X )
{
    if ( IsFull(Q) ) {
        printf("队列满");
        return false;
    }
    else {
        Q->Rear = (Q->Rear+1)%Q->MaxSize;
        Q->Data[Q->Rear-1] = X;
        printf("\n");
        return true;
    }
}
 
bool IsEmpty( Queue Q )
{
    return (Q->Front == Q->Rear);
}
 
int DeleteQ( Queue Q )
{
	int i;
    if ( IsEmpty(Q) ) { 
        printf("队列空");
        return ERROR;
    }
    else  {
    	
        Q->Front =(Q->Front+1)%Q->MaxSize;
        if(IsEmpty(Q)){
        	printf("\n");
        	return ERROR;
		}
		else{
			printf("\n");
			return  Q->Data[Q->Front];	
		}
    }
}

3.2 链表

//队列的链式存储 
#include<stdio.h>
#include<stdlib.h>
#define ERROR -1
typedef int ElementType;

typedef struct Node *PtrToNode;
struct Node { /* 队列中的结点 */
    ElementType Data;
    PtrToNode Next;
};
typedef PtrToNode Position;

typedef struct QNode * PtrToQNode;
struct QNode {
    Position Front, Rear;  /* 队列的头、尾指针 */
//    int MaxSize;           /* 队列最大容量 */
};
typedef struct QNode *Queue;
 
void p(Queue Q);
Queue CreateQueue(); 
bool AddQ(Queue Q,ElementType x);
bool IsEmpty( Queue Q ); 
ElementType DeleteQ( Queue Q );

int main(){
	int i; 
	Queue q;
	Q=CreateQueue();
	p(q); 
	AddQ(q,1); p(q);
	AddQ(q,2); p(q);
	AddQ(q,3); p(q); 
	i=DeleteQ(q);p(q); 
	printf("删除元素后,队列头元素:%d(若为-1表示此时队列为空)\n",i);
	i=DeleteQ(q);p(q); 
	printf("删除元素后,队列头元素:%d(若为-1表示此时队列为空)\n",i);
	AddQ(q,4);p(q); 
	i=DeleteQ(q);p(q); 
	printf("删除元素后,队列头元素:%d(若为-1表示此时队列为空)\n",i);
	i=DeleteQ(q);p(q); 
	printf("删除元素后,队列头元素:%d(若为-1表示此时队列为空)\n",i);
	
}

void p(Queue Q){
	Position replace=Q->Front;
	printf("队列数据:");
	
	if(IsEmpty(Q)){
		printf("无    ");	
	}
	else{		
		while(replace!=NULL){
			printf(" %d ",replace->Data);
			replace=replace->Next;
		}		
	}
	printf("打印完成\n");
}

//void p(Queue Q){           //这样写是错的,形参Q改变了函数外实参q->Fornt的值 
//	printf("队列数据:");
//	
//	if(IsEmpty(Q)){
//		printf("无    ");	
//	}
//	else{		
//		while(Q->Front!=NULL){
//			printf(" %d ",Q->Front->Data);
//			Q->Front=Q->Front->Next;
//		}		
//	}
//	printf("打印完成\n");
//}


Queue CreateQueue(){
//Q的建立过程其实分为两步,但代码可一步完成(帮助理解Q的构成) 
//	1.PtrToNode q; //先建立一个链表q 
//	q=(PtrToNode)malloc(sizeof(struct Node));
//	2.Queue Q;     //建立含链表的队列(用链表储存信息) 
//	Queue Q=(Queue)malloc(sizeof(struct QNode));

	//q被Q包含,类似一种嵌套
	//即Q等于两个指向同一链表的结构体指针(Front、Rear)+MaxSize
	Queue Q=(Queue)malloc(sizeof(struct QNode));
	Q->Front=Q->Rear=NULL;
	
	
	return Q;
}
bool AddQ(Queue Q,ElementType x){
	Position RearCell=(Position)malloc(sizeof(struct Node));
	
	if(IsEmpty(Q)){
		Q->Front=Q->Rear=RearCell;
		Q->Rear->Data=x;
		Q->Rear->Next=NULL;
	}
	else{
		
		Q->Rear->Next=RearCell;
		Q->Rear=RearCell;
		Q->Rear->Data=x;
		Q->Rear->Next=NULL;
	}
}

bool IsEmpty( Queue Q )
{
    return ( Q->Front == NULL);
}
 
ElementType DeleteQ( Queue Q )
{
    Position FrontCell; 
    ElementType FrontElem;
     
    if  ( IsEmpty(Q) ) {
        printf("队列空");
        return ERROR;
    }
    else {
        FrontCell = Q->Front;
        if ( Q->Front == Q->Rear ) /* 若队列只有一个元素 */
            Q->Front = Q->Rear = NULL; /* 删除后队列置为空 */
        else                     
            Q->Front = Q->Front->Next;
        FrontElem = FrontCell->Data;
 
        free( FrontCell );  /* 释放被删除结点空间  */
        return  FrontElem;
    }
}

结构体指针改变队列问题
结构体指针改变队列问题

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值