数据结构之链队列的链式存储使用

//链队列的链式存储使用

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

typedef char ElemType;

typedef struct LNode {
	ElemType data;
	struct LNode* next;
}LNode,*QueuePtr;//构造链结构

typedef struct QueueList {
	QueuePtr front, rear;
}QueueList;//;构造链队列的结构

//创建队列
InitQueue(QueueList* q) {
	q->front = q->rear = (QueuePtr)malloc(sizeof(LNode));
	if (!q->front)
		exit(0);
	q->front->next = NULL;
}

//入链队列操作
InsertQueue(QueueList* s, ElemType e) {
	QueuePtr p;
	p = (QueuePtr)malloc(sizeof(LNode));
	p->data = e;
	p->next = NULL;
	s->rear->next = p->next;
	s->rear = p;
}

//出链队列操作
OutQueue(QueueList* s, ElemType* e) {
	QueuePtr p; 
	if (s->front == s->rear)
		return;
	p = s->front->next;
	*e = p->data;
	s->front = p->next;
	if (s->rear == p)
	{
		s->rear = s->front;
	}
	free(p);
}

int main(void) {
	ElemType e;
	QueueList s;
	InitQueue(&s);
	printf("请输入一串数字来让队列进行储存和打印(输入#结束):\n");
	scanf("%c", &e);
	if (e != '#') {
		printf("链队列入列的数据为:");
	}
	else {
		printf("你输入的数据有误!\n");
		return -1;
	}
	while (e != '#') {
		InsertQueue(&s,e);
		printf("%c ", e);
		scanf("%c", &e);
	}
	printf("\n链队列出列的数据为:");
	while (s.front != s.rear) {
		OutQueue(&s, &e);
		printf("%c ", e);
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值