关于用链表结构编写队列的问题,请指教
struct queue_node
{
int data;
struct queue_node * next;
};
typedef struct queue_node queue;
typedef queue * link;
link front = NULL; /*初始队头指针*/
link rear = NULL; /*初始队尾指针*/
void addqueue(int value) /*从队尾输入队列数据*/
{
link newnode;
newnode = (link) malloc(sizeof(queue));
newnode-> data = value;
newnode-> next = NULL;
if ( rear == NULL ) { /*若为队列第一个数据*/
rear = newnode; /*rear指向新结点*/
front = newnode; /*front指向新结点*/
}
else {
rear-> next = newnode; /*rear所指的结点指向新结点*/
rear = newnode; /*rear指向新结点*/
}
}
int outqueue() /*从队头输出队列数据*/
{
link top;
int temp;
if ( front != NULL ) { /*看队列是否为空*/
top = front;
front = front-> next;
temp = top-> data;
free(top);
return temp;
}
else
return -1;
}
如果我开始用addqueue(int value)输入了一个数据,那么rear和front都会指向新结点newnode,然后我再用outqueue()把这个数据输出,根据这个outqueue()函数,front指向了front-> next,也就是NULL,这个结点也被释放,可是原先也指向这个结点的rear指向什么了?
outqueue()这个函数是不是写的有问题?
[解决办法]
Yesky首页 | 产品库 | 商情 | 手机 | 数码 | 笔记本 | 台式机 | DIY硬件 | 外设 | 网络 | 数字家庭 | 评测 | 软件 | e时代 | 游戏 | 图片 | 壁纸 | 网摘 | 社区 | 博客 | 下载
北京 上海 广东
重庆 成都 沈阳 用户
登录软件频道> 程序开发> JavaVBVCDelphiC/C++Web开发微软专栏移动开发数据库程序人生软件工程|论坛下载Vista频道 您现在的位置: 天极Yesky > 软件 > 开发者网络 > 数据结构C语言实现系列——队列双核本本也能如此便宜?!咨询致电800-858-0410 全文评论数据结构C语言实现系列——队列2007-02-04 08:00 作者: 88250 出处: csdn 责任编辑:龙犊 #include <stdio.h>
#include <stdlib.h>
typedef int elemType;
/************************************************************************/
/* 以下是关于队列链接存储操作的6种算法 */
/************************************************************************/
struct sNode{
elemType data; /* 值域 */
struct sNode *next; /* 链接指针 */
};
struct queueLK{
struct sNode *front; /* 队首指针 */
struct sNode *rear; /* 队尾指针 */
};
/* 1.初始化链队 */
void initQueue(struct queueLK *hq)
{
hq-> front = hq-> rear = NULL; /* 把队首和队尾指针置空 */
return;
}
/* 2.向链队中插入一个元素x */
void enQueue(struct queueLK *hq, elemType x)
{
/* 得到一个由newP指针所指向的新结点 */
struct sNode *newP;
newP = malloc(sizeof(struct sNode));
if(newP == NULL){
printf( "内存空间分配失败! ");
exit(1);
}
/* 把x的值赋给新结点的值域,把新结点的指针域置空 */
newP-> data = x;
newP-> next = NULL;
/* 若链队为空,则新结点即是队首结点又是队尾结点 */
if(hq-> rear == NULL){
hq-> front = hq-> rear = newP;
}else{ /* 若链队非空,则依次修改队尾结点的指针域和队尾指针,使之指向新的队尾结点 */
hq-> rear = hq-> rear-> next = newP; /* 注意赋值顺序哦 */
}
return;
}
/* 3.从队列中删除一个元素 */
elemType outQueue(struct queueLK *hq)
{
struct sNode *p;
elemType temp;
/* 若链队为空则停止运行 */
if(hq-> front == NULL){
printf( "队列为空,无法删除! ");
exit(1);
}
temp = hq-> front-> data; /* 暂存队尾元素以便返回 */
p = hq-> front; /* 暂存队尾指针以便回收队尾结点 */
hq-> front = p-> next; /* 使队首指针指向下一个结点 */
/* 若删除后链队为空,则需同时使队尾指针为空 */
if(hq-> front == NULL){
hq-> rear = NULL;
}
free(p); /* 回收原队首结点 */
return temp; /* 返回被删除的队首元素值 */
}
/* 4.读取队首元素 */
elemType peekQueue(struct queueLK *hq)
{
/* 若链队为空则停止运行 */
if(hq-> front == NULL){
printf( "队列为空,无法删除! ");
exit(1);
}
return hq-> front-> data; /* 返回队首元素 */
}
/* 5.检查链队是否为空,若为空则返回1, 否则返回0 */
int emptyQueue(struct queueLK *hq)
{
/* 判断队首或队尾任一个指针是否为空即可 */
if(hq-> front == NULL){
return 1;
}else{
return 0;
}
}
/* 6.清除链队中的所有元素 */
void clearQueue(struct queueLK *hq)
{
struct sNode *p = hq-> front; /* 队首指针赋给p */
/* 依次删除队列中的每一个结点,最后使队首指针为空 */
while(p != NULL){
hq-> front = hq-> front-> next;
free(p);
p = hq-> front;
} /* 循环结束后队首指针已经为空 */
hq-> rear = NULL; /* 置队尾指针为空 */
return;
}
/************************************************************************/
int main(int argc, char* argv[])
{
struct queueLK q;
int a[8] = {3, 8, 5, 17, 9, 30, 15, 22};
int i;
initQueue(&q);
for(i = 0; i < 8; i++){
enQueue(&q, a[i]);
}
printf( "%d ", outQueue(&q)); printf( "%d ", outQueue(&q));
enQueue(&q, 68);
printf( "%d ", peekQueue(&q)); printf( "%d ", outQueue(&q));
while(!emptyQueue(&q)){
printf( "%d ", outQueue(&q));
}
printf( " ");
clearQueue(&q);
system( "pause ");
}