Linked list. Accessing data in element inside node.

Aug 23, 2014 at 4:55pm
1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
In my class Car. I have this

ID Model Driver
1  Car   John

If im using the default print() function from the Linked List, it will show all of em

ID Model Driver
1  Car   John

What I want it to show is only the driver

ID Driver
1  John

What should I write to retrieve the driver only?


2)
1
2
3
4
5
6
7
8
9
10
11
12
I want the change the driver data into something else

ID Model Driver
1  Car   John
Insert new driver: Popo

ID Model Driver
1  Car   Popo
Driver changed!

What should I write to change the driver without having 
to reconstruct the whole element inside the node?



Here's the sample of the code
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
Class Car(){
 ....
 friend ostream& operator<< (ostream& os, Car& c) {
  os << left << c.getModel();
  os << left << c.getDriver();
  return os;
  }
 friend istream& operator>> (istream& is, Car& c){
  is >> model;
  is >> driver;
  return is;
 }
};

template <typename T>
struct Node {
 .....
};

template <typename T>
class LList {
 .....
 void print() {
   Node<T> *ptr = start;
   int i=0;
   while (ptr!=NULL ) {
    cout << "\t" << ++i << ". " << ptr->info;
    ptr = ptr->next;
    }
   }   
};

int main(){
 LList<Car> cars;
 Car c("Car", "John");
 cars.insertBack(c);
 cars.print();
 system("pause");
 return 0;
}


Are those possible? Thanks!
Last edited on Aug 23, 2014 at 5:03pm
Aug 23, 2014 at 5:42pm
Possible, yes.

1. Your LList::print could take a function pointer with 0 as default. If pointer is 0, you print as you do now. If pointer is not 0, then you call the function with the ptr->info as parameter.

2. You LList must have a method to reference single node, and the node type must allow change of member.
Aug 23, 2014 at 6:04pm
Thanks for the reply.

Talking about referencing single node. I've wrote this if it's what you're refering from
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void insertAt (T& elementToInsert, int atIndex) {
    int i = 1;
    Node<T> *ptr = start;
    Node<T> *newNode = new Node<T>;
    while(ptr != NULL && i != atIndex)
        if(i != atIndex)
        {
            ptr = ptr->next;
            i++;
        }
        if(i == atIndex)
            newNode->info = elementToInsert;
            newNode->next = ptr->next;
            ptr->next = newNode;
    }


And I rephrase my question.

how to retrieve member of element inside node?
how to write the code. I don't have any clue of doing it
Last edited on Aug 23, 2014 at 6:13pm
Aug 23, 2014 at 6:26pm
See http://www.cplusplus.com/reference/forward_list/forward_list/front/
Do you notice, that the front() returns a reference to value_type object?

The front() is just an alternative for *(begin()). Begin returns iterator. Iterator can advance to point other elements, but then dereference would return reference to that further object.

You could consider a LList member
T& itemAt (int atIndex) const;

PS. If the user gives an invalid atIndex, then your insertAt will crash.
Last edited on Aug 23, 2014 at 6:28pm
Topic archived. No new replies allowed.