队列

//链队列
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 30

typedef struct node
{
	char data;
	struct node *next;
}QNode;

typedef struct
{
	QNode *front, *rear;  //头尾指针
}LQueue;

void Init_LQueue(LQueue **q)
{
	QNode *p;
	p = (QNode *)malloc(sizeof(QNode));      //申请队列的头节点
	(*q) = (LQueue *)malloc(sizeof(LQueue)); //申请带头尾指针的节点
	p->next = NULL;
	(*q)->front = p;
	(*q)->rear = p;
}

int Empty_LQueue(LQueue *q)                  //队列空
{
	if (q->front == q->rear)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

void In_LQueue(LQueue *q, char x)
{
	QNode *p;
	p = (QNode *)malloc(sizeof(QNode));
	p->data = x;
	p->next = NULL;
	q->rear->next = p;
	q->rear = p;                              //使队尾指针指向新的队尾节点*p
}

void Out_LQueue(LQueue *q, char *x)           //出队
{
	QNode *p;
	if (Empty_LQueue(q))
	{
		printf("Empty!\n");
		return ;
	}
	else
	{
		p = q->front->next;                    //p 指向第一个节点
		*x = p->data;
		q->front->next = p->next;              //把第二个节点作为第一个节点
		free(p);
		if (q->front->next == NULL)         //出队后队列为空
		{
			q->rear = q->front;
		}
	}
}

void Print(LQueue *q)
{
	QNode *p;
	p = q->front->next;
	while (p != NULL)
	{
		printf("%4c", p->data);
		p = p->next;
	}
	printf("\n");
}

int main()
{
	LQueue *q;
	char x, *y = &x;
	Init_LQueue(&q);
	if (Empty_LQueue(q))
	{
		printf("Empty!\n");
	}
	printf("Input any string:\n");
	scanf("%c", &x);
	while (x != '\n')
	{
		In_LQueue(q, x);
		scanf("%c", &x);
	}

	printf("Output element of Queue:\n");
	Print(q);
	
	printf("Output Queue:\n");
	Out_LQueue(q, y);

	printf("Element of Output Queue is %c\n", *y);
	Print(q);

	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值