Bladeren bron

some comments

Rafael Arce Nazario 4 jaren geleden
bovenliggende
commit
db9026ac79
1 gewijzigde bestanden met toevoegingen van 13 en 1 verwijderingen
  1. 13
    1
      Llist.cpp

+ 13
- 1
Llist.cpp Bestand weergeven

35
 
35
 
36
 void Llist::insertAfter(Node *p, ElementType d) {
36
 void Llist::insertAfter(Node *p, ElementType d) {
37
   Node *n;
37
   Node *n;
38
+
39
+  // p == NULL is interpreted as prepending
38
   if (p == NULL) {
40
   if (p == NULL) {
39
     n = new Node(d, head);
41
     n = new Node(d, head);
40
     head = n;
42
     head = n;
65
 void Llist::printRev() const {
67
 void Llist::printRev() const {
66
   printRev(head); 
68
   printRev(head); 
67
 }
69
 }
70
+
68
 void Llist::printRev(Node *p) const {
71
 void Llist::printRev(Node *p) const {
69
   if (p != NULL) {
72
   if (p != NULL) {
70
     printRev(p->next);
73
     printRev(p->next);
74
 
77
 
75
 void Llist::removeAfter(Node *cur) {
78
 void Llist::removeAfter(Node *cur) {
76
   Node *suc;
79
   Node *suc;
80
+
81
+  // cur == NULL is interpreted as removing the first
82
+  // node of the list
77
   if (cur == NULL and head != NULL) {
83
   if (cur == NULL and head != NULL) {
78
     suc = head->next;
84
     suc = head->next;
79
     delete head;
85
     delete head;
80
     head = suc;
86
     head = suc;
81
-
87
+    // in case the removed node was the only node
82
     if (suc == NULL) tail = NULL;
88
     if (suc == NULL) tail = NULL;
83
   }
89
   }
84
   else if (cur->next != NULL) {
90
   else if (cur->next != NULL) {
85
     suc = cur->next->next;
91
     suc = cur->next->next;
86
     delete cur->next;
92
     delete cur->next;
87
     cur->next = suc;    
93
     cur->next = suc;    
94
+    // if the removed node was the last, update the tail pointer
88
     if (suc == NULL) tail = cur;
95
     if (suc == NULL) tail = cur;
89
   }
96
   }
90
   length--;
97
   length--;
91
 }
98
 }
92
 
99
 
100
+// Assignment operator:
101
+// 1. empty the list of the invoking object
102
+// 2. traverse the list of the parameter appending each
103
+//    node data to the invoking object
104
+
93
 Llist& Llist::operator=(const Llist &L) {
105
 Llist& Llist::operator=(const Llist &L) {
94
   if (this != &L) {
106
   if (this != &L) {
95
     while(head != NULL) removeAfter(NULL);
107
     while(head != NULL) removeAfter(NULL);