链队列的问题!

时间一粒 2010-05-24 11:39:39
/*用单链表实现队列,如下图所示,并令front=rear=NULL表示队列为空,编写实现队列
的如下五种运算的函数。

Setnull:将队列置成空队列。

getfirst:返回队列的第一个元素。

enqueue:把元素插入队列的后端。

dequeue:删除队列的第一个元素。

empty:判定队列是否为空。*/

#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define OVERFLOW -2
#define ERROR 0

typedef int ElemType;
typedef int Status;

typedef struct QNode{
ElemType data;
struct QNode *next;
} QNode, *QueuePtr;

typedef struct{
QueuePtr front;
QueuePtr rear;
}LinkQueue;

//判空
Status empty(LinkQueue Q)
{
if(Q.front==Q.rear) return OK;
else return ERROR;
}
//置空
Status SetnullQueue(LinkQueue &Q)
{
Q.front=Q.rear=NULL;
printf("队列为空!\n");
exit(OVERFLOW);
}
//入队
Status EnQueue(LinkQueue &Q,ElemType e)
{
QueuePtr p=(QueuePtr)malloc(sizeof(QNode));
if(!p)exit(OVERFLOW);
p->data=e;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
return OK;
}
//出队
Status OutQueue(LinkQueue Q,ElemType &e)
{
QueuePtr p;
if(empty(Q))
{ exit(OVERFLOW);}//下溢
else
{
p=Q.front->next;
Q.front->next=p->next;
e=p->data;
free(p);
if(Q.front->next==NULL)
Q.rear=Q.front ;
return OK;
}
//return e;//返回原队头数据
}
///创建队列
Status InitQueue(LinkQueue &Q,int n)
{
Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
if(!Q.front)exit(OVERFLOW);
Q.front->next=NULL;
//Q.rear->next=NULL;
ElemType e;
for(int i=0;i<n;i++)
{
scanf("%d",&e);
EnQueue(Q,e);
}
return OK;
}
//取出队头元素
Status getfirst(LinkQueue Q,ElemType &e)
{
QueuePtr p;
if(empty(Q))
{ exit(OVERFLOW);}//下溢
else{
p=Q.front->next;
e=p->data;
return e;
}
return OK;
}
//删除队头元素
Status dequeue(LinkQueue Q)
{
ElemType e;
QueuePtr p;
if(empty(Q)){exit(OVERFLOW);}
else {
p=Q.front->next;
e=p->data;
Q.front->next=p->next;
if(Q.rear==p) Q.rear=Q.front;
free(p);
//printf("%d",e);
}
printf("\n");
return OK;
}
//插入元素
Status enqueue(LinkQueue &Q,ElemType e)
{
QueuePtr p=(QueuePtr)malloc(sizeof(QNode));
if(!p)exit(OVERFLOW);
p->data=e;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
return OK;
}
//输出
Status QPrint(LinkQueue Q,int n)
{
ElemType e;
printf("队的元素为:");
for(int k=0;k<n;k++)
{
OutQueue(Q,e);
printf("%d ",e);
}
}
//主函数
int main()
{
int n;
ElemType e;
LinkQueue Q;

printf("Input the number you need:");
scanf("%d",&n);
InitQueue(Q,n);
//SetnullQueue(Q);
getfirst(Q,e);
printf("队头元素:%d\n",e);

printf("请输入要插入的元素:");
scanf("%d",&e);
enqueue(Q,e);
//n=;
QPrint(Q,n+1);



printf("删除队头元素:");
dequeue(Q);
//n=n-1;
QPrint(Q,n-1);

//empty(Q);

printf("\n");
system("pause");
}

运行结果出错!



...全文
200 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuanzhang198711 2010-05-25
  • 打赏
  • 举报
回复
这是我临时做的一个链队的程序。

/*queue.c*/
#include <stdio.h>
#include <stdlib.h>

typedef struct Node *link;


struct Node {
unsigned char item;
link next; /*pointer the next node */
};

static link head = NULL; /*create the head node of list,and it is null */

static link front = NULL; /*front pointer of queue */

/*create a simple node,it is not in the list*/
link make_node(unsigned char item) {
link p = malloc(sizeof(*p));

p->item = item;
p->next = NULL;
return p;
}

/*release the space of the node*/
void free_node(link p)
{
free(p);
}

/*clear all the node,and release the space*/
void destroy(void)
{
link q, p = head;

head = NULL;
while (p) {
q = p;
p = p->next;
free_node(q);
}
}

/*add a node to the tail*/
void add(link p){
if(head==NULL){
head=p;
}
else{
link q=head;
link r;
/*find the tail*/
while(q){
r=q->next;/*record the tail node*/
q=q->next;
}
/*add the node to the tail*/
p=r;
}
}

/*delete a node in the list*/
void delete(link p)
{
link pre;

if (p == head) {
head = p->next;
}

/*find the node ,then delete it */
for (pre = head; pre; pre = pre->next)
if (pre->next == p) {
pre->next = p->next;
return;
}

}//end list.

/*region of queue*/

//enqueue,or let node in the queue.
void enqueue(unsigned char item) {
link p = make_node(item);
if (front == NULL) {
front = p;
}
add(p);
printf("enqueue success!\n");
}

//dequeue,ro let node out the queue.
void dequeue() {
if (front == NULL) {
link tmp = front;
delete(front);
front = tmp->next;
printf("dequeue a front is :%d\n", tmp->item);
}
printf("queue has no items!\n");
}

//get the front node.
unsigned char peek() {
if (front != NULL) {
return front->item;
} else {
printf("queue has no items!\n");
}
}

//clear all node.
void dequeueAll() {
destroy();
}

void print_item(link p) {
printf("%d\n", p->item);
}

int main(void) {
enqueue(200);
enqueue(100);
enqueue(55);
printf("The front of the queue is %d\n", peek());
return 0;
}
时间一粒 2010-05-25
  • 打赏
  • 举报
回复
谢谢各位帮忙!
Awang_126 2010-05-25
  • 打赏
  • 举报
回复
#include <stdio.h>
#include<stdlib.h>
#define OK 1
#define OVERFLOW -2
#define ERROR 0

typedef int ElemType;
typedef int Status;

typedef struct QNode{
ElemType data;
struct QNode *next;
} QNode, *QueuePtr;

typedef struct{
QueuePtr front;
QueuePtr rear;
}LinkQueue;

//判空
Status empty(LinkQueue Q)
{
if(Q.front==Q.rear) return OK;
else return ERROR;
}
//置空
Status SetnullQueue(LinkQueue &Q)
{
Q.front=Q.rear=NULL;
printf("队列为空!\n");
exit(OVERFLOW);
}
//入队
Status EnQueue(LinkQueue &Q,ElemType e)
{
QueuePtr p=(QueuePtr)malloc(sizeof(QNode));
if(!p)exit(OVERFLOW);
p->data=e;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
return OK;
}
//出队
Status OutQueue(LinkQueue Q,ElemType &e)
{
QueuePtr p;
if(empty(Q))
{ exit(OVERFLOW);}//下溢
else
{
p=Q.front->next;
Q.front->next=p->next;
e=p->data;
free(p);
if(Q.front->next==NULL)
Q.rear=Q.front ;
return OK;
}
//return e;//返回原队头数据
}
///创建队列
Status InitQueue(LinkQueue &Q,int n)
{
Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
if(!Q.front)exit(OVERFLOW);
Q.front->next=NULL;
//Q.rear->next=NULL;
ElemType e;
for(int i=0;i<n;i++)
{
scanf("%d",&e);
EnQueue(Q,e);
}
return OK;
}
//取出队头元素
Status getfirst(LinkQueue Q,ElemType &e)
{
QueuePtr p;
if(empty(Q))
{ exit(OVERFLOW);}//下溢
else{
p=Q.front->next;
e=p->data;
return e;
}
return OK;
}
//删除队头元素
Status dequeue(LinkQueue Q)
{
ElemType e;
QueuePtr p;
if(empty(Q)){exit(OVERFLOW);}
else {
p=Q.front->next;
e=p->data;
Q.front->next=p->next;
if(Q.rear==p) Q.rear=Q.front;
free(p);
//printf("%d",e);
}
printf("\n");
return OK;
}
//插入元素
Status enqueue(LinkQueue &Q,ElemType e)
{
QueuePtr p=(QueuePtr)malloc(sizeof(QNode));
if(!p)exit(OVERFLOW);
p->data=e;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
return OK;
}
//输出
void QPrint(LinkQueue Q,int n)
{
ElemType e;
printf("队的元素为:");
for(int k=0;k<n;k++)
{
OutQueue(Q,e);
printf("%d ",e);
}
}
//主函数
void main()
{
int n;
ElemType e;
LinkQueue Q;

printf("Input the number you need:");
scanf("%d",&n);
InitQueue(Q,n);
//SetnullQueue(Q);
getfirst(Q,e);
printf("队头元素:%d\n",e);

printf("请输入要插入的元素:");
scanf("%d",&e);
enqueue(Q,e);
//n=;
QPrint(Q,n+1);



//printf("删除队头元素:");
// dequeue(Q);
// n=n-1;
// QPrint(Q,n-1);

//empty(Q);

printf("\n");
system("pause");
}
Awang_126 2010-05-25
  • 打赏
  • 举报
回复
因为你在 QPrint(Q,n+1)中边显示每个节点数据,同时释放其空间,
所以你后面再执行
printf("删除队头元素:");
dequeue(Q);
//n=n-1;
QPrint(Q,n-1); //这里有问题 之前显示的时候已经释放空间了,你这里却还想显示,故有问题


OnlyOB 2010-05-25
  • 打赏
  • 举报
回复
形参表中的取地址符再检查下
yuanzhang198711 2010-05-25
  • 打赏
  • 举报
回复
有点错误,是你定义的函数中的形参表中的取地址符。
yuanzhang198711 2010-05-25
  • 打赏
  • 举报
回复
去掉程序中所有的取地址符,基本上已经没什么问题。至于道理,你自己想想,在指针结构面前加&,你的目的是什么?你是传参吗?

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧