• 数据结构作业——队列子系统


    #include<iostream>
    using namespace std;
    
    typedef int ElemType;
    
    typedef struct QueueNode{    //结点结构 
        ElemType data;
        QueueNode *next;
    }QNode,*PNode;
    
     
    typedef struct FirstNode{
        QueueNode *rear,*front;  //队列就是一个队头结点后面跟一个链表,
    }*LkQueue;                    // front是头指针,而不是首元素节点 
    
    LkQueue IntiQueue(){
        LkQueue Q;
        Q=new FirstNode;
        if(Q==NULL){
            return Q;   //空间分配失败返回NULL 
        }
        
        PNode N=new QNode;
        if(N==NULL){          
            return NULL;
        }
        
        N->next==NULL;
        Q->rear=N;      //头指针,尾指针指向同一个数据域为空结点 
        Q->front=N;        //这个空结点相当于是链表表头 
        
        return Q;      //返回队指针 
    }
    
    
    bool EnQueue(LkQueue Q,ElemType &x){
        if(Q==NULL){        //队列无效 
            return false;
        }
        
        PNode Cell;
        Cell=new QNode;      //为入队的数据分配空间 
        if(Cell==NULL){            //空间分配失败 
            return false;
        }
        
        Cell->data=x;     
        Cell->next=NULL;      //队尾入队 
        Q->rear->next=Cell;
        Q->rear=Cell;
        
        return true;
    }
    
    
     bool DeQueue(LkQueue Q,ElemType &x){
         if(Q==NULL||Q->front==Q->rear){        //队无效或空队 
             return false;
         }
         PNode temp=Q->front;  //temp是头指针 
         x=temp->next->data;  
         Q->front=temp->next;   //队头出队 
         delete temp;         //释放结点空间 
         
         return true; 
     }
     
     
     bool FrontQueue(LkQueue Q,ElemType &x){
         if(Q==NULL||Q->front==Q->rear){  //队无效或空队 
             return false;
         }
         
         x=Q->front->next->data;  //访问首元素 
         
        return true; 
     }
     
     
     bool PrintQueue(LkQueue Q){
         if(Q==NULL||Q->front==Q->rear){   //队无效或空队 
             return false;
         }
         
         ElemType x;
         PNode index=Q->front;    //index是头指针的副本,改变的是副本,原队列不改变 
         while(index!=Q->rear){     
             x=index->next->data;
             cout<<x<<" ";            
             index=index->next;  //遍历队列 
         }
         
         return true;
     }
     
     //菜单函数 
     void Meue(){    
      
         cout<<"             队列子系统"<<endl;
         cout<<"----------------------------------------"<<endl;
        cout<<"|           1-初始化队列               |"<<endl;
        cout<<"|           2-入队操作                 |"<<endl;
        cout<<"|           3-出队操作                 |"<<endl;
        cout<<"|           4-求队头元素               |"<<endl;
        cout<<"|           5-显示队中所有的元素       |"<<endl;
        cout<<"|           0-返回                     |"<<endl;
        cout<<"----------------------------------------"<<endl;
        cout<<" 请输入菜单号:"; 
     }
     
    int main(){
        
        Meue();
        LkQueue Q;
        ElemType x;  //x为操作元素 
        int m;       //m为菜单号
        cin>>m;
        while(m){
            
            switch(m){
                case 1:Q=IntiQueue();cout<<"完成初始化。"<<endl<<endl;break;
                case 2:cout<<"请输入待插入元素:";
                        cin>>x;
                        EnQueue(Q,x);
                        cout<<"插入完成"<<endl<<endl;
                        break;
                case 3:if(DeQueue(Q,x)){
                            cout<<"队空,无法操作。"<<endl<<endl;
                        }
                        else{
                            cout<<"元素"<<x<<"已出队。"<<endl<<endl;
                            
                        }
                        break;
                case 4:if(!FrontQueue(Q,x)){
                            cout<<"队空,无法操作。"<<endl<<endl;
                        }
                        else{
                        
                        cout<<"队头元素为:"<<x<<endl<<endl;
                        }
                        break;
                case 5:cout<<"队中所有的元素:";
                        PrintQueue(Q);
                        cout<<endl<<endl;break;
                        default:break; 
            }
            
            Meue();
            cin>>m;
        
        } 
        
    
        
        return 0;
    } 
  • 相关阅读:
    Eval与DataBinder.Eval的区别
    ETL增量抽取方式
    SPSS Clementines 预测分析模型啤酒+尿片故事的实现机理(转载)
    【转载】SPSS Clementine 数据挖掘入门3
    SSIS 包配置的过程
    SPSS Clementine 数据挖掘入门2(转载)
    SPSS19.0实战之聚类分析(转载)
    SPSS Clementine 数据挖掘入门1(转载)
    【转】ASP网站源代码修改方法
    【转】【转】一个一年工作经验的java工程师从工作初到今天的所有收藏的学习java的网站(有些很经典
  • 原文地址:https://www.cnblogs.com/WP-WangPin/p/12741947.html
Copyright © 2020-2023  润新知