第8章第16题的完整程序(Powered by biggates)
来源:互联网 发布:rsyslog mysql乱码 编辑:程序博客网 时间:2024/05/17 07:40
/// 8_16.cpp : 假设以数组sequ[m]存放循环队列的元素,同时设变量
/// rear 和quelen 分别指示循环队列中队尾元素的位置和内含元素的个数。
/// 试给出判别此循环队列的队满条件,并写出相应的入队列和出队列的算法。
/// 说明:本程序之所以不用typedef 把char定义成datatype,是因为:
/// 在C语言中没有输入输出的重构函数,所以即使把char定义成datatype,
/// 在处理输入输出的时候仍然要面对众多的 %d、%c之类,反而容易引起错误
/// 因此,为了使问题更加便于理解,采用了char而不是datatype。
#include "stdafx.h"
#include <stdlib.h>
#include <conio.h>
const int MAXSIZE = 10;
typedef 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[
(head->rear - head->quelen + i + 1 + MAXSIZE) % MAXSIZE
]);
//注意这句,在纸上画一下比较好理解
}
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->sequ[head->rear + 1] = 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[(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;
}
- 第8章第16题的完整程序(Powered by biggates)
- 第8章第11题的完整程序(Powered by biggates)
- 第10章第16题完整代码(cpp) (Powered by biggates)
- 第11章第15题完整代码(cpp) (Powered by biggates)
- 第10章第16题定义(h) (Powered by biggates)
- 第11章第15题定义代码(h) (Powered by biggates)
- 第4周项目6 point类的完整程序
- 第13章 一个完整的例子
- 新的BMW Stdio的Logo (posted by biggates)
- 第六章31题全部代码(powered by spirit_only)
- 第六章22题全部代码(powered by spirit_only)
- 深入浅出MFC学习笔记(第7章:简单而完整的MFC骨干程序)
- 深入浅出MFC学习笔记(第7章:简单而完整的MFC骨干程序)
- 算法导论第6章实现堆排序的完整程序
- 隐藏PHP程序头部发出的:X-Powered-By: PHP/5.2.4类似的信息
- Powered by django的中文站点
- x-powered-by 隐藏 php的版本号
- 实验7_3 by biggates
- 不是一番寒彻骨,哪得梅花扑鼻香
- 网络安装操作系统
- NET經典代碼
- 可怕的大头贴诅咒
- SAS9 ODBC使用注意
- 第8章第16题的完整程序(Powered by biggates)
- 简明x86汇编语言教程
- 第八章十六题修改后POWERBY KTL
- 气泡法排序 输入N个数字 由小到大排序
- 用SQL语句对数据库字段值做批量修改
- 前公司在制作播客系统(Web程序)中,用到从视频截图功能.
- Linux必学的60个命令
- 如何才能去做喜欢的事情
- Java核心思想学习笔记003(Object Construction )