栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

队列(数据结构)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

队列(数据结构)

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

#include"stdafx.h"
#include
#include
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;
}

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/766696.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号