在线求一程序的纠正

arkor 2011-01-06 09:26:05
代码如下:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10

typedef int Datatype;
typedef struct stack //创建一个队列
{
Datatype *elem;
int rear,front;
}Stack;

Stack Init() //初始化队列
{
Stack S;
S.front=S.rear=0;
return S;
}

void Push(Stack *S,Datatype x) //在队列中插入一个元素
{
if((S->rear+1)%SIZE==S->front)
printf("error");
else
{
S->rear=(S->rear+1)%SIZE;
S->elem[S->rear]=x;
}
}

Datatype Pop(Stack *S) //出列
{
Datatype x;
if(S->front==S->rear)
printf("error");
else
{
S->front=(S->front+1)%SIZE;
x=S->elem[S->front];
}
return x;
}

int main(void) {
Stack S; //定义一个队列
S=Init(); //初始化队列
Datatype x; //定义要插入的元素
printf("请输入队列的元素:");
while(x!=6)
{
scanf("%d",&x);
Push(&S,x); //队列中插入元素
}
printf("%4d",Pop(&S)); //输出
puts("arkor!!"); /* prints arkor!! */
return EXIT_SUCCESS;
}

当我编译时,没有错误,但就是编译不出来,在线求纠正
...全文
81 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuhex 2011-01-06
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10

typedef int Datatype;
typedef struct queue //创建一个队列
{
Datatype *elem;
int rear,front;
}queue;

bool Init(queue &S) //初始化队列
{
S.front=S.rear=0;
S.elem=new int[SIZE];
if(S.elem!=NULL)
return true;
else
return false;
}

void Push(queue *S,Datatype x) //在队列中插入一个元素
{
if((S->rear+1)%SIZE==S->front)
{
printf("overflow\n");
return ;
}


else
{
S->elem[S->rear]=x;
S->rear=(S->rear+1)%SIZE;

}
}

Datatype Pop(queue *S) //出列
{
Datatype x;
if(S->front==S->rear)
{
printf("empty\n");
return 6;
}
else
{
x=S->elem[S->front];
S->front=(S->front+1)%SIZE;

}
return x;
}

int main(void) {
queue S; //定义一个队列
Init(S);
Datatype x; //定义要插入的元素
printf("请输入队列的元素:");

do
{
scanf("%d",&x);
Push(&S,x); //队列中插入元素
}while(x!=6);
printf("%d\n",Pop(&S)); //输出
puts("arkor!!"); /* prints arkor!! */


system("pause");
return EXIT_SUCCESS;
}
你建的是循环队列,使用数组时要保留一个位置来做判断,front指向队列的第一个元素,rear指向队列最后一个元素的下一个位置,这样判断队列为空:S.front==S.rear为真即可,判断队列满了:(S.rear+1)%SIZE==S.front为真。
之前程序中有些地方不合理,按照你的意思改了改,自己看看吧。
arkor 2011-01-06
  • 打赏
  • 举报
回复
[Quote=引用楼主 arkor 的回复:]
代码如下:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10

typedef int Datatype;
typedef struct stack //创建一个队列
{
Datatype *elem;
int rear,front;
}Stack;

Stack Init……
[/Quote]
void InitQueue(LiQueue *&q),请问初始化的时候,为什么要*&q这样赋值啊。
arkor 2011-01-06
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 liuhex 的回复:]
首先你要知道,stack是栈,queue才是队列,这是命名常识。
你的栈初始化没有分配空间,就是说Datatype *elem没有指向。你自己先改改吧。
[/Quote]

朋友,非常感谢你的回答,很受用了,只是小弟还是有些不明白,请具体指教下。谢谢。
sheila_1988 2011-01-06
  • 打赏
  • 举报
回复


void Push(Stack *S,Datatype x) //在队列中插入一个元素
{
if((S->rear+1)%SIZE==S->front)
printf("error");
else
{
S->rear=(S->rear+1)%SIZE;
S->elem[S->rear]=x; // elem没分配空间
}
}


把你的结构体的定义改成数组,如果你不想分配内存的话
liuhex 2011-01-06
  • 打赏
  • 举报
回复
首先你要知道,stack是栈,queue才是队列,这是命名常识。
你的栈初始化没有分配空间,就是说Datatype *elem没有指向。你自己先改改吧。
無_1024 2011-01-06
  • 打赏
  • 举报
回复

#include <stdio.h>
#include<malloc.h>
typedef char ElemType;

typedef struct qnode
{
int data;
struct qnode *next;
}QNode;

typedef struct
{
QNode *front;
QNode *rear;
}LiQueue;

//初始化队列
void InitQueue(LiQueue *&q)
{
q=(LiQueue*)malloc(sizeof(LiQueue));
q->front=q->rear=NULL;
}

//判断是否为空
int QueueEmpty(LiQueue *q)
{
if(q->rear==NULL)
return 1;
else
return 0;
}

//释放
void ClearQueue(LiQueue *q)
{
QNode *p=q->front,*r;
if(p!=NULL)
{
r=p->next;
while(r!=NULL)
{
free(p);
p=r;r=p->next;
}
}
free(q);
}

//实现队列的入队
void enQueue(LiQueue *q,int e)
{
//封装结点
QNode *s;
s=(QNode*)malloc(sizeof(QNode));
s->data =e;
s->next=NULL;
if(q->rear==NULL)
{
q->front =s;
q->rear =s;
}
else
{
q->rear->next =s;
q->rear =s;
}

}

//出队函数
int deQueue(LiQueue *q,ElemType &e)
{
QNode *t;
if(q->rear ==NULL)
return 0;
if(q->front ==q->rear )//只有一个结点
{
t=q->front ;
q->front =NULL;
q->rear =NULL;
}
else
{
t=q->front;
q->front=q->front->next;
}
e=t->data;
free(t);
return 1;
}

//实现队列的测长
int length(LiQueue *q)
{
int n=0;
qnode *p;
p=q->front ;
while(p!=NULL)
{
p=p->next;
n++;
}
return n;
}

int main()
{
ElemType e;
LiQueue *q;
printf("初始化队列q\n");
InitQueue(q);
printf("依次进入队列元素a,b,c\n");
enQueue(q,'a');
enQueue(q,'b');
enQueue(q,'c');
enQueue(q,'d');
printf("队列为%s\n",(QueueEmpty(q)?"空":"非空"));
if(deQueue(q,e)==0)
printf("队空不能出对\n");
else
printf("出对一个元素%c\n",e);
printf("队列的元素个数:%d\n",length(q));
printf("释放队列\n");
ClearQueue(q);
system("pause");
}

//你的做法很诡异

69,369

社区成员

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

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