Reverse Linked List

Haha this is my second post about this program. But this time the linked list id being printed in reverse. Anybody see the problem, because I can't.

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <cmath>
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;

struct person_info
{
     string firstname;
     double height;  // height in inches
     double weight;     // weight in pounds
     int ID;
};       

typedef person_info infoType;

struct nodeType
{
     infoType info;
     nodeType *link;
};

person_info newOne (ifstream& inFile);
void print_to_Screen(nodeType *list);
nodeType* reverseList(nodeType* head);

int main(){
    
    ifstream inFile;
    
    nodeType *first = NULL, *temp;
    person_info one;
    
    inFile.open("peeps.txt");
    
    if (!inFile)
    {
         cout << " file not found ";
         system ("pause");
         return 1;
    }
    
    while(!inFile.eof())
    {
        one = newOne(inFile);
        temp = new nodeType;
        temp->info = one;
        temp->link = first;
        first = temp;
    }
    reverseList(first);
    print_to_Screen(first);
    
    inFile.close();
    inFile.clear();
    
    inFile.open("changes.txt");
    
    int changeID, changeW;
    
    nodeType *current;
    current = first;
    while(inFile){
          inFile >> changeID;
          inFile >> changeW;
          
          current->info.ID = changeID;
          current->info.weight = current->info.weight + changeW;
          
          current = current->link; 
    }   
    
    inFile.close();
    
    print_to_Screen(first);
 
    system("pause");
    return 0;
}



person_info newOne (ifstream& inFile) 
{  
    string name;
    double weight;
    double height;
    int ID;
      person_info temp;

   
  	    inFile >> name;	
  		inFile >> ID;
  		inFile >> height;  
        inFile >> weight;

    temp.firstname = name;
    temp.weight = weight;
    temp.height = height;
    temp.ID = ID;
       
    return temp;
}

void print_to_Screen(nodeType *list)
{  
      
      nodeType *current;
      current = list;
      

     while (current != NULL)
     {
        cout << setw(10) << (current->info.firstname);
        cout << setw(7) << (current->info.ID);
        cout << setw(7) << (current->info.weight);
        cout << setw(7) << (current->info.height);
        
        current = current->link;
        cout << endl;
     }
     cout << endl;
}
Yes, it should print in reverse order because you are adding every new node at the start of the linked list. If you want to print the nodes in right order you have to add the new node at the end of the linked list. OR what you can do is to print the linked list in reverse order.

But the first way is better.
Last edited on
I'm not that experienced with linked lists. So. possibly point me in the right direction for this?
Here is a code that you may want to study:

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

#include <iostream>

struct Information {
    int a, b, c;
};

struct Node {
    Information info;
    Node *next;

};

struct List {
    Node *head, *tail;
    List() { head = tail = 0; }
};

void addItem(List *list, int x, int y, int z)
{
    if (list->head == 0) // special case. for first item in the list.
    {
        list->head = new Node;
        list->tail = list->head; // the only item is both first and last
    }
    else
    {
        list->tail->next = new Node;
        list->tail = list->tail->next;
    }

    list->tail->info.a = x;
    list->tail->info.b = y;
    list->tail->info.c = z;
    list->tail->next = 0;
}

void loopList(List *list, void (*cb)(Information))
{
    Node *i = list->head;
    while (i != 0)
    {
        cb(i->info);
        i = i->next;
    }
}

void printInformation(Information info)
{
    std::cout << "a:" << info.a << " b:" << info.b << " c:" << info.c << std::endl;
}

int main(int argc, char *argv[])
{
    List *lst = new List;
    addItem(lst, 1, 2, 3);
    addItem(lst, 22, 33, 77);
    addItem(lst, 121, 343, 979);

    loopList(lst, printInformation);

    // free list
    // deallocation code omitted
    return 0;
}


It's just an example, and the code is more organised, and hopefully it doesn't have too many C++ features that you won't understand.

The point is to add new nodes to the last one. In your code you are adding the previous node to the current one, and that is the reason why your linked list is in reverse order. Play the while loop that you've written in your mind and you will understand what I am saying.
Last edited on
Below is the code which should be instead of line 46-50(inside the while loop)
But for this you have to initialize nodeType *last=NULL; on line no.32.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
one = newOne(inFile);
        temp = new nodeType;
        temp->info = one;
        if(first==NULL)           // for addition of 1st node in the list
        {
               first=temp;          // this will add the node at the starting
               last=temp;           // last node at this instant should also be the 1st node
         }
        else
         {
               last->link=temp;  // links temp with the last node i-e the add at the end
               last=temp;          // and then the last node becomes 'temp'
          }

        temp->link = NULL;    // and it will NULL the temp's 'link'  




Edited: This implementation is better then the last one. Thanks unoriginal for pointing out.
Last edited on
possibly point me in the right direction for this?

Hehe, point. Bad puns.
Last edited on
@Ihtsiham

Looping all the way to the last node every time we add a new node is not very practical. It does provide a good solution, but one that is very slow and inelegant.

That is why in my example I just "pointed" out how to store both first and last node in separate variables.

Last edited on
Topic archived. No new replies allowed.