返回信息流[QUOTE]实验题目:
假设以带头结点的循环链表表示队列,并且只设一个指针指向队尾元素结点而不设头指针,试编写相应的队列初始化、入队列、出队列和判断队列状态的算法。
利用上述算法完成下面的各操作,并在每一操作后输出队列状态。
1)下列元素逐一入队:5,7,3,8,55 状态:5个元素
2)3个元素出队 状态:2个元素
3)再2个元素出队 状态:队空
4)再1个元素出队 状态:队空(指示下溢)
下面是我的程序:
#include<stdio.h>
#define QUEUE_NULL 0
#define OVERFLOW 1
#define QUEUE_OK 2
typedef int status;
struct QueueList{
int data;
struct QueueList* next;
};
typedef struct QueueList QList;
typedef QList* QListPtr;
void init(QListPtr *);
status QueueIn(QListPtr *,int);
status QueueOut(QListPtr *,int);
void ShowStatus(QListPtr,status);
void ShowData(QListPtr);
main()
{
int choice,n;
status s;
QListPtr p;
init(&p);
printf("enter the number of elements you want in\n");
scanf("%d",&n);
s=QueueIn(&p,n);
ShowStatus(p,s);
ShowData(p);
printf("enter the number of elements you want out\n");
scanf("%d",&n);
s=QueueOut(&p,n);
ShowStatus(p,s);
ShowData(p);
printf("enter the number of elements you want out\n");
scanf("%d",&n);
s=QueueOut(&p,n);
ShowStatus(p,s);
ShowData(p);
printf("enter the number of elements you want out\n");
scanf("%d",&n);
s=QueueOut(&p,n);
ShowStatus(p,s);
ShowData(p);
system("pause");
return 0;
}
void init(QListPtr *pp)
{
*pp=(QListPtr)malloc(sizeof(QList));
(*pp)->next=*pp;
(*pp)->data=0;
}
status QueueIn(QListPtr *pp,int n)
{
if((*pp)->next->data==-1)
return OVERFLOW;
QListPtr t,tt;
int i=0,e;
printf("input %d element(s)\n",n);
while(i!=n)
{
scanf("%d",&e);
t=(QListPtr)malloc(sizeof(QList));
t->data=e;
tt=(*pp)->next;
(*pp)->next=t;
t->next=tt;
*pp=t;
(*pp)->next->data++;
i++;
}
if((*pp)->next->data==0)
return QUEUE_NULL;
return QUEUE_OK;
}
status QueueOut(QListPtr *pp,int m)
{
if((*pp)->next->data<=0)
{
if(m==0&&(*pp)->next->data==0)
return QUEUE_NULL;
else
{
(*pp)->next->data=-1;
return OVERFLOW;
}
}
QListPtr t=(*pp)->next->next,tt;
int i=0;
while(i!=m &&(*pp)->next->data>0)
{
tt=t;
t=t->next;
(*pp)->next->data--;
(*pp)->next->next=t;
free(tt);
i++;
}
if((*pp)->next->data==0&&i==m)
return QUEUE_NULL;
if(i<m)
{
(*pp)->next->data=-1;
return OVERFLOW;
}
return QUEUE_OK;
}
void ShowStatus(QListPtr p,status s)
{
if(s==QUEUE_OK)
printf("%d ELEMENT(S) REMAIND\n",p->next->data);
if(s==QUEUE_NULL)
printf("QUEUE NULL\n");
if(s==OVERFLOW)
printf("OVER FLOW\n");
}
void ShowData(QListPtr p)
{
if(p->next->data==-1)
printf("OVER FLOW\n");
else if(p->next->data==0)
printf("QUEUE NULL\n");
else
{
printf("ELEMENT(S): ");
QListPtr t;
t=p->next->next;
while(p->next!=t)
{
printf("%d, ",t->data);
t=t->next;
}
printf("\n");
}
}
这是一条镜像帖。来源:北邮人论坛 / cpp / #14108同步于 2008/10/15
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
[求助] 一道关于数据结构的C语言题(我实在没办法了)
shuwn
2008/10/15镜像同步11 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
好像这句出错了: free(tt);不能释放...
提个小建议:status QueueOut(QListPtr *pp,int m)我觉得用引用好点,status QueueOut(QListPtr &pp,int m),里面就不需要(*pp)了,可以直接用PP了
还没有试 但是非常感谢哈 谢谢大牛
【 在 newstar19870 的大作中提到: 】
: 好像这句出错了: free(tt);不能释放...
: 提个小建议:status QueueOut(QListPtr *pp,int m)我觉得用引用好点,status QueueOut(QListPtr &pp,int m),里面就不需要(*pp)了,可以直接用PP了
你的建议我改了 还是过不了 我觉得free 没问题
哦 你可以看看运行结果。 谢谢了
【 在 newstar19870 的大作中提到: 】
: 好像这句出错了: free(tt);不能释放...
: 提个小建议:status QueueOut(QListPtr *pp,int m)我觉得用引用好点,status QueueOut(QListPtr &pp,int m),里面就不需要(*pp)了,可以直接用PP了
一直没看明白
你不是已经都typedef Qlist * QlistPtr了么?
怎么下面在编码的时候还用QlistPtr *
难道你要用双重指针?
恩 我在书上看的
【 在 ericyosho 的大作中提到: 】
: 一直没看明白
: 你不是已经都typedef Qlist * QlistPtr了么?
: 怎么下面在编码的时候还用QlistPtr *
: ...................