队列(数据结构)

1.自己完成  终于到达学期初梦想的彼岸

#include"stdafx.h"
#include<iostream>
#include<assert.h>
using namespace std;
typedef int Elemtype;
typedef struct Node{
	Elemtype data;
	struct Node *next;
}QueueNode,*PQueueNode;
typedef struct {
	QueueNode *front;
	QueueNode* rear;
}LinkQueue,*PLinkQueue;
//购买节点
QueueNode *BuyNode(){
	QueueNode *s = (QueueNode*)malloc(sizeof(QueueNode));
	if (s == NULL)  exit(1);
	s->next = NULL;
	return s;
}

//初始化
void Init_Queue(LinkQueue *q){
	assert(q != NULL);
	QueueNode *s = BuyNode();
	q->front = s;
	q->rear = s;
}
//入队
bool Push_rear(LinkQueue *q, Elemtype val){
	assert(q != NULL);
	QueueNode *s=BuyNode();
	s->data = val;
	q->rear->next = s;
	q->rear = s;
	s->next = NULL;
	return true;
}
//打印
void Print_Queue(LinkQueue *q){
	assert(q != NULL);
	QueueNode *s = q->front->next ;
	while (s != NULL){
		cout << s->data << endl;
		s = s->next;
	}
}
//出队
bool Pop_Front(LinkQueue *q){
	assert(q != NULL);
	QueueNode *s = q->front->next;
	q->front->next = s->next;
	free(s);
	return true;
}
//判空
bool Empty_Queue(LinkQueue *q){
	assert(q != NULL);
	if (q->rear == q->front){
		return true;
	}
	else return false;
}
//清空队列
bool Clear_Queue(LinkQueue *q){
	assert(q != NULL);
	while (!Empty_Queue(q)){
		Pop_Front(q);
	}
	return true;
}
//摧毁队列
bool Destory_Queue(LinkQueue *q){
	assert(q != NULL);
	Clear_Queue(q);
	free(q->front);
	return true;
}

//获取队头元素的值
Elemtype *Get_Front(LinkQueue *q, Elemtype *s){
	assert(q != NULL);
	if (s == NULL)  return NULL;
	*s = q->front->next->data ;
	return s;
}
//队列
int main(){
	LinkQueue q;
	Init_Queue(&q);
	cout << "入队后的元素为" << endl;
	for (int i = 1; i <= 10; i++){
		Push_rear(&q, i);
	}
	Print_Queue(&q);
	cout << "队列清空后:" << endl;
	Clear_Queue(&q);
	Print_Queue(&q);
	int s;
	//Get_Front(&q, &s);
	//cout << endl<<"栈顶元素为:" << endl;
	//cout << s << endl;
	cout << "时光作渡,眉目成书,从此深情,不被辜负。" << endl;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值