首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

整了一上午,还是有这么多异常,初学者

2013-02-06 
整了一下午,还是有这么多错误,菜鸟求助本帖最后由 liupanmin123456789 于 2013-01-22 18:17:07 编辑#inclu

整了一下午,还是有这么多错误,菜鸟求助
本帖最后由 liupanmin123456789 于 2013-01-22 18:17:07 编辑 #include<iostream>
#include<string>
using namespace std;
const  int maxsize=10;
template <class T>

class LinkQueue
{
public:
LinkQueue();
~LinkQueue()
{}
/*
~LinkQueue()
{
Node<T> *temp;
while(front!=rear)
{
temp=front->next;
front->next=temp->next;
if(temp->next==NULL)
delete temp;
}
delete front;
}
*/


bool EnQueue( const T &s);
bool DeQueue( T & item);
bool empty()
{
/*if(front!=rear)
return 0;
return 1;
*/
return sizes==0;
}
bool isfull()
{
if(items==maxsize)

return 1;
else 
return 0;

}

private:
class Node
{
public:
T item;
Node  *next;
Node(const T &x):item(x),next(0)
{
}
};
Node *rear;
Node *front;
int items;

};
template<class T>
LinkQueue<T>::LinkQueue()
{
Node  *s=new Node;
s->next=NULL;
front=rear=s;//定义头节点;
   items=0;
}
template <class T>
bool LinkQueue<T>::EnQueue( const T &s)
{
             if(isfull())
          return false;
        //else 
//{


Node *data=new node(s);
s->next=NULL;
rear=next=data;
rear=data;
     items++;
return true;
//}
}


template <class T>
bool LinkQueue<T>::DeQueue(T &item)
{
if(front==rear)
return 0;
//else
//{
Node<T>*p=front->next;
item=p->data;
front->next=p->next;
if(p->next==NULL)
rear=front;
delete p;
         items--;
return 1;
//}
}


void main()
{
LinkQueue<string>Q;

string s;
char c;
while(1)
{
cout<<"哈哈入队啦"<<endl;
while(getline(cin,s))
{
if(Q.EnQueue(s))
{
cout<<"对不起,队满啦"<<endl;
break;
}

}
cout<<"要出队吗,选择吧(y|Y)"<<endl;


cin>>c;
//cin.get(c);
if(c=='y'||c=='Y')
{
while(1)
{
if(Q.DeQueue(s))
{
cout<<"呜呜,队空了,求你别再出队了"<<endl;
break;
}
}
}


else 
break;


}

}




第一次这么用,有很多错误,麻烦长辈帮忙改一下,系谢谢了






















[解决办法]
在长辈的要求下,我改了一下

#include<iostream>


#include<string>
using namespace std;
const  int maxsize=10;
template <class T>

class LinkQueue
{
public:
LinkQueue();
~LinkQueue()
{}


bool EnQueue( const T &item);//入队操作
bool DeQueue( T & item);//出对操作
bool empty()//判断对是否为空
{

return items==0;
}
bool isfull()//判断对是否为满
{

return items==maxsize;
}

private:
class Node
{
public:
T item;
Node  *next;
Node(const T &x):item(x),next(0)
{
}
};
Node *rear;
Node *front;
int items;

};
template<class T>
LinkQueue<T>::LinkQueue()
{
Node  *s=new Node;
s->next=NULL;
front=rear=s;//定义头节点;
   items=0;
}
template <class T>
bool LinkQueue<T>::EnQueue( const T &item)//入队
{
             if(isfull())
          return false;
            Node *data=new node(s);
s->next=NULL;
rear=next=data;
rear=data;
     items++;
return true;

}


template <class T>
bool LinkQueue<T>::DeQueue(T &item)//出队
{
if(front==rear)
return 0;
else
{
Node<T>*p=front->next;
item=p->data;
front->next=p->next;
if(p->next==NULL)
rear=front;
delete p;
         items--;
return 1;
}

}


void main()
{
LinkQueue<string>Q;

string s;

while(!Q.isfull())//队不满,入队
{
{
    cout<<"入队"<<endl;
cout<<"请输入字符串"<<endl;
        getline(cin,s);
     Q.EnQueue(s);
}
cout<<"队列已满"<<endl;
}
while(!Q.empty())//队不空,出队
{
Q.DeQueue(s);

cout<<s<<endl;
}
}























热点排行