队列操作--出队入队
来源:互联网 发布:伦敦黄金交易软件 编辑:程序博客网 时间:2024/04/29 23:06
/// : 假设以数组sequ[m]存放循环队列的元素,同时设变量/// rear 和quelen 分别指示循环队列中队尾元素的位置和内含元素的个数。/// 试给出判别此循环队列的队满条件,并写出相应的入队列和出队列的算法。/// 说明:本程序之所以不用typedef 把char定义成datatype,是因为:/// 在C语言中没有输入输出的重构函数,所以即使把char定义成datatype,/// 在处理输入输出的时候仍然要面对众多的 %d、%c之类,反而容易引起错误/// 因此,为了使问题更加便于理解,采用了char而不是datatype。#include <stdio.h>#include <stdlib.h>#include <conio.h>#define MAXSIZE 10typedef struct sequeue{ char sequ[MAXSIZE]; int rear; int quelen;}sequeue;// 显示队列的函数void PrintQueue(sequeue* head){ if(head == NULL || head->quelen <= 0) { printf("这是一个空列表。\n"); return; } for(int i = 0; i < head->quelen; i++) { printf("%c", head->sequ[i // (head->rear - head->quelen + i + 1 + MAXSIZE) % MAXSIZE ]); //注意这句,在纸上画一下比较好理解///i就可以了 } printf("\n"); return;}//判断队列是否满的函数bool IsFull(sequeue* head){ if (head == NULL) { printf("队列不存在!\n"); //返回真,防止在非法队列中进行的入队操作 //因为一般情况下不会对一个满队列进行入队 return true; } if (head->quelen == MAXSIZE) { return true; } else { return false; }}//入队函数char* EnQueue(sequeue* head, char data){ printf("入队:%c\t\t", data); if (IsFull(head)) { printf("入队失败!\n"); return NULL; } if (head->rear + 1 >= MAXSIZE) { head->rear -= MAXSIZE; } head->rear += 1; head->sequ[head->rear ] = data; // head->rear += 1; head->quelen += 1; PrintQueue(head); return &(head->sequ[head->rear - 1]);}//出队函数char DeQueue(sequeue* head){ printf("出队:"); if (head->quelen <= 0) { printf("出队失败!\n"); return -1; } int tempdata = head->sequ[1//(MAXSIZE + head->quelen - head->rear + 1) % MAXSIZE]; head->quelen -= 1; if(head->rear >= MAXSIZE) { head->rear -= MAXSIZE; } printf("%c\t\t", tempdata); PrintQueue(head); return tempdata;}//带提示的出队函数char DeQueueWithData(sequeue* head){ char temp = DeQueue(head); if(temp != -1) { printf("%c已经出队\n", temp); } return temp;}//带提示的入队函数void EnQueueWithData(sequeue* head){ char temp; printf("请输入一个字母:"); temp = getche(); printf("\n"); EnQueue(head, temp);}//队列初始化函数void InitialQueue(sequeue* head){ head->quelen = 0; head->rear = MAXSIZE - 1; for(int i = 0; i < MAXSIZE; i ++) { head->sequ[i] = 0; }}//输入字符串的函数sequeue* InputList(void){ sequeue* head = NULL; head = (sequeue*)malloc(sizeof(sequeue)); if (head == NULL) { printf("内存分配出错!\n"); return NULL; } InitialQueue(head); char ch = ' '; printf("请输入一个字符串:\n"); ch = getchar(); while(ch != '\n' && !IsFull(head)) { EnQueue(head, ch); ch = getchar(); } return head;}int main(int argc, char* argv[]){ sequeue* head = NULL; char ch = 'y'; head = InputList(); PrintQueue(head); while(ch != 27) { printf("请输入E入队,D出队,ESC退出\n"); ch = getch(); switch(ch) { case 'd': case 'D': DeQueueWithData(head); break; case 'e': case 'E': EnQueueWithData(head); break; default: break; } } return 0;}
0 0
- 队列操作--出队入队
- 链式队列操作,初始化,入队,出队
- 编程实现队列入队出队操作
- 队列的入队、出队操作实现
- 队列基本操作 出队与入队
- 队列的出队/入队的操作
- 队列的入队和出队操作
- 队列的入队/出队操作
- 队列的入队、出队操作
- 队列的入队、出队基本操作
- 队列操作 出队与入队
- 队列的入队出队操作
- 链式队列的入队出队操作
- 队列之出队入队
- 队列的入队,出队,测长,打印操作
- 队列的入队,出队,测长,打印操作 .
- (10.15)[转]C++学习(队列操作,入队,出队)
- 【数据结构】队列的出队和入队操作
- File was rejected by digital signature policy
- 交行网银:解析证书DN异常
- phpmyadmin 提示 PHP 5.2+ is required 的解决办法
- CentOS 5.4 PHP MySQL的升級/降级
- 找回Word 2000的菜单栏
- 队列操作--出队入队
- Cisco3550日常管理笔记
- [ZT]linux+squid利用Layer7限制迅雷下载、QQ、MSN方法
- Mysql “Table 'mysql.servers' doesn't exist”
- [ZT]Cisco路由器的备份配置方法
- 千兆网线的制作方法和千兆网线的施工注意事项
- Cluster群集三类:NLB,CLB,MSCS
- 如何建立一个单独的普通用户加域
- 启用OpenLDAP的日志功能