队列的链式存储的、初始化、创建、进队、出队、销毁和遍历

学海无涯苦做舟,天天7.起来奋斗。
道路遥遥无期需作伴加QQ845264178
点个赞谢谢

#include<iostream>

using namespace std;

#define MAX 1204
#define ERROR -1
#define OK 0
typedef int ElemType;
typedef int Status;

typedef struct node
{	
	ElemType elem;
	struct node* next;

}Node;


typedef struct  Queue
{
	Node* front;
	Node* rear;
	int count;
}QueueLink;

//初始化
QueueLink* InitQueueLink()
{
	QueueLink* q = (QueueLink*)malloc(sizeof(QueueLink));
	if (q != NULL)
	{
		q->front = NULL;
		q->rear = NULL;
		q->count = 0;

	}
	
	return q;

}

bool empty(QueueLink* q)
{
	return(q->count == 0);
}
//进队
Status  EnQueueLink(QueueLink* q, ElemType e)
{
	Node* s = (Node*)malloc(sizeof(Node));
	if (s!=NULL )
	{
		s->elem = e;
		s->next = NULL;

	}
	if (empty(q))
		q->front = s;
	else
		q->rear->next = s;
	q->rear = s;
	q->count++;

	return OK;
	


}

//出队
Status OutQueueLink(QueueLink* q, ElemType* e)
{
	if (q->rear == NULL)
	{
		cout << "empty" << endl;
		return ERROR;
	}
	

	*e = q->front->elem;
	Node* p = q->front;
	q->front = q->front->next;
	if (q->count==1)
	{
		q->rear = NULL;
	}
	free(p);
	
	return OK;


}



//创建

void CreateQueueLink(QueueLink* q)
{
	int n;
	cout << "输入你要创建的个数" << endl;
	cin >> n;
	for (int i = 0;i < n;i++)
	{
		EnQueueLink(q, i);
		//cout << "n=" << i << endl;
	}
}


//销毁
void DelQueueLink(QueueLink* q)
{
	Node* p = q->front;
	q->front = q->rear = NULL;
	Node* tmp;
	while (p != NULL)
	{
		tmp = p->next;
		free(p);
		p = tmp;
	}
}

//打印
void Show(QueueLink *q)
{
	Node *p = q->front;
	while (p != NULL)
	{
		cout << p->elem << endl;
		p = p->next;
		//cout << "show行号=" << __LINE__ << endl;
	}
	
}



int main()
{
	QueueLink* q = InitQueueLink();
	CreateQueueLink(q);
	Show(q);
	cout << "----------------------" << endl;
	EnQueueLink(q, 666);
	EnQueueLink(q, 777);

	int e;
	OutQueueLink(q, &e);
	cout << "e=" << e << endl;
	
	Show(q);
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

困了就喝白茶

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

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

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

打赏作者

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

抵扣说明:

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

余额充值