help?

sruct node {
int val;
node* next;
node() {val = 0; next = 0;}
node(int v, node* n = 0) {
val = v;
next = n; }
~node(){ next = 0; }
void Add (int);
void Print() const { cout << val << endl; }
};

struct list {
node* first;
void Copy(list& l);
void Delete();
list() { first = 0; }
list(list&);
~list();
list& operator=(list&);
node* Last() const;
void AddFirst(int);
void AddLast(int);
void Add(node*, int);
void Print() const;

};
void list::Copy (list& l) {
node* p = new node(l.first->val);
first = p;
for (node*q=p->next; p; p=p->next)
Last()->Add(q->val);

}
this is the hard part for me.. i'm learning alone, only help is you and the books. i understand the notion of pointers and references, but i'm still kinda newbie in using them so deep,, so this just messes me up. can someone enlighten me mot-a-mot if possible ? Thank you.

this came afterwards..
first = pointer to objects of class node*
l.first = member first of object pointed by l ?
l.first->val = member val of object pointed by member first of object l ?
don't really make any sense
Last edited on
closed account (o3hC5Di1)
Hi there,

node* p = new node(l.first->val);

Create a variable p, of the type "pointer to a node-struct" and create a new node-object, setting it's value to.
In other words, p now contains the memory address of a new Node.

first = p;

Set the member variable "first" of the List-object to the same memory address as the one in p.
This is done so that you will always have the latest inserted element of the list as the "first" element accessible.

for (node*q=p->next; p; p=p->next)

While there are more values in the list that is being copied:

Last()->Add(q->val);

Add that item to the copy of the list.


Hope that helps.

All the best,
NwN
btw, next time, use "code" tags because it looks confusing... even now, i haven't fully read you code...
Topic archived. No new replies allowed.