| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 537 人关注过本帖
标题:这个问题怎么解决
取消只看楼主 加入收藏
淡雅蜻蜓
Rank: 1
等 级:新手上路
帖 子:23
专家分:8
注 册:2010-11-21
结帖率:25%
收藏
已结贴  问题点数:0 回复次数:0 
这个问题怎么解决


const int maxSize=50;
template <class E>
class Queue {
public:
     Queue() { };          //构造函数
     ~Queue() { };          //析构函数
     virtual bool EnQueue(E x) = 0;           //进队列
     virtual bool DeQueue(E& x) = 0;          //出队列
     virtual bool getFront(E& x) = 0;          //取队头  
     virtual bool IsEmpty() const = 0;          //判队列空
     virtual bool IsFull() const = 0;          //判队列满
    // void MakeEmpty(){front=rear=0;}
};

#include <iostream.h>
#include "Queue.h"
template <class E>
class LinkedQueue:public Queue<E> {   
public:
    LinkedQueue() : rear(NULL), front(NULL) { }
    ~LinkedQueue(){delete[] elements;}
    bool EnQueue(const E& x);
    bool DeQueue(E& x);            
    bool GetFront(E& x) const;            
    void makeEmpty(){front=rear=0;}            //实现与~Queue()同
    bool IsEmpty() const { return (front == NULL)?true:false; }
    int getSize()const;
    friend ostream& operator <<(ostream& os,LinkedQueue<E>& Q);{
    protected:     QueueNode <E> *front, *rear;
};


template <class E>
LinkedQueue<E>::~LinkedQueue() {      
    QueueNode<E> *p;
    while (front != NULL) {              
        p = front;  front = front->link;  delete p;
    }
};//逐个删除队列结点


template <class E>
bool LinkedQueue<E>::EnQueue(const E& x) {
       if (front == NULL)  {              
          front = rear = new QueueNode<E> (x);
          if (front == NULL) return false; }     
     else {                                      
          rear->link = new QueueNode<E> (x);
          if (rear->link == NULL) return false;
          rear = rear->link;
     }
     return true;
};//链式队列的入队操作


template <class E>
bool LinkedQueue<E>::DeQueue(E& x) {
     if (IsEmpty() == true) return false;        
     QueueNode<E> *p = front;        
     x = front->data;  front = front->link;   
     delete p;   return true;               
};//链式队列的出队操作


template <class E>
bool LinkedQueue<E>::GetFront(E& x)const {
    if (IsEmpty() == true) return false;     
    x = front->data;  return true;
};//链式队列取队头元素操作


template<class E>
int LinkedQueue<E>::getSize()const {
QueueNode<E>  *p=front;int k=0;
while (p!=NULL){p=p->link;k++;}
return k;
};


template<class E>
friend ostream& operator <<(ostream& os,LinkedQueue<E>& Q){
    os<<"队列中元素个数有"<<Q.getSize()<<endl;
    QueueNode<E>  *p=Q.front;int i=0;
    while (p!=NULL){
        os<<++i<<","<<p->data<<endl;}
    return os;
};//元素的重载





void main()
{   
    int maxSize,number,i,select=5;
    char s;
    cout<<"\n请输入对的大小:"<<endl;
    cin>>maxSize;  
    LinckedQueue<char> LinkedQueue(maxSize);
    while(select!=4)
     {
        cout<<"\n\t\t\t************操作菜单*****************\n";
        cout<<"\t\t\t\t 1 入队"<<endl;
        cout<<"\t\t\t\t 2 出队"<<endl;
        cout<<"\t\t\t\t 3 取队头元素"<<endl;
        cout<<"\t\t\t\t 4 退出"<<endl;
        cout<<"\t\t\t************请选择序号*****************\n";
        cin>>select;
        switch(select)
        {
         case 1:
            cout<<"请输入一个队列并以.结束"<<endl;
            cin>>s;
            while(s!='.')
            {
              LinkedQueue.EnQueue(s);
              cin>>s;

             }
            break;
         case 2:
             cout<<"请输入出队字符数:";
             cin>>number;
             cout<<"\n出队元素依次为:";
             for(i=0;i<number;i++)
               if (LinkedQueue.DeQueue(s)) cout<<s<<"  ";
             break;
         case 3:
             if (!LinkedQueue.GetFront(s)){cout<<"队空"<<endl;break;}
             cout<<"队头元素为:"<<s;
             break;
         case 4:
             break;
        }
     }
       cout<<"谢谢使用"<<endl;
}
搜索更多相关主题的帖子: include public 
2010-12-10 14:28
快速回复:这个问题怎么解决
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.031755 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved