自各儿编写链式队列类的实现以后,在测试程序运行时会停止工作
自己编写链式队列类的实现以后,在测试程序运行时会停止工作
自己编写的.h,.cpp和测试程序
//linkQueue.h
#ifndef LINKQUEUE_H_INCLUDED
#define LINKQUEUE_H_INCLUDED
#define NULL 0
template<class elemType>
class linkQueue
{
private:
struct node {
elemType data;
node *next;
node(const elemType &x, node *N = NULL){data = x; next = N;}
node():next(NULL) {}
~node() {}
};
node* front,*rear;
public:
linkQueue() { front = rear = NULL; }
~linkQueue();
bool isEmpty() {return front == NULL;}
void enQueue(const elemType &x) ;
elemType deQueue() ;
elemType getHead() {return front->data;}
};
#endif // LINKQUEUE_H_INCLUDED
//linkQueue.cpp
#include "linkQueue.h"
template <class elemType>
linkQueue<elemType>::~linkQueue()
{
node* p;
while (front)
{
p=front;
front=front->next;
delete p;
}
}
template <class elemType>
void linkQueue<elemType>::enQueue(const elemType& x)
{
if (rear==NULL)front=rear=new node(x);
else rear=new node(x,rear->next);
}
template <class elemType>
elemType linkQueue<elemType>::deQueue()
{
node* p=front;
elemType x=front->data;
front=front->next;
if (front==NULL)rear=NULL;
delete p;
return x;
}
//test_linkQueue.cpp
#include "linkQueue.h"
#include "linkQueue.cpp"
#include <iostream>
using namespace std;
int main()
{
linkQueue<int> s;
cout<<s.isEmpty()<<endl;
s.enQueue(10);
s.enQueue(20);
s.enQueue(1);
s.enQueue(1);
cout<<s.getHead()<<endl;
cout<<s.deQueue()<<endl;
cout<<s.getHead()<<endl;
}
各位大神们,就是在运行这个test_linkQueue.cpp时程序运行到一半会停止工作,这是为什么啊
------解决思路----------------------
怎么感觉起来不伦不类的感觉,你要用类就全部用类,你搞个结构体中里面写些类该做的事情(虽然没错,但感觉……),一般是要么全部是类,要么类(成员+函数) + 结构体(成员)……
这个就有问题了,你这个node(x)和node(x, rear->next)函数调用的是
------解决思路----------------------
试改如下:
自己编写的.h,.cpp和测试程序
//linkQueue.h
#ifndef LINKQUEUE_H_INCLUDED
#define LINKQUEUE_H_INCLUDED
#define NULL 0
template<class elemType>
class linkQueue
{
private:
struct node {
elemType data;
node *next;
node(const elemType &x, node *N = NULL){data = x; next = N;}
node():next(NULL) {}
~node() {}
};
node* front,*rear;
public:
linkQueue() { front = rear = NULL; }
~linkQueue();
bool isEmpty() {return front == NULL;}
void enQueue(const elemType &x) ;
elemType deQueue() ;
elemType getHead() {return front->data;}
};
#endif // LINKQUEUE_H_INCLUDED
//linkQueue.cpp
#include "linkQueue.h"
template <class elemType>
linkQueue<elemType>::~linkQueue()
{
node* p;
while (front)
{
p=front;
front=front->next;
delete p;
}
}
template <class elemType>
void linkQueue<elemType>::enQueue(const elemType& x)
{
if (rear==NULL)front=rear=new node(x);
else rear=new node(x,rear->next);
}
template <class elemType>
elemType linkQueue<elemType>::deQueue()
{
node* p=front;
elemType x=front->data;
front=front->next;
if (front==NULL)rear=NULL;
delete p;
return x;
}
//test_linkQueue.cpp
#include "linkQueue.h"
#include "linkQueue.cpp"
#include <iostream>
using namespace std;
int main()
{
linkQueue<int> s;
cout<<s.isEmpty()<<endl;
s.enQueue(10);
s.enQueue(20);
s.enQueue(1);
s.enQueue(1);
cout<<s.getHead()<<endl;
cout<<s.deQueue()<<endl;
cout<<s.getHead()<<endl;
}
各位大神们,就是在运行这个test_linkQueue.cpp时程序运行到一半会停止工作,这是为什么啊
------解决思路----------------------
void linkQueue<elemType>::enQueue(const elemType& x)
{
if (rear==NULL)
front=rear=new node(x);
else
rear=new node(x,rear->next);
}
这个就有问题了,你这个node(x)和node(x, rear->next)函数调用的是
node(const elemType &x, node *N = NULL){data = x; next = N;}rear->next永远也是NULL……你根本就没有为node* next分配空间……
------解决思路----------------------
试改如下:
#define NULL 0
template<class elemType>
class linkQueue
{
private:
struct node {
elemType data;
node *next;
node(const elemType &x, node *N=NULL){//修改如下
data = x;
next = NULL;
if (N) N->next = this;
}
node():next(NULL) {}
~node() {}
};
node* front,*rear;
public:
linkQueue() { front = rear = NULL; }
~linkQueue();
bool isEmpty() {return front == NULL;}
void enQueue(const elemType &x) ;
elemType deQueue() ;
elemType getHead() {return front->data;}
};
template <class elemType>
linkQueue<elemType>::~linkQueue()
{
node* p;
while (front)
{
p=front;
front=front->next;
delete p;
}
}
template <class elemType>
void linkQueue<elemType>::enQueue(const elemType& x)
{
if (rear==NULL)front=rear=new node(x);
else rear=new node(x,rear);//修改 else rear=new node(x,rear->next);
}
template <class elemType>
elemType linkQueue<elemType>::deQueue()
{
node* p=front;
elemType x=front->data;
front=front->next;
if (front==NULL)rear=NULL;
delete p;
return x;
}
#include <iostream>
using namespace std;
int main()
{
linkQueue<int> s;
cout<<s.isEmpty()<<endl;
s.enQueue(10);
s.enQueue(20);
s.enQueue(1);
s.enQueue(1);
cout<<s.getHead()<<endl;
cout<<s.deQueue()<<endl;
cout<<s.getHead()<<endl;
}