队列的基本操作
来源:互联网 发布:九章算法强化班 云盘 编辑:程序博客网 时间:2024/04/24 17:55
这两天学习了”队列“这种数据结构,自己写了一个小程序,进行队列的一些基本操作。
与之前的链表相比,队列的操作要简单一点,它只允许在表尾插入,在表头删除。
仍然采用菜单的形式,只不过这次队列打算存人名,所以节点就采用字符数组了。
进队时要注意队列是否为空,出队时要注意队列是否只有1个节点。
程序中还设置了队列的长度上限,如果队列已满,就不再允许进队。
运行的部分截图:
完整的代码:
#include <STDIO.H>#include <STDLIB.H>#include <CONIO.H>#define MAX_SIZE 10//队列最大长度#define NAME_LENGTH 20//姓名最大长度struct node{char name[NAME_LENGTH];struct node *next;};struct queue{struct node *front;struct node *rear;int length;};typedef struct node Node;typedef struct queue Queue;void InitQueue (Queue *);bool IsQueueEmpty (const Queue *);bool IsQueueFull (const Queue *);int GetQueueLength (const Queue *);void ShowQueue (const Queue *);void ShowNode (Node *);void Insert (Queue *);void IntoQueue (Queue *, Node *);void OutOfQueue (Queue *);void ClearQueue(Queue *);void ShowMenu (void);void ClearBuffer (void);int main (void){Queue m_Queue;Queue *q;char response;q = &m_Queue;InitQueue(q);ShowMenu();while ((scanf("%c", &response) != 1) || (response != 'q')){if (response == '\r' || response == '\n'){printf("输入有误。\n");} else if (response < 'a' || response > 'e'){printf("输入有误。\n");ClearBuffer();}else{ClearBuffer();switch (response){case 'a':Insert(q);break;case 'b':OutOfQueue(q);break;case 'c':ShowQueue(q);break;case 'd':printf("队列长度为%d。\n", GetQueueLength(q));break;case 'e':ClearQueue(q);break;}}printf("\n按任意键继续...\n");getch();printf("\n");ShowMenu();}return 0;}void InitQueue (Queue *q){q->front = NULL;q->rear = NULL;q->length = 0;}bool IsQueueEmpty (const Queue *q){if (q->length == 0){return true;} else{return false;}}bool IsQueueFull (const Queue *q){if (q->length == MAX_SIZE){return true;} else{return false;}}int GetQueueLength (const Queue *q){return q->length;}void ShowQueue (const Queue *q){if (IsQueueEmpty(q)){printf("当前队列为空。\n");} else{Node *p = q->front;printf("当前队列为:\n");while (p != q->rear){ShowNode(p);printf("->");p = p->next;}ShowNode(p);printf("\n");}}void ShowNode (Node *p){int k = 0;while (p->name[k] != '\0'){putchar(p->name[k]);k++;}}void Insert (Queue *q){if (IsQueueFull(q)){printf("当前队列已满,无法进队。\n");} else{Node *p = (Node *)malloc(sizeof(Node));printf("输入姓名:");scanf("%s", p->name);ClearBuffer();p->next = NULL;IntoQueue(q, p);printf("完成进队。");ShowQueue(q);}}void IntoQueue (Queue *q, Node *p){if (IsQueueEmpty(q)){q->front = p;q->rear = p;} else{q->rear->next = p;q->rear = p;}q->length++;}void OutOfQueue (Queue *q){Node *p;if (IsQueueEmpty(q)){printf("队列为空,无法出队。\n");} else{p = q->front;if (GetQueueLength(q) == 1){q->front = NULL;q->rear = NULL;} else{q->front = p->next;}q->length--;printf("出队者的姓名是:");ShowNode(p);free(p);printf("\n出队完成。");ShowQueue(q);} }void ClearQueue(Queue *q){if (IsQueueEmpty(q)){printf("当前队列已经为空。\n");} else{Node *p;while (q->front != q->rear){p = q->front;q->front = p->next;free(p);}p = q->front;q->front = NULL;q->rear = NULL;q->length = 0;free(p);printf("已清空当前队列。\n");}}void ShowMenu (void){printf("请选择:\na.进队 b.出队 c.显示 d.长度 e.清空 q.退出\n");}void ClearBuffer (void){while (getchar() != '\n')continue;}
0 0
- 队列的基本操作
- 队列的基本操作
- 队列的基本操作
- 队列的基本操作!
- 队列的基本操作
- 队列的基本操作
- 队列的基本操作
- 队列的基本操作
- 队列的基本操作
- 队列的基本操作
- 队列的基本操作
- 队列的基本操作
- 队列的基本操作
- 队列的基本操作
- 队列的基本操作
- 队列的基本操作
- 队列的基本操作
- 队列的基本操作
- Unity接入ShareSDK的巨坑(error:redirect_uri_mismatch),第一次接入的惨状和解决办法
- Bringing up interface eth0: Device eth0 does not seem to be present, delaying initialization
- 启动VIP报CRS-1028/CRS-0223致使VIP状态为UNKNOWN故障分析与解决
- android Handler发送与处理消息小结
- 使用VC库函数中的快速排序函数
- 队列的基本操作
- Android中Bitmap、Drawable、byte[]转换
- LINUX下安装JDK
- Ngnix学习
- 【翻译自mos文章】 asmcmd cp命令不能拷贝大于2GB的文件。
- Html5 Canvas 标签
- openeim几本android应用方面优化和技巧方面的书
- 装Eclipse遇到的问题
- 白话经典算法系列之六 快速排序 快速搞定