![]() |
#2
m21wo2010-09-25 20:03
你写的太乱了!你的命名也很乱,pop,push等词也通常用于堆栈!!
这是我帮你写的一个构造单链表的程序: ![]() #include <iostream> using namespace std; class node { private: int element; node* link; public: node(int el=0,node* lk=0):element(el),link(lk) {} node* Creat(); void output(node* p); node* del(node* head,int num); }; node* node::Creat() { node *first=new node; node *p; node* s=new node; int x; int n=0; while(cin>>x) { p=new node; p->element=x; if(n==0) { first=new node(x,0); s=first; n=1; } else { s->link=p; s=s->link; } } s->link=NULL; return first; } void node::output(node* p) { while(p!=NULL) { cout<<p->element<<endl; p=p->link; } } node* node::del(node* head,int num) { node* p=head; node* s=new node; while((p->element != num)&&(p->link != NULL)) { s=p; p=p->link; } if(p->element==num) { if(p==head) { head=p->link; delete p; } else { s->link=p->link; delete p; } } else cout<<"there is no element in the link !"<<endl; return head; } int main() { node l; node* e,*s1; e=l.Creat(); l.output(e); if(cin.fail()) { cin.clear(); cin.ignore(); } cout<<"please input a elememt that you want to delete :"; int x1; cin>>x1; s1=l.del(e,x1); l.output(s1); return 0; } |
最近刚刚自己学了C++,于是就想练练手,写了一个链表的类,编译的时候都还没问题,但是运行的时候总出错。我用的是visual studio2008,代码如下:
#include<iostream>
#include<windows.h>
#include<stdlib.h>
using namespace std;
struct Node{struct Node *next;int data;};
class A
{
public:
A();
~A();
int push(int e);
int pop();
//bool erease();
struct Node * begin();
struct Node * end();
private:
struct Node * V;
};
A::A()
{
struct Node *H=new struct Node;
H->next=NULL;
(this->V)->next=H;
}
A::~A()
{
delete [](this->V);
}
int A::push(int e)
{
struct Node *B=new struct Node;
struct Node *p;
p=end();
B->data= e;
B->next= NULL;
p->next=B;
return 0;
}
/*int A::pop()
{
struct Node *p,*q;
p=end();
q=begin();
while(q->next != p)
q=q->next;
q->next=NULL;
return p->data;
delete p;
}
/*bool A::erease(struct Node *p)
{
struct Node *q;
while(q->next != p)
q=q->next;
q->next=p->next;
delete p->next;
return true;
}*/
struct Node *A::begin()
{
return V->next;
}
struct Node *A::end()
{
struct Node* p;
p=begin();
while(p->next != NULL)
p=p->next;
return p;
}
int main()
{
int e,f=0;
A shiyan;
cin>>e;
shiyan.push(e);
//f=shiyan.pop();
//cout<<f<<endl;
system("pause");
return 0;
}
在运行的时候,出现了错误提示:实验.exe 中的 0x00e71e2d 处未处理的异常: 0xC0000005: 写入位置 0xcccccccc 时发生访问冲突。
这个程序我看了几天了,还是找不出问题。这个到底是什么情况?望高手解答,谢谢了。