鱼C论坛

 找回密码
 立即注册
查看: 3811|回复: 7

队 的问题

[复制链接]
发表于 2013-10-8 22:45:44 | 显示全部楼层 |阅读模式
1鱼币
# include <stdio.h>
# include <MALLOC.H>

typedef struct Queue
{
int *Base;//存放数据的数组
int front;
int rear;
}QUEUE, *PQUEUE;
void init(PQUEUE);//初始化
void en_queue(PQUEUE, int);//入队
void traveres(PQUEUE);//遍历
void out_queue(PQUEUE);//出队
bool full_queue(PQUEUE);//是否为满
bool empty_queue(PQUEUE);//是否为空

int main(void)
{
QUEUE queue;//建立一个队列并没有初始化
init(&queue);
en_queue(&queue, 1);
en_queue(&queue, 2);
en_queue(&queue, 3);
en_queue(&queue, 4);
en_queue(&queue, 5);
printf("%d\n", queue.Base[4]);
//只能存放5个值以为有一个值当做未满所以有效元素为6-1
traveres(&queue);
out_queue(&queue);
traveres(&queue);
return 0;
}
void init(PQUEUE Pqueue)
{
Pqueue->Base = (int *)malloc(sizeof(int) * 6);
Pqueue->front = 0;//相等为空
Pqueue->rear = 0;//
if (Pqueue->Base == NULL)
{
  printf("初始化失败\n");
}
else
{
  printf("初始化成功\n");
}
}
bool full_queue(PQUEUE Pqueue)
{
if ((Pqueue->rear + 1) % 6 == Pqueue->front)
{
  return true;
}
else
{
  return false;
}
}
void en_queue(PQUEUE Pqueue, int val)
{
if (full_queue(Pqueue))
{
  printf("队列已满\n");
}
else
{
  Pqueue->Base[Pqueue->rear] = val;
  Pqueue->rear = (Pqueue->rear + 1) % 6;
}
}
void traveres(PQUEUE Pqueue)
{
int i = Pqueue->front;
while(i != Pqueue->rear)
{
  printf("%d\n", Pqueue->Base[i]);
  i = (i + 1) % 6;
}
printf("\n");
}
bool empty_queue(PQUEUE Pqueue)
{
if (Pqueue->front == Pqueue->rear)
{
  return true;
}
else
{
  return false;
}
}
void out_queue(PQUEUE Pqueue)
{
if (empty_queue(Pqueue))
{
  printf("队列为空\n");
}
else
{
  Pqueue->front = (Pqueue->front + 1) % 6;
}
}


这是第一个版本的代码 结果会输出1,2,3,4,5数组长度是 Pqueue->Base = (int *)malloc(sizeof(int) * 6);     6
现在我就只改一个地方 Pqueue->Base = (int *)malloc(sizeof(int) * 1);   数组长度为1
但是尼玛为什么还是输出1,2,3,4,5求大神解释啊

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-10-9 19:21:17 | 显示全部楼层
:cry:cry没人吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-10-9 23:15:10 | 显示全部楼层
帮你顶起。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-10-17 22:05:38 | 显示全部楼层
有人但我看不懂
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-10-19 13:57:03 | 显示全部楼层
好好好哈哈哈啊哈哈
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-10-25 16:00:28 | 显示全部楼层
全不懂:cry
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-11-14 18:31:26 | 显示全部楼层
你定义的是指针数组,感觉只要开辟个首地址就行了的样子下面的好像不用申请的。。也不是太清楚
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2013-11-14 18:57:40 | 显示全部楼层
说错了,你定义的是一个数组指针 *base。。开辟的是指针的的内存空间
在{
  Pqueue->Base[Pqueue->rear] = val;
  Pqueue->rear = (Pqueue->rear + 1) % 6
当Pqueue->rear + 1时就等base+1又开辟了一段空间
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-6 03:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表