C语言队列的两种实现

### 本文比较注重动手能力,关于各种数据结构的理论知识请查阅书籍或相关网页,代码部分都有注释

一、基于循序表的循环队列

#include<stdlib.h>
#include<stdio.h>
#define MaxSize 20

typedef struct{
	int data[MaxSize];
	int front,rear;
}squeue, *squlink;


//清空队列
void Clearsqueue(squlink q){
	q->front=q->rear=0;
}

//判断队列是否为空
int Emptysqueue(squlink q){
	if (q->front==q->rear) return 1;
	else return 0;
}

// 判断队列是否已经满了
int IsFullsqueue(squlink q){
	if ((q->rear+1)%MaxSize == q->front) return 1;
	else return 0;
}

// 实现队列的插入
int Insertsqueue(squlink q, int x){
	if (IsFullsqueue(q)==1)
	{	printf("squeue is Full ,insert failing");
		return 0;
	}
	else
	{
		q->rear =(q->rear+1)%MaxSize;
		q->data[q->rear] = x;
		printf("%d is inserted into squeue!\n",x);
		return 1;
	}
}


//出队列的算法
int Deletesqueue(squlink q){
	if (Emptysqueue(q)){
		printf("squeue is empty, delete failing");
		return NULL;
	}
	else{
		int x ;
		// 此处+1是因为,约定头指针front位置不存放数据,如果强制取出其内部数据是没有意义的
		x = q->data[q->front+1];
		printf("*****************%d**************\n",x);


		q->front = (q->front+1)%MaxSize;
		return x;
	}
}


void main(){
	squlink q =(squlink) malloc(sizeof(squeue));
	Clearsqueue(q);
	printf("q-> front:%d\n", q->front);
	printf("q-> rear:%d\n", q->rear);
	
	int sign;
	sign = Emptysqueue(q);
	if (sign==1) printf("squeue is empty");
	else printf("squeue is not empty");

	printf("\n");
	printf("\n");
	printf("\n");
	printf("\n");

	//q->front=3;
	//q->rear=2;
	//sign = IsFullsqueue(q);
	//if (sign==1) printf("squeue is full");
	//else printf("squeue is not full");
	
	// 插入队列
	for(int i=0;!(IsFullsqueue(q));i++)
	{
		Insertsqueue(q, i);
	}
	

	// 此时队列是满的,测试再次插入
	int x=1000;
	Insertsqueue(q,x);
	
	// 测试插入算法
	// 首先打印一下头指针的位置
	printf("q->front is pointing to NO. %d\n",q->front);
	x = Deletesqueue(q);
	printf("%d is delete from q\n", x);
	//再次打印一下头指针的位置
	printf("after deleting q->front is pointing to NO. %d\n",q->front);


}

 二、链式队列

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

typedef struct QNode{
	int data;
	struct QNode *next;
}QNode,*QueuePtr;

typedef struct{
	QueuePtr front;
	QueuePtr rear;
}LinkQueue;

//初始化队列
void InitQueue(LinkQueue *q){
	q->front=(QueuePtr)malloc(sizeof(QNode));
	q->front->next=NULL;
	q->rear = q->front;
}


//判断队列是否为空,判断条件为q->front==->rear
int IsEmptyQueue(LinkQueue *q){
	if (q->front==q->rear)	return (1);
	else return (0);
}

//置空队列
void ClearQueue(LinkQueue *q){
	while(!IsEmptyQueue(q))
		DeQueue(q);
}

//进队列,在队列尾部插入
void EnQueue(LinkQueue *q, int x){
	QueuePtr new_node = (QueuePtr)malloc(sizeof(QNode));
	new_node->data = x;
	printf("\n%d is inserted to to queue\n",x);
	new_node->next=NULL;
	q->rear->next=new_node;
	q->rear=new_node;
}

//出队列
int DeQueue(LinkQueue *q){
	QueuePtr temp;
	temp = q->front->next;
	int x = temp->data;
	q->front->next=temp->next;
	if (q->rear==temp) q->rear=q->front;	
	free(temp);
	printf("\n%d is out \n",x);
	return x;
}


void main(){
	LinkQueue *Q = (LinkQueue *)malloc(sizeof(LinkQueue));
       	InitQueue(Q);
	for(int i=1;i<10;i++)
		EnQueue(Q,i);
	DeQueue(Q);
	ClearQueue(Q);

	int i;
	i = IsEmptyQueue(Q);
	printf("Q is empty or not %d\n",i);
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值