did i get this straight?

I'm goin to write after // what the fuction does in my opionion..please correct me and my apologies for bad english.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
struct 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 node::Add(int k) {
node* p = new node(k);   //  creates a new object and set next to point 
next = p;               //   to every new object with adress of k created
};

void list::Copy (list& l) {
node* p = new node(l.first->val);   //  creates a new object with adress 
first = p;                          //  of val, and put first to point to
for (node*q=p->next; p; p=p->next)  //  for evry new obj of the list and 
Last()->Add(q->val);               //  val, assign to p new object with 
}                                // adress of k and add a val ?

void list::Add(node* n, int k) {  
if (n) {                     // set p to point to adress of k, assign the
node *p = new node(k);      //new object to point to the current one,then 
p->next = n->next;         // assign current one to point to new object? 
n->next = p;
}
}

void list::AddFirst(int k) {  //create an object with adress of k  and
node *p = new node(k);       //make this object point to the beginning of 
p->next = first;           // of the list, then make the beggining of the
first = p;                // list to point to every new object created
}

node* list::Last() const {        //for every p = beggining, next element[b/] 
node* p;                         //[b] p=next element, return last element
for(p=first; p->next; p=p->next);
return p;
}

void list::AddLast(int k) {
if (first)                //if beggining of the list call Add to create 
Last()->Add(k);         // new object with adress of k, else , create        
else {                 // new object adress k and set beggining of the 
node *p = new node(k); // to point to the new object
first = p;
}
}

void list::Print() const {          //for every element on the list print
for (node* p=first; p; p=p->next)  //the val of all
p->Print();
}

void list::Delete() {  //  free memory allocated with new,  
}

list::list(list& l) { //  copy constructor ? 
Copy (l);
}

list::~list() {   // destructor for list delete memory till first =0 ? 
Delete();           
first = 0;
}

list& list::operator=(list& l) {
if(&l != this) {                //  if there is created any object but l 
Delete();                      //delete and call fuction copy that copies
Copy(l);                      // object l?
}
return *this;                // return l ? 
}


This is a mess, i can feel it. Help me out here, tell me what I'm not seeing ? Can wait to hear your take.
Thank you
closed account (o3hC5Di1)
Hi there,

Just a few comments, haven't studied your code thoroughly:

1
2
void list::Delete() {  //  free memory allocated with new,  
}


This isn't doing anything at all.
You'll need to do delete on the dynamically allocated memory.
Note that when you just delete the list pointers, the actual data those pointers point to will still be there.
You will need to use a for-loop traversing the list, deleting the actual data first and then deleting the pointer to it.
If you don't do this, you'll have data in memory which you cannot access any more, because the pointers to it are gone.


1
2
3
4
5
6
list& list::operator=(list& l) {
if(&l != this) {                //  if there is created any object but l 
Delete();                      //delete and call fuction copy that copies
Copy(l);                      // object l?
}
return *this;                // return l ?  


This is not returning l - it's returning a pointer to a pointer to the current object.
this is a pointer to the current object, so it should suffice.

All the best,
NwN
Thank you for the insight.
Anyone else about what's out there?
Thank you.
Topic archived. No new replies allowed.