关于C++编写的队列程序,不知道怎么改?
最近我看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()根本没执行借结束了。我也没想明白!
我是不是在指针分配的时候出什么问题了!
希望高手们能帮我指出来,
万分感谢!
这几天一直很郁闷。简单程序都没法实现。真不知道怎么学编程。
#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()根本没执行借结束了。我也没想明白!
我是不是在指针分配的时候出什么问题了!
希望高手们能帮我指出来,
万分感谢!
这几天一直很郁闷。简单程序都没法实现。真不知道怎么学编程。
...全文
相关推荐
一个用C++编写的画图程序
本文将深入探讨一个使用C++在Visual C++平台上编写的简单画图程序,以帮助初学者理解C++与图形用户界面(GUI)编程的结合。1.
C++队列、双向队列、单调队列
队列(Queue)是一种基本的线性数据结构,它遵循先进先出(First In First Out, FIFO)的原则。这意味着最先被添加到队列中的元素将会是最先被移除的。和生活中,无人插队的队列一样。队列内部实现可能是连续空间,也可能是多段空间。
C++ 跨平台 异步消息队列
本项目名为"C++ 跨平台 异步消息队列",显然它提供了一个用C++编写的跨平台解决方案,用于在不同线程间安全地传递和处理消息。下面我们将深入探讨这个项目的相关知识点。
C++实现队列源代码
队列的头部和尾部都是链表的头节点。`main.cpp`是主程序,用于测试`MyQueue`类的功能。
双端队列C++实现 双端队列C++实现
在C++标准库中,`std::deque`是内置的双端队列容器,提供了高效且灵活的内存管理。然而,如果你想要自定义一个双端队列,我们可以从头开始探讨如何实现。


