返回信息流#include<iostream>
using namespace std;
struct node
{
node *next;
char *contents;
};
class list
{
protected:
node *head;
int size;
public:
list(){};
void insert (char*a);
char *get();
void clear();
~ list () { clear(); }
};
void list::insert(char* a)
{ node *p=new node;
p->contents=a;
if(head==0)
{head=p;
head->next=0;}
else
{p->next=head;
head=p;}
}
char* list::get()
{ char* p;
node *q;
if(head==0)
return 0;
else
{p=head->contents;
q=head;
head=head->next;
delete q;
return p;}
}
void list::clear()
{
while (head!=0)
{node*p=head;
head=head->next;
delete p;}
}
class int_stack: public list
{
public:
void push( char*a){insert(a);}
char* pop()
{char*a;
a=get();
return a;}
int_stack(){};
~int_stack(){};
};
void main()
{
int_stack sta;
sta.push("asa");
sta.push("hdhahd");
cout<<sta.pop()<<endl;
cout<<sta.pop()<<endl;
return;
}
附件(2.8MB)
这是一条镜像帖。来源:北邮人论坛 / cpp / #16080同步于 2008/11/16
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
为什么指针老错呢??
yjs826397
2008/11/16镜像同步1 回复
订阅后,新回复会通过你的通知中心匿名送达。
1 条回复
没有在list::list()中把list::head初始化为NULL
在list::insert()中就会出问题
if(head==0)//不可能进入这个分支
{
....
}
else
{
....
}
这种问题加个断点走一遍不就查出来了么...