bejiz
2010-01-07 16:36:50 UTC
Hello,
I try to delete the elements of a linked list with the fonction clear
but according to the result only the first element gets deleted, after
there are only 0's. Perhaps do you know what to do about it.
Thanks.
#include <iostream>
using namespace std;
class linked_list
{
public:
linked_list(): p_begin(0), p_end(0) {};
~linked_list(){};
void clear( linked_list A)
{
while(p_begin!=0)
{
Node* p_zap = new Node(0,0);
p_zap = A.p_begin;
cout << " delete : " << p_zap->data << endl;
p_begin = p_begin->p_next;
delete p_zap;
}
}
void push_back( const int& a)
{
if(!p_begin)
{
Node* temp = new Node(a,0);
p_begin = p_end = temp;
}
else
{
Node* temp = new Node(a,0);
p_end->p_next = temp;
p_end = temp;
}
}
friend
ostream& operator<<( ostream& xout, const linked_list& A )
{
Node* a = A.p_begin;
if (a==0){
xout << " empty " << endl;
return xout;
}
else
xout << a->data << endl;
while((a->p_next)&&a)
{
a = a->p_next;
xout << a->data << endl;
}
return xout;
}
private:
struct Node
{
int data;
Node* p_next;
Node(int val, Node* p = 0): data(val), p_next(p) {};
}*p_begin, *p_end;
};
int main()
{
linked_list A;
A.push_back(23);
A.push_back(233);
A.push_back(73);
cout << A ;
A.clear(A);
cout << A;
return 0;
}
I try to delete the elements of a linked list with the fonction clear
but according to the result only the first element gets deleted, after
there are only 0's. Perhaps do you know what to do about it.
Thanks.
#include <iostream>
using namespace std;
class linked_list
{
public:
linked_list(): p_begin(0), p_end(0) {};
~linked_list(){};
void clear( linked_list A)
{
while(p_begin!=0)
{
Node* p_zap = new Node(0,0);
p_zap = A.p_begin;
cout << " delete : " << p_zap->data << endl;
p_begin = p_begin->p_next;
delete p_zap;
}
}
void push_back( const int& a)
{
if(!p_begin)
{
Node* temp = new Node(a,0);
p_begin = p_end = temp;
}
else
{
Node* temp = new Node(a,0);
p_end->p_next = temp;
p_end = temp;
}
}
friend
ostream& operator<<( ostream& xout, const linked_list& A )
{
Node* a = A.p_begin;
if (a==0){
xout << " empty " << endl;
return xout;
}
else
xout << a->data << endl;
while((a->p_next)&&a)
{
a = a->p_next;
xout << a->data << endl;
}
return xout;
}
private:
struct Node
{
int data;
Node* p_next;
Node(int val, Node* p = 0): data(val), p_next(p) {};
}*p_begin, *p_end;
};
int main()
{
linked_list A;
A.push_back(23);
A.push_back(233);
A.push_back(73);
cout << A ;
A.clear(A);
cout << A;
return 0;
}