How do you access linked list stored in array?

I want to store some employee information in 2 structs linked together by Node *top. Each employee should have their own linked list, but this code piles all the info into 1 linked list. How do I access the next node so that new information can be stored there?

1
2
3
4
5
6
7
8
9
struct Skill {
   string name;
   int years;
};

struct Node {
   Skill data;
   Node * next;
};

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
                #include <iostream>
                #include <fstream>

                using namespace std;

                #include "Skill.h"

                // insert your function prototypes here
                struct Emp{
                    string fname;
                    int num;
                    Node *top;
                };

                Node * createNode (string skill_name, int skill_years){
                    Node *newNode;
                    newNode= new Node;

                    newNode->data.name=skill_name;
                    newNode->data.years=skill_years;

                    newNode->next==NULL;
                    return newNode;
                }

                Node * insertAtHead (Node * top, string skill_name, int skill_years){
                    Node *newNode;
                    newNode=createNode(skill_name, skill_years);

                    newNode->next=top;
                    top=newNode;

                    return top;
                }


                bool contains (Node * top, string key){
                    if(top==NULL)
                        return false;
                    if(top->data.name==key)
                        return true;

                    return contains(top->next, key);
                }

                int size (Node * top){
                    if(top==NULL)
                        return 0;

                    return 1+size(top->next);
                }

                void printList (Node * top) {
                   Node * curr;

                   curr = top;
                   while (curr != NULL) {
                      cout << curr->data.name<<", "<< curr->data.years << endl;
                      curr = curr->next;
                   }
                }


                int main() {

                    ifstream in;
                    in.open("Employees.txt");

                    Emp e[30];
                    Skill s;

                    string skill="";

                    int year=0;
                    int count=0;
                    int option=0;
                    int i=0;
                    int len=0;

                    Node *top;
                    top = NULL;

                    printMenu();
                    cin>>option;



                    if(option==1){
                        in>>e[i].fname;

                        while(e[i].fname!="END"){
                            in>>e[i].num;

                            cout<<e[i].fname<<" - "<<e[i].num<<endl;

                            count=0;
                                while(count!=e[i].num){
                                    in>>skill>>year;
                                    top=insertAtHead(top, skill, year);
                                    count++;
                                }
                                printList(top);

                            cout<<endl;

                            i++;
                            in>>e[i].fname;
                        }

                        cout<<"Data read"<<endl;
                    }

                }


I'm trying to do something like this below, but I'm not sure of the proper syntax
 
 e[i].top=top->next; 
Last edited on
do yourself a favor and encapsulate your nih list class

`top' is a `Node'
`next' is a `Node*' (a pointer to a Node)
¿how do you expect that assignment to work?
Last edited on
I just realized the error, I had it in my other code. I've fixed it now. But it's still left me with the same problem.

eg)This is the txt file
1
2
3
Joseph 3 farming 10 fishing 5 hunting 3
Mary 2 sewing 15 talking 20 
Sally	0


My code gives me a linked list of hunting, fishing farming, talking, sewing
What I am trying to do is give Joseph a LL for farming, fishing, hunting & Mary a LL for sewing and talking separately. Essentially giving each entry into e[i] their own linked list by using a pointer to the first Node of that Emp
I 'm just confused about the syntax to use.
Last edited on
Topic archived. No new replies allowed.