Linked List of Linked Lists

Hello!

I'm trying to create a linked list where each node contains a grade level (ints), and where each node points to a linked list of students and information about them (age, Last name, first name). It will look like this

Grade 1 -------> Grade 2 -------> Grade 3 -------> Grade 4 -------> NULL
'''''|''''''''''''''''''''''''''''''''|''''''''''''''''''''''''''''''''|'''''''''''''''''''''''''''''''|
''''\/'''''''''''''''''''''''''''''''\/''''''''''''''''''''''''''''''\/'''''''''''''''''''''''''''''\/
Student 1'''''''''''Student 1''''''''''Student 1''''''''''''Student 1
'''''|''''''''''''''''''''''''''''''''|''''''''''''''''''''''''''''''''|'''''''''''''''''''''''''''''''|
''''\/'''''''''''''''''''''''''''''''\/''''''''''''''''''''''''''''''\/'''''''''''''''''''''''''''''\/
Student 2'''''''''''Student 2''''''''''Student 2''''''''''''Student 2

etc...

So far I have the first linked list finished (the grade level one). It can add, delete, and print the contents of the nodes. I'm not entirely sure how I make each of the nodes have their own linked list that has the same functionality (I want to be able to add, delete and print the students of each grade level)


Here is my header file, please let me know what I need to add in order to make this work!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include <stdio.h>



class List
{
private:
	typedef struct node{
		int data;
		node* next;
	}*nodePtr;

	nodePtr head;
	nodePtr curr;
	nodePtr temp;

public:
	List();
	void AddNode(int addData);
	void DeleteNode(int delData);
	void PrintList();

};


I've spent a good deal of time on trying to figure this out. I know the above doesn't show this, but I've started over multiple times. I'm trying to start this over on a clean slate, and hopefully get help from people much better at this than me, so I don't run into a dead end again.

Any help would be greatly appreciated!
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <list>

//...

struct Student
{
/*
 */
};

//...
using Node = std::pair<int, std::list<Student>>;
std::list<Node> data;
Thanks for the reply!

I tried putting what you said into the header, but I had some errors, this is the updated 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
#include <stdio.h>
#include <list>
#include <string>

struct Student
{
	void AddStudent(int gradeLevel, std::string lastName, std::string firstName);
	void DeleteNode(int gradeLevel, std::string lastName, std::string firstName);
	void PrintStudents(int gradeLevel);
};
class List
{
private:
	typedef struct node
	{
		int data;
		node* next;
		using Node = std::pair<int, std::list<Student>>;
		std::list<Node> data;
		
	}*nodePtr;

	nodePtr head;
	nodePtr curr;
	nodePtr temp;
public:
	List();
	void AddNode(int addData);
	void DeleteNode(int delData);
	void PrintList();
};


I'm not entirely sure where the using declaration was supposed to go. Sorry for my incompetence, I'm pretty new to C++.
Well, it is whole class. PrintList and Add/DeleteNode will be better as non-member functions. But if you need them to be members, here is the example:

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
#include <iostream>
#include <list>
#include <utility>

struct Student
{
//...
};

class MagicList
{
  public:
    void AddNode(int addData);
    void DeleteNode(int delData);
    void PrintList() const;
  private:
    using Node = std::pair<int, std::list<Student>>;
    std::list<Node> data;
};

void MagicList::AddNode(int addData)
{
    data.emplace_back(addData, std::list<Student>());
}

void MagicList::DeleteNode(int delData)
{
    data.remove_if([delData](const Node& x){ return x.first == delData;});
}

void MagicList::PrintList() const
{
    unsigned count = 0;
    for(const auto& item: data) {
        std::cout << "Node " << count << ", data value: " << item.first << std::endl;
        ++count;
        std::cout << "Students: ";
        if (item.second.empty()) {
            std::cout << "None;" << std::endl;
            continue;
        }
        bool first = true;
        for(const auto& student: item.second) {
            std::cout << (first?"":", ") ;//<< student;
                                          //↑Commented for the sake of example, 
                                          //provide your own operator << overload
        }
        std::cout << ';' << std::endl;
    }
}


int main()
{
    MagicList l;
    l.AddNode(6);
    l.AddNode(2);
    l.AddNode(4);
    l.DeleteNode(2);
    l.PrintList();
}
http://coliru.stacked-crooked.com/a/ec0947002552364c

Read about
pair: http://en.cppreference.com/w/cpp/utility/pair
list: http://en.cppreference.com/w/cpp/container/list
Last edited on
Topic archived. No new replies allowed.