是小狐狸啊 2016-10-12 12:36 采纳率: 63.6%
浏览 1575

数据结构基础问题 这个链队列为什么不能入队和出队?

程序可以运行,界面如下;反复检查代码似乎都没有问题,为什么出不了结果呢。。恳求大神指教!!

图片说明

    //链队列
    #include "stdAfx.h"
    #include <iostream>
    using namespace std;

    typedef int ElemType;
    extern void Error( char * s );
    enum Status{
        ERROR,
        OVERFLOW1,
        OK
    };

    //链队列的类型定义
    typedef struct QNode    /* 结点结构 */ 
    { 
            ElemType data; 
            struct QNode *next; 
    } QNode,*QueuePtr; 
    typedef struct          /* 队列的链表结构 */ 
    { 
            QueuePtr front,rear; /* 队头、队尾指针 */ 
    } LinkQueue; 
    ////////////////////////////////算法实现//////////////////////////////////
    //1.初始化队列
    Status InitQueue_L( LinkQueue &Q )
    {
        if( Q.front != NULL)
            return OVERFLOW1;
        Q.front = Q.rear ;  
        Q.front->next = NULL;
        return OK;
    }
    //2. 销毁队列
    Status DestroyQueue_L( LinkQueue &Q )
    {

            if(Q.front==Q.rear)  
            return ERROR;
        delete Q.front ;
            Q.front =Q.rear =NULL;
        return OK;

    }
    //3. 入队:把x插入队尾
    Status EnQueue_L( LinkQueue &Q , ElemType x )
    {
        QNode*p = new QNode; 
        if(p!=NULL)
            return OVERFLOW1;
        p->data = x;
        p->next =NULL;
        Q.rear->next = p;
        Q.rear = p;
        return OK;
    }
    //4. 从出队: 如果队空,返回false;否则返回队头元素到x
    Status DeQueue_L( LinkQueue &Q , ElemType &x )
    {
        QNode*p = new QNode;
            if(Q.front==Q.rear)  
            return ERROR;
            p=Q.front->next;    
            Q.front->next=p->next;  
            if(Q.front->next==NULL)  
            {  
                    Q.rear=Q.front;  
            }  
        x = p->data;
            delete p;  
            return OK;  

    }

    int main(int argc, char* argv[])
    {
        //测试链队列
        LinkQueue queue;
        cout<<"测试链队列"<<endl;
        InitQueue_L( queue );//初始化队列
        for ( int i = 0 ; i < 100 ; i ++ )
        {
            EnQueue_L( queue , i );//入入队的顺序为0,1,2,3,...,99(自行添加出错处理)
        }
        cout<<"队列的测试"<<endl;
        while ( DeQueue_L( queue , i ) == OK )//出队,输出出队的元素
        {
            cout<< i <<" "; 
        }
        cout<<endl;
        DestroyQueue_L( queue );//销毁队列
        cout<<"退出main函数"<<endl;
        system("pause");
        return 0;
    }
  • 写回答

1条回答 默认 最新

  • devmiao 2016-10-12 16:03
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler