How to Implement List of Lists

So I'm trying to create a list of lists where basically in the main list, each node represents a line in a text file. Then in the sub-list, each node represents a word or number in that line. But I'm having trouble on implementing the lists and on how to deallocate each list.

EDIT: I've got it now where I have a separate pointer to point to the next node in the sub-list. So each node has two pointers: (1) Points to the next node in the main list which contains the contents of the next line. (2) Points to the node in the sub-list which contains a word or number of that line.

I'd still like to know if there's a more efficient way to create lists of lists and if implementing it like I have above seems like a good plan. It's working for now but I'd hate to have it work for now then see later on it's actually a horrible implementation.

Also, I'm trying to avoid using the stl list class because I'm not sure my professor will allow it.

Thanks!
Last edited on
> avoid using the stl list class because I'm not sure my professor will allow it
Ask.


The algorithms for inserting and deleting nodes are completely independent to whatever type the nodes are storing.
There is no difference in implementing a list of integers, a list of strings, a list of lists of dogs.


> I cannot keep the contents of a line in an array;
¿what are you talking about?
I'm pretty sure he isn't going to allow it and I'd rather understand how to do it without using the stl library.

I understand that but that isn't really what I asked. I'm asking if having two separate pointers, one for pointing to the next node in the main list then another pointing to the next node in the sub-list, is an efficient and maintainable way to implement a list of lists. Let me elaborate on the assignment further.

We have to read in a text file. Each line of the text has words or numbers separated by a tab. The words and numbers must all be stored on nodes of a list, then each line of the text file must also be indicated by nodes of a list -- therefore, a list of lists. Then we are asked to do operations where an op will indicate the line we do the op on and what the op is. Some ops are things like finding the max num or finding the mean.

So what I'd like to know is, with the way I've implemented the lists of lists and for what the assignment requires, does this seem like a good and efficient implementation? Or do you have any suggestions that would make doing ops on this list of lists easier?
I would recommend to make the nodes to have a list instead of a pointer to the header.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class list_of_list{
   class node{
      //node_list *header; //instead of this
      list the_list; //this (data)

      node *next; //node of list_of_list
   };
};

class list{
   class node{
      node *next; //node of list
      T data;
   };
};
An if you can typedef nih::list< ni::list<T> > nih::list_of_list<T>;

The code to operate a list_of_list or a list would be equivalent. You may copy-paste the algorithms for inserting and deleting elements.


> Some ops are things like finding the max num or finding the mean.
you'll be operating on a list, instead of a header pointer.
And hopefully all those operations you've already implemented in previous assignments.
Topic archived. No new replies allowed.