链式队列的基础操作实现(C语言)
来源:互联网 发布:可视化编程软件初学 编辑:程序博客网 时间:2024/05/16 13:01
链式队列是队列的一种链表表示形式
主要操作有:
1 : 清空并创建一个新的队列
2 : 添加一个元素到队列中
3 : 添加一组元素到队列中
4 : 从队列中删除一个元素
5 : 从队列中删除一个元素
6 : 获取队列长度
7 : 打印队列元素到屏幕
8 : 清空队列元素
9 : 重新初始化队列(销毁队列后执行)
10: 销毁队列元素(请执行命令9)
待完成功能:LinkQueue* DeleteAny (LinkQueue *Queue, int any) //返回新的链表
从队头删除任意元素,并将其存入新的队列已备用;
/*--------------------------------------------------目的:学习队列的创建及用法,链式存储实现--作者:刘志强--创建时间:2015-1-7-------------------------------------------------*/#include <stdio.h>#include <stdlib.h> #define ElementType int/*--------------------------------------------------创建队列节点及队的指针 -------------------------------------------------*/typedef struct Node {ElementType Data;struct Node *Next; }QueueNode;typedef struct {QueueNode *rear; //队尾指针一个 QueueNode *front; //队头指针一个 }LinkQueue;LinkQueue PtrQ,NewQueue; //为什么定义成指针不行呢?
/*--------------------------------------------------功能函数;判断队列是否为空 -------------------------------------------------*/ElementType IsEmpty (LinkQueue *Queue){if(Queue->front->Next == NULL){printf("队列为空!!!\n");}return Queue->front->Next == NULL; }
/*--------------------------------------------------功能函数;初始化队列 ,创建空队列 -------------------------------------------------*/void InitQueue (LinkQueue *Queue){Queue->front = Queue->rear = (QueueNode *) malloc(sizeof(QueueNode));Queue->front->Next = NULL;printf("初始化完成!\n");}
/*--------------------------------------------------功能函数;元素 Element 进入队列 -------------------------------------------------*/void AddQueue (LinkQueue *Queue,ElementType Element) {QueueNode *temp = (QueueNode*) malloc(sizeof(QueueNode));if (temp) {temp->Data = Element;temp->Next = NULL;Queue->rear->Next = temp;Queue->rear = temp;}}
/*--------------------------------------------------功能函数;创建队列 -------------------------------------------------*/void CreatQueue (LinkQueue *Queue) {ElementType temp;InitQueue (Queue);printf("请输入一组数字创建队列(注意:请按换行后Ctrl+Z再回车结束输入):\n");while(scanf("%d",&temp) != EOF) //若不能安全结束循环会造成:内存满->电脑死机!!! AddQueue (Queue,temp);printf("队列创建成功!\n"); }
/*--------------------------------------------------功能函数;从队列中删除一个元素 -------------------------------------------------*/ElementType DeleteQueue(LinkQueue *Queue) {//带头结点的链式队列 ,每次删除头结点后面的一个元素 QueueNode *FrontCell;ElementType FrontElem;if (IsEmpty(Queue)==1)return NULL;FrontCell=Queue->front;FrontElem=Queue->front->Next->Data;if(Queue->front==Queue->rear)Queue->front=Queue->rear=NULL;elseQueue->front=Queue->front->Next;free(FrontCell);return FrontElem; }
/*--------------------------------------------------功能函数;打印队列中的所有元素 -------------------------------------------------*/void Print (LinkQueue *Queue){QueueNode *TempCell;if (Queue->front->Next == NULL){printf("队列中的所有元素为:无\n"); return NULL; //不再执行此函数下面的程序 }printf("队列中的所有元素为:\n"); TempCell=Queue->front->Next;while(TempCell != Queue->rear) { printf("%d ",TempCell->Data); TempCell = TempCell->Next; } printf("%d",TempCell->Data); printf("\n"); }
/*--------------------------------------------------功能函数;获取队列长度 -------------------------------------------------*/int GetQueueLength (LinkQueue *Queue) {/*因队头和队尾指针用指针变量定义注意不能使用指针改变数据结构 */ int length=0;QueueNode *TempCell;TempCell = Queue->front->Next;if (IsEmpty(Queue)==1)return 0;while(TempCell->Next!=NULL){QueueNode *Temp; //交换思想 Temp=TempCell->Next;TempCell=Temp;length++; }return length+1;}
/*--------------------------------------------------功能函数;清空队列 -------------------------------------------------*/void ClearQueue (LinkQueue *Queue) {while(1){QueueNode *FrontCell;if (Queue->front->Next == NULL)break;FrontCell=Queue->front;if(Queue->front==Queue->rear)Queue->front=Queue->rear=NULL;elseQueue->front=Queue->front->Next;free(FrontCell);}printf("队列已清空\n"); }
/*--------------------------------------------------功能函数;销毁队列 -------------------------------------------------*/void DestroyQueue (LinkQueue *Queue) {while(1){QueueNode *FrontCell;if (Queue->front->Next == NULL)break;FrontCell=Queue->front;if(Queue->front==Queue->rear)Queue->front=Queue->rear=NULL;elseQueue->front=Queue->front->Next;free(FrontCell);} free(Queue->front);//free(Queue->rear);printf("队列已销毁,请重新初始化!\n"); }
/*--------------------------------------------------功能函数;从队头中删除 i 个元素-------------------------------------------------*///LinkQueue* DeleteAny (LinkQueue *Queue, int any) //返回新的链表 void DeleteAny (LinkQueue *Queue){if(IsEmpty(Queue)){return NULL;}else{int i=0,length=0;length = GetQueueLength(Queue);printf("请输入要删除元素的个数(范围:0 < i <= %d ):",length);scanf("%d",&i); while(i<=0||i>length){printf("输入范围不对,请重新输入");scanf("%d",&i);}//删除然后打印,或者储存到新的链表中已备后用 printf("删除元素为:");while(i){printf("%d ",DeleteQueue(Queue));i--; } }}
/*--------------------------------------------------功能函数;向队尾中添加 any 个元素-------------------------------------------------*/void AddAny (LinkQueue *Queue){printf("请输入要添加的数字(以空格区分不同数字,注意:请按换行后Ctrl+Z再回车结束输入):\n");ElementType temp;while(scanf("%d",&temp) != EOF) //若不能安全结束循环会造成:内存满->电脑死机!!! AddQueue (Queue,temp);printf("添加成功!\n"); }
0 0
- 链式队列的基础操作实现(C语言)
- 队列的链式结构C语言实现
- 队列的链式实现(C语言)
- 队列的链式存储实现c语言
- 数据结构实现链式队列(C语言)
- 链式循环队列实现(C语言)
- 数据结构——队列的链式实现(C语言)
- C语言一个队列的实现(链式)
- C语言实现链式队列
- C语言实现链式队列
- c语言实现链式队列
- 链式队列的实现(c++)
- 链式队列的操作实现
- 数据结构---C语言之队列的链式表示和实现(链队列)
- (C语言)队列的链式实现(数据结构十一)
- C链式队列的实现
- 队列(C语言实现,基于链式结构)
- C语言实现一个链式队列
- javaweb中使用过滤器解决乱码问题
- WMS仓库管理系统---(2)供应商模块
- 面向对象编程之抽象
- SkipList 跳表
- 网络编程中的wait和waitpid
- 链式队列的基础操作实现(C语言)
- GCDAsyncSocket 笔记
- ubifs and ubi and mtd
- 输入一行字符串,统计其中有多少个单词,单词之间用一个或者多个空格分隔开
- window下用PyInstaller把python文件 打包成exe文件
- 16线程同步和异步套接字
- ios 录制视频
- 【Android开发】边距及scrollTo相关
- 我的天,远程沟通真心困难