关于C++编写的队列程序,不知道怎么改?

CSU305 CSU305 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
最近我看C++编程时,就最近动手想编个队列程序 其代码如下:
#include <iostream>
using namespace std;
#define DEFAULT -1
class qtype {
friend class Cqueue;
public:
qtype(int elem);
qtype ( );
private:
int data;
qtype * next;
};
inline qtype::qtype( int elem):data(elem),next(0) { }
inline qtype::qtype( ):data(DEFAULT),next(0){ }

class Cqueue {
public:
Cqueue ();
int queuelen ( ) const ;
void insert ( int elem,qtype *p= 0) ;

void display ( ) ;
void insert_front ( int elem);
qtype *head, *tail;
private:
int qlen_;
// qtype *head, *tail;
Cqueue ( qtype * q) ;
Cqueue& operator= (const Cqueue&);


};
inline Cqueue::Cqueue():qlen_(0),head(0),tail(0) {}

int Cqueue::queuelen() const {
return qlen_;
}
void Cqueue::insert(int elem,qtype* p ) {
qtype *temp = new qtype (elem);
if( p != 0){
temp->next =p->next ;
p->next = temp;
qlen_ ++;
}
else {
atfront( elem);
}


}
void Cqueue::atfront ( int elem) {
qtype * qtemp = new qtype (elem);

if (!head) {
head = tail = qtemp;
}
else {
qtemp->next = head;
head = qtemp;
}
qlen_++;
}
void Cqueue::display( ) {
//qtype *que = new qtype();
qtype *que = head;
cout << "the elem of the queue is:\n" << endl;
while ( !que) {
cout << que->data << " " <<endl;
que = que->next ;
}
}

void main () {
Cqueue myque;
for (int i = 0; i< 10; i++) {
myque.insert ( i ,myque.head);
//为了测试列表头的元素/是否在变
cout << "the value of head is: \n" << myque.head->data << endl;

}
myque.display ();
}

我往列表中插入10个元素,按照我的程序,应该是每次在队列头插入新的表项,但是在测试时输出的元素总是“0”,我检查了自己的程序实在是找不来。
还有这个的 dispaly()根本没执行借结束了。我也没想明白!
我是不是在指针分配的时候出什么问题了!
希望高手们能帮我指出来,
万分感谢!
这几天一直很郁闷。简单程序都没法实现。真不知道怎么学编程。
...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
CSU305
C++ 语言 65,214 成员 250,514 内容