队列的建立与输出问题,郁闷中........
#include <stdio.h>
#include <stdlib.h>
struct lnode
{
char data;
struct lnode *next;
};
struct queuelk
{
struct lnode *front;
struct lnode *rear;
};
int main()
{
struct queuelk *hq=NULL;
int a=0;
void creat_queue(struct queuelk *hq);
void insert_queue(struct queuelk *hq,int a);
void output_queue(struct queuelk *hq);
creat_queue(hq);
printf( "scanf the elem end with #:\n ");
while(a!= '# ')
{
a=getchar();
insert_queue(hq,a);
}
output_queue(hq);
getchar();
getchar();
return 0;
}
void creat_queue(struct queuelk *hq)
{
hq-> front=hq-> rear=NULL;
printf( "creat queue successfully!\n ");
}/* creat the term*/
void insert_queue(struct queuelk *hq,int a)
{
struct lnode *newl=NULL;
newl=(struct lnode *)malloc(sizeof(struct lnode));
newl-> next=NULL;
newl-> data=a;
if(!newl)
{
printf( "error!\n ");
exit (0);
}
if(hq-> rear==NULL)
hq-> front=hq-> rear=newl;
else
hq-> rear=hq-> rear-> next=newl;
}
/*insert elem*/
void output_queue(struct queuelk *hq)
{
struct lnode *q;
q=hq-> front;
if(hq-> front==NULL)
{
printf( "error!\n ");
exit (0);
}
printf( "the data is:%s\n ",q-> data);
hq-> front=q-> next;
free(q);
/*while(hq-> rear!=hq-> front)
{
}*/
hq-> front=hq-> rear=NULL;
}
/*out put the queue term*/
哪位大侠帮看看这堆该死的代码错在哪了? 为什么老死循环呢?感觉函数没什么错误啊,本人菜鸟幼稚的地方还请包涵
[解决办法]
我该了,你看一下
#include <stdio.h>
#include <stdlib.h>
struct lnode
{
char data;
struct lnode *next;
};
struct queuelk
{
struct lnode *front;
struct lnode *rear;
};
int main()
{
struct queuelk *hq=(struct queuelk *)malloc(sizeof(struct queuelk));;
int a=0;
void creat_queue(struct queuelk *hq);
void insert_queue(struct queuelk *hq,int a);
void output_queue(struct queuelk *hq);
creat_queue(hq);
printf( "scanf the elem end with #:\n ");
a=getchar();
while(a!= '# ')
{
insert_queue(hq,a);
a=getchar();
}
output_queue(hq);
getchar();
return 0;
}
void creat_queue(struct queuelk *hq)
{
hq-> front=hq-> rear=NULL;
printf( "creat queue successfully!\n ");
}/* creat the term*/
void insert_queue(struct queuelk *hq,int a)
{
struct lnode *newl=(struct lnode *)malloc(sizeof(struct lnode));
newl-> next=NULL;
newl-> data=a;
if(!newl)
{
printf( "error!\n ");
exit (0);
}
if(hq-> rear==NULL)
hq-> front=hq-> rear=newl;
else
{
hq-> rear-> next =newl;
hq-> rear=newl;
}
}
void output_queue(struct queuelk *hq)
{
struct lnode *q;
q=hq-> front;
printf( "the data is:\n ");
while(q)
{
printf( "%c ",q-> data);
q=q-> next;
}
printf( "\n ");
}