I've created a linked list however, when I try to print it, the list prints backwards.

Apr 6, 2020 at 4:51am
Hello!

I've been working on some project code and the project is basically to read in a file that has contacts in it, put the data into nodes, put those nodes into a list, and then print out the list.

Most of my code is working, however, when I attempt to print out the contact list, instead of printing out correctly (like the format in the text file), it prints out backward.

Example, in the text file, the contacts list looks like:
Julie B. Andrews 320 393 9103
Ryan S. Toesede 102 930 01234
Laila U. Brown 789 102 9310

Instead of printing like that, it prints:
Laila U. Brown 789 102 9310
Ryan S. Toesede 102 930 01234
Julie B. Andrews 320 393 9103

I feel like the problem lies somewhere within my insert function, but I'm not totally sure.

I would really appreciate any pointers on how to combat this problem.

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
  #include <iostream>
#include <stdlib.h>
#include <fstream>
#include <sstream>

using namespace std;
typedef string ElementType;


class Node
{
private:
  ElementType first, mid, last, phone;
  Node *next;

  friend class List;
public:
  Node(ElementType f, ElementType mi, ElementType sur, ElementType ph){
        first = f;
        mid = mi;
        last = sur;
        phone = ph;
        next = nullptr;
  };

void printNode(){
  cout << first << " " << mid << " " << last << " " << phone;
}


};
typedef Node* NodePointer;

/*--------------------------------------------------------------*/
class List {
private:
  Node* first;

public:
List(){
  first =  nullptr;
}

NodePointer getFirst(){
  return first;
};

/*--------------------------------------------------------*/

void insert(ElementType fir, ElementType midd, ElementType las, ElementType phon, NodePointer pos){
  NodePointer pN;
 if(pos == first){
    pN = new Node(fir, midd, las, phon);
    pN->next = pos;
    first = pN;
  }

  else{
    pN = new Node(fir, midd, las, phon);
    pN->next = pos->next;
    pos->next = pN;
  }

}

/*--------------------------------------------------------------*/

void printContactList(){
    NodePointer pN;
    for(pN = first; pN != nullptr; pN = pN->next){
      pN->printNode();
      cout << endl;
    }
  }
};

/*--------------------------------------------------------------*/

void readData(){
  List L;
  ElementType fi, mi, la, ph, ph1, ph2;
  ifstream fin;
  fin.open("C:\\Users\\owner\\Documents\\contacts.txt");


  while(!fin.eof()){
  fin >> fi >>  mi >>la >> ph >> ph1 >> ph2;
  ph = ph + " " + ph1 + " "+ ph2;
  L.insert(fi, mi, la, ph, L.getFirst());
}
  L.printContactList();

}

/*--------------------------------------------------------------*/

int main()
{

    readData();
    return 0;
}
Last edited on Apr 6, 2020 at 5:19am
Apr 6, 2020 at 5:22am
> L.insert(fi, mi, la, ph, L.getFirst());
What is the purpose of the last parameter here?

> pN->next = pos;
> first = pN;
You always do this, so first is always the most recent (pN) node you created.

If you really want to append to the list, then a specific append function would be better.

> while(!fin.eof()){
> fin >> fi >> mi >>la >> ph >> ph1 >> ph2;
eof() doesn't do what you think it does.
It is a status (of a past event), not a prediction of a future event.
That is, your >> has to fail before eof() will become true.

Use this instead.
1
2
3
while ( fin >> fi >>  mi >>la >> ph >> ph1 >> ph2 ) {
  // do stuff
}

Apr 6, 2020 at 5:45am
1. The purpose of the last parameter was to tell where to insert the node, which is at the head, which is "first". (So I guess there lies the problem?)

2. My professor wants us to use .eof(). I'm not sure why but he was very insistent on it. I used the while (fin >> fi...) at first, but he told me I shouldn't.

3. If I make an append function, would that replace the insert function?
Last edited on Apr 6, 2020 at 5:45am
Apr 6, 2020 at 7:05am
> So I guess there lies the problem?
Pretty much.
If you want the list order to reflect the file order, you normally append to the tail of the list, not push things onto the front of the list.

> I'm not sure why but he was very insistent on it.
And if you could prove to him that he is wrong, would you?

Here's what I get with your program, as it stands.
1
2
3
4
5
6
7
8
9
$ cat foo.txt
Julie B. Andrews 320 393 9103
Ryan S. Toesede 102 930 01234
Laila U. Brown 789 102 9310
$ ./a.out 
Laila U. Brown 789 102 9310 102 9310
Laila U. Brown 789 102 9310
Ryan S. Toesede 102 930 01234
Julie B. Andrews 320 393 9103

By failing to correctly handle the end of file condition, you end up with a 'garbage' last iteration of the loop resulting in two "Laila U. Brown" lines printed.

Do you see the same thing when you run your code on your machine?


> 3. If I make an append function, would that replace the insert function?
That depends, how many features do you need your linked list function to have.

A general purpose linked list library has all sorts of methods.
But if you're writing code for a one-off such as this, it make sense to only implement what you need.

Topic archived. No new replies allowed.