内存问题?

elegant87 2008-10-19 01:56:39

//编译和连接能通过,但运行时候提示内存错误!大家帮忙看看吧!
//哪有问题呢?大家提提意见吧!
#include <iostream>
#include <cstdlib>

using namespace std;

typedef struct node
{
int data;
struct node *next;
}Qnode;


class Queue
{
public:
Queue();
void InitQueue();
void show()const;
void EnQueue(int e);
void DeQueue(int &e);
~Queue();
private:
Qnode *rear;
Qnode *front;
};

Queue::Queue()
{
front=new node;
rear=front;
front->next=NULL;
}

void Queue::InitQueue()
{
Qnode *p2=new node;
int i=0;
cout<<"请输入数值(-1 退出): ";
cin>>p2->data;
while (p2->data!=-1)
{
++i;
if(i==1)
{
front->next=p2; //队首指针指向队首
rear->next=p2;
rear=p2;
}
else
{
rear->next=p2;
rear=p2;
}
p2=new node;
cin>>p2->data;
}
rear->next=NULL;
}


void Queue::show() const
{

if(front==rear)
cout<<"此队为空队!"<<endl;
else
{
node *p=front->next;
cout<<"顺序的队值为: "<<endl;
while(p)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
}

void Queue::EnQueue(int e) //入队
{
node *p=new node;
p->data=e;
p->next-=NULL;
rear->next=p;
rear=p;
}

void Queue::DeQueue(int &e) //从队首删除一个元素
{
if (front==rear)
cout<<"此队为空队!"<<endl;
else
{
node *p=front->next;
e=p->data;
front->next=p->next;
if(rear==p)
rear=front;
delete p;
}
}

Queue::~Queue()
{
while(front)
{
rear=front->next;
delete front;
front=rear;
}
}

int main ()
{
Queue Q;
int e;
Q.InitQueue();
cout<<"请输入要进队数值:"<<endl;
cin>>e;
Q.EnQueue(e);
Q.DeQueue(e);
cout<<"出队数值: "<<e<<endl;
Q.show();
cout<<endl;
return 0;
}

...全文
96 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
tianyuanfeige 2008-10-19
  • 打赏
  • 举报
回复
ding
hurryboylqs 2008-10-19
  • 打赏
  • 举报
回复
呵呵,写代码要注意点
星羽 2008-10-19
  • 打赏
  • 举报
回复
如1楼所说
c19122273 2008-10-19
  • 打赏
  • 举报
回复

void Queue::InitQueue()
{
Qnode *p2=new node;
int i=0;
cout<<"请输入数值(-1 退出): ";
cin>>p2->data;
while (p2->data!=-1)
{
++i;
if(i==1)
{
front->next=p2; //队首指针指向队首
rear->next=p2;
rear=p2;
}
else
{
rear->next=p2;
rear=p2;
}
p2=new node;
cin>>p2->data;
}
rear->next=NULL;
}


你这个函数里面的 i在哪有输入啊
yinyuexiuluo 2008-10-19
  • 打赏
  • 举报
回复
void Queue::InitQueue()
{
Qnode *p2=new node;
int i=0;
cout<<"请输入数值(-1 退出): ";
cin>>p2->data;
while (p2->data!=-1)
{
++i;
if(i==1)
{
front->next=p2; //队首指针指向队首
rear->next=p2;
rear=p2;
}
else
{
rear->next=p2;
rear=p2;
}
p2=new node;
cin>>p2->data;
}
rear->next=NULL;
}
  • 打赏
  • 举报
回复
void Queue::EnQueue(int e) //入队
{
node *p=new node;
p->data=e;
p->next-=NULL;<===多了个减号吧
rear->next=p;
rear=p;
}


void Queue::EnQueue(int e) //入队
{
node *p=new node;
p->data=e;
p->next=NULL;
rear->next=p;
rear=p;
}

64,652

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧