链队列的定义及相关操作
来源:互联网 发布:soap rest 知乎 编辑:程序博客网 时间:2024/05/16 15:56
#include <stdio.h>#include <malloc.h>typedef struct node{ int data; node *next;} Node,*LinkQueueNode;typedef struct{ LinkQueueNode front; //队头指针 LinkQueueNode rear; //队尾指针} LinkQueue;int GetLength_LinkQueue(LinkQueue *Q);//求链队列的长度bool InitQueue(LinkQueue *Q);//链队列初始化bool EnterQueue(LinkQueue *Q,int x);//入队,将数据元素x插入到队列Q中bool DeleteQueue(LinkQueue *Q,int *x);//出队,将队列Q的队头元素出队并保存到x所指的存储空间中bool IsEmpty_LinkQueue(LinkQueue *Q);//判断链队列是否为空void Destroy_LinkQueue(LinkQueue *Q);//销毁链队列void Clear_LinkQueue(LinkQueue *Q);//清空链队列void GetHead_LinkQueue(LinkQueue *Q,int *x);//取得链队列的头部元素void GetRear_LinkQueue(LinkQueue *Q,int *x);//取得链队尾的头部元素void Print_LinkQueue(LinkQueue *Q);//输出链队列int main(void){ int x; int *a=&x; LinkQueue Q; LinkQueue *q=&Q; InitQueue(q); if(IsEmpty_LinkQueue(q)) printf("该队列为空\n"); for(int i=1; i<=10; i++) EnterQueue(q,i); printf("队列中有%d个元素\n",GetLength_LinkQueue(q)); Print_LinkQueue(q); GetHead_LinkQueue(q,a); printf("队头元素为%d\n",x); GetRear_LinkQueue(q,a); printf("队尾元素为%d\n",x); for(int i=1; i<=10; i++) { DeleteQueue(q,a); printf("删除元素%d ",x); } printf("\n"); Clear_LinkQueue(q); if(IsEmpty_LinkQueue(q)) printf("该队列为空\n"); return 0;}int GetLength_LinkQueue(LinkQueue *Q)//求链队列的长度{ int count = 0; LinkQueueNode p = Q->front->next;//指向存放数据的第一个结点 while(p) { count++; p = p->next; } return count;}bool InitQueue(LinkQueue *Q)//链队列初始化{ Q->front=(LinkQueueNode)malloc(sizeof(Node)); if(Q->front!=NULL) { Q->rear=Q->front; Q->front->next=NULL; return true; } else return false;//溢出}bool EnterQueue(LinkQueue *Q,int x)//入队,将数据元素x插入到队列Q中{ LinkQueueNode NewNode; NewNode=(LinkQueueNode)malloc(sizeof(Node)); if(NewNode!=NULL) { NewNode->data=x; NewNode->next=NULL; Q->rear->next=NewNode; Q->rear=NewNode; } else return false;//溢出}bool DeleteQueue(LinkQueue *Q,int *x)//出队,将队列Q的队头元素出队并保存到x所指的存储空间中{ LinkQueueNode p; if(Q->front==Q->rear)//表示队列已空 return false; p=Q->front->next; Q->front->next=p->next;//队头元素p出队 if(Q->rear==p)//如果队中只有一个元素p,则p出队后成为空队 Q->rear=Q->front; *x=p->data; free(p);//释放存储空间 return true;}bool IsEmpty_LinkQueue(LinkQueue *Q)//判断链队列是否为空{ return Q->front == Q->rear;}void Destroy_LinkQueue(LinkQueue *Q)//销毁链队列{ while(Q->front)//从头结点开始释放链队列中所有的结点 { Q->rear = Q->front->next; free(Q->front); Q->front = Q->rear; }}void Clear_LinkQueue(LinkQueue *Q)//清空链队列{ Destroy_LinkQueue(Q); InitQueue(Q); printf("队列已清空\n");}void GetHead_LinkQueue(LinkQueue *Q,int *x)//取得链队列的头部元素{ if(IsEmpty_LinkQueue(Q)) printf("链队列为空!\n"); else *x = Q->front->next->data;}void GetRear_LinkQueue(LinkQueue *Q,int *x)//取得链队尾的头部元素{ if(IsEmpty_LinkQueue(Q)) printf("链队列为空!\n"); else *x = Q->rear->data;}void Print_LinkQueue(LinkQueue *Q)//输出链队列{ LinkQueueNode p = Q->front->next;//p指向头结点的下一个结点,即存放数据的第一个结点 if(IsEmpty_LinkQueue(Q)) printf("链队列为空!\n"); else { while(p) { printf("%d ",p->data); p = p->next; } printf("\n"); }}
阅读全文
1 0
- 链队列的定义及相关操作
- 链栈的定义及相关操作
- 循环队列的定义及操作
- 顺序栈的定义及相关操作
- 数据结构(9)--链队列的定义以及相关操作的实现
- 普通队列,循环队列以及链队列的相关操作
- 队列的相关操作
- 队列的相关操作
- 队列的相关操作
- 队列的相关操作
- PHP数组的定义及相关的操作函数
- 静态循环队列的相关操作及详解
- 静态循环队列的相关操作及详解
- 定义一个过滤器Filter及Filter提供的相关操作
- 循环队列的相关操作
- 循环队列的相关操作
- js定义数组及相关操作
- js定义数组及相关操作
- QML ListView悬浮标题栏
- Qt学习笔记1:初步认识控件
- Java重写与重载
- PyQt5学习记录(2)---QMainWindow菜单栏、状态栏和工具栏
- VMware虚拟机三种网络模式详解-Host-Only(仅主机模式)
- 链队列的定义及相关操作
- 44. Struts2_表单的重复提交问题
- Eclipse实用快捷键个人总结
- Makefile中的死循环问题--学习李云《驾驭Makefile》中的疑惑解决
- jsp所需基础
- IntersectVisitor类
- python 电路绘制库 schemdraw
- day23-linux
- Android WiFi--系统架构