队列无法连续出队,出队一次就初始化了队列

何止。 2017-12-14 03:56:44
求助:队列无法连续出队,出队一次就初始化了队列

#include<iostream>
#include<stdio.h>
#include <stdlib.h>
using namespace std;
#define FLASE 0
#define TRUE 1
typedef int QueueElemType;
typedef struct Node
{
QueueElemType data;
struct Node *next;
}LinkQueueNode;
typedef struct
{
LinkQueueNode *front;
LinkQueueNode *rear;
}LinkQueue;
int initQueue(LinkQueue *Q)
{
Q->front=(LinkQueueNode *)malloc(sizeof(LinkQueueNode));
if(Q->front!=NULL)
{
Q->rear=Q->front;
Q->front->next=NULL;
return(TRUE);
}else return(FLASE);
}
int EnterQueue(LinkQueue *Q,QueueElemType x)
{
LinkQueueNode *NewNode;
NewNode=(LinkQueueNode*)malloc(sizeof(LinkQueueNode));
if(NewNode!=NULL)
{
NewNode->data=x;
NewNode->next=NULL;
Q->rear->next=NewNode;
Q->rear=NewNode;
return(TRUE);
}
else return(FLASE);
}
int DeleteQueue(LinkQueue *Q,QueueElemType *x)
{
LinkQueueNode *p;
if(Q->front==Q->rear)
return(FLASE);
p=Q->front->next;
Q->front->next=p->next;
if(Q->rear==p);
Q->rear=Q->front;
*x=p->data;
free(p);
return(TRUE);
}
int print(LinkQueue *Q)
{
if(!Q)
return (FLASE);
Node *pre;
pre=Q->front->next;
while(pre) {
cout<<pre->data<<' ';
pre=pre->next;
}
return (TRUE);
}
int main()
{
LinkQueue Q;
QueueElemType x;
int i,j,ch;
initQueue(&Q) ;
while(1)
{
printf("\n队列操作:(1.入队 2.出队, 3.exit)\n");
scanf("%d", &ch);
switch(ch)
{
case(1):{
printf("输入要入队元素:");
scanf("%d",&x);
j=EnterQueue(&Q,x);
if(j){printf("入队后队列元素为");
print(&Q); }
else{printf("入队失败");
}
break;
}
case(2):{
printf("出队");
j=DeleteQueue(&Q,&x);
if(j){printf("出队后队列元素为");
print(&Q); }
else{printf("出队失败");
}
break;
}
case(3):
return 0;
}}
return 0;
}
...全文
152 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2017-12-14
  • 打赏
  • 举报
回复
关于自己是否适合编程的很简单的测试: 在报纸或杂志上随便找一段约1000字的文章,在Word中输入一遍。输完后再参考下面答案: A里面有10处以上文字或标点错误 B里面没有文字或标点错误并敢为此跟人打赌 C里面没有文字或标点错误并且字体和排版完全与原稿一致 D打印在半透明的纸上和原稿重叠在一起检查一模一样,且自我感觉很有成就感 A不适合编程(理由:打字准确度偏低、粗心大意) B初级程序员(理由:打字准确度很高、认真细致、自信、理解全角半角概念) C高级程序员(理由:在B的基础上理解字体和排版也是电脑打印的重要因素、但相比D还不够偏执、精益求精、结果可验证) D软件项目经理(理由:能针对项目给出令人信服的细致到极点的需求说明和典型测试用例。用户几乎挑不出毛病。专业!) 如果想从A变成B的话,到我的资源http://download.csdn.net/detail/zhao4zhong1/4084259里面下载“适合程序员的键盘练习”
自信男孩 2017-12-14
  • 打赏
  • 举报
回复
另外,false的拼写不是flase
自信男孩 2017-12-14
  • 打赏
  • 举报
回复
#include<iostream>
#include<stdio.h>
#include <stdlib.h>

using namespace std;

#define FLASE 0
#define TRUE 1

typedef int QueueElemType;
typedef struct Node
{
    QueueElemType data;
    struct Node *next;
}LinkQueueNode;

typedef struct
{
    LinkQueueNode *front;
    LinkQueueNode *rear;
}LinkQueue;

int initQueue(LinkQueue *Q)
{
    Q->front=(LinkQueueNode *)malloc(sizeof(LinkQueueNode));
    if (Q->front == NULL)
        return FLASE;

    Q->rear=Q->front;
    Q->front->next=NULL;

    return(TRUE);
}

int EnterQueue(LinkQueue *Q,QueueElemType x)
{
    LinkQueueNode *NewNode;
    NewNode=(LinkQueueNode*)malloc(sizeof(LinkQueueNode));
    if(NewNode == NULL)
        return(FLASE);
    NewNode->data=x;
    NewNode->next=NULL;
    Q->rear->next = NewNode;
    Q->rear=NewNode;
    return(TRUE);
}
int DeleteQueue(LinkQueue *Q,QueueElemType *x)
{
    LinkQueueNode *p;
    if(Q->front==Q->rear)
        return(FLASE);
    p = Q->front->next;
    Q->front->next = p->next;
    /*
    if(Q->rear==p);
        Q->rear=Q->front;
        */
    *x = p->data;
    free(p);

    return(TRUE);
}
int print(LinkQueue *Q)
{
    if(!Q)
        return (FLASE);
    Node *pre;
    pre = Q->front->next;
    while(pre) {
        cout<<pre->data<<' ';
        pre = pre->next;
    }
    return (TRUE);
}
int main()
{
    LinkQueue Q;
    QueueElemType x;
    int i,j,ch;
    initQueue(&Q) ;
    while(1)
    {
        printf("\n队列操作:(1.入队 2.出队, 3.exit)\n");
        scanf("%d", &ch);
        switch(ch)
        {
            case(1):{
                        printf("输入要入队元素:");
                        scanf("%d",&x);
                        j=EnterQueue(&Q,x);
                        if(j){
                            printf("入队后队列元素为");
                            print(&Q);
                        }
                        else{printf("入队失败");
                        }
                        break;
                    }
            case(2):{
                        printf("出队");
                        j=DeleteQueue(&Q,&x);
                        if(j){
                            printf("出队后队列元素为");
                            print(&Q);
                        }
                        else{
                            printf("出队失败");
                        }
                        break;
                    }
            case(3):
                    return 0;
        }}
    return 0;
}
参考一下吧 出队时,去掉2条语句,如下:
int DeleteQueue(LinkQueue *Q,QueueElemType *x)
{
    LinkQueueNode *p;
    if(Q->front==Q->rear)
        return(FLASE);
    p = Q->front->next;
    Q->front->next = p->next;
    /*
    if(Q->rear==p);
        Q->rear=Q->front;
        */
    *x = p->data;
    free(p);

    return(TRUE);
}
去掉就没问题了。

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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