数据结构的链式队列实现,数据元素为字符数组

链式队列的C语言实现,数据元素为字符串。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define ERROR 0
#define OK 1

typedef int Status;

typedef struct QNode{
	char data[20];
	struct QNode *next;
}QNode,*QueuePtr;

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

int main(){
	Status InitQueue(LinkQueue *);
	Status DestroyQueue(LinkQueue *); 
	Status ClearQueue(LinkQueue *);
	Status QueueEmpty(LinkQueue *);
	int QueueLength(LinkQueue *);
	Status GetTop(LinkQueue *,char *);
	Status EnQueue(LinkQueue *,char *);
	Status DeQueue(LinkQueue *,char *);
	void visit(char *);
	Status QueueTraverse(LinkQueue *,void (*f)(char *));
	
	LinkQueue q,*qp=&q;
	InitQueue(qp);
	char a[20]="你好",b[20]="C语言",c[20]="再见",d[20];
	printf("当前队列长度:%d\n",QueueLength(qp));
	if(QueueEmpty(qp)){
		printf("当前队列为空队列\n");
	}
	EnQueue(qp,a);
	printf("当前队列长度:%d\n",QueueLength(qp));
	if(QueueEmpty(qp)){
		printf("当前队列为空队列\n");
	}
	printf("遍历当前队列:");
	QueueTraverse(qp,visit);
	EnQueue(qp,b);
	printf("\n当前队列长度:%d",QueueLength(qp));
	if(QueueEmpty(qp)){
		printf("当前队列为空队列");
	}
	printf("\n遍历当前队列:");
	QueueTraverse(qp,visit);
	GetTop(qp,d);
	printf("\n获取对头元素,但是不出队列:%s",d);
	EnQueue(qp,c);
	printf("\n当前队列长度:%d\n",QueueLength(qp));
	if(QueueEmpty(qp)){
		printf("\n当前队列为空队列\n");
	}
	printf("遍历当前队列:");
	QueueTraverse(qp,visit);
	printf("\n队头元素出队列");
	DeQueue(qp,d);
	printf("\n当前队列长度:%d",QueueLength(qp));
	if(QueueEmpty(qp)){
		printf("\n当前队列为空队列\n");
	}
	printf("\n遍历当前队列:");
	QueueTraverse(qp,visit);
	if(DestroyQueue(qp)){
		printf("\n队列成功销毁\n");
	}
	return 0;
}

Status InitQueue(LinkQueue *q){
	q->front=(QueuePtr)malloc(sizeof(QNode));
	if(!q->front){
		exit(1);
	}
	q->rear=q->front;
	q->rear->next=NULL;
	return OK;
}

Status DestroyQueue(LinkQueue *q){
	while(q->front){
		q->rear=q->front->next;
		free(q->front);
		q->front=q->rear;
	}
	return OK;
}

Status ClearQueue(LinkQueue *q){
	q->rear=q->front;
	return OK;
}

Status QueueEmpty(LinkQueue *q){
	if(q->front==q->rear){
		return 1;
	}else{
		return 0;
	}
}

int QueueLength(LinkQueue *q){
	QueuePtr p=q->front;
	int n=0;
	while(p!=q->rear){
		n++;
		p=p->next;
	}
	return n;
}

Status GetTop(LinkQueue *q,char *e){
	if(q->front==q->rear){
		return ERROR;
	}
	QueuePtr p=q->front->next;
	strcpy(e,p->data);
	return OK;
}

Status EnQueue(LinkQueue *q,char *e){
	QueuePtr p=(QueuePtr)malloc(sizeof(QNode));
	if(!p){
		exit(1);
	}
	strcpy(p->data,e);
	p->next=NULL;
	q->rear->next=p;
	q->rear=p;
	return OK;
}

Status DeQueue(LinkQueue *q,char *e){
	if(q->front==q->rear){
		return ERROR;
	}
	QueuePtr p=q->front->next;
	strcpy(e,p->data);
	q->front->next=p->next;
	if(q->rear==p){
		q->rear=q->front;
	}
	free(p);
	return OK;
}

void visit(char *e){
	printf("%s\t",e);
}

Status QueueTraverse(LinkQueue *q,void (*f)(char *)){
	if(q->front==q->rear){
		return ERROR;
	}
	QueuePtr p=q->front->next;
	while(p!=q->rear){
		f(p->data);
		p=p->next;
	}
	f(q->rear->data);
	return OK;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

KOKO银角大王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值