For an assignment, I am tasked to created a multi-dimensional linked list based on a file input. Each node contains a seat object with the row, column(as a character), and character data of the file. For example, if the file was
ABC
DEF
GHI
Then the linked list would be (where - and | signify connected pointers)
A-B-C
| | |
D-E-F
| | |
G-H-I
I'm currently trying to create all of the nodes first, based on the number of elements in the file. I've created the head node but I am unsure how to go about allocating the rest of the variable number of nodes. So far I have 2 for loops traversing each character of the file to collect the row, column, and character, like so:
scanner.open(fileName);
if(scanner)
{
for(int i = 0; i < cols; i++)
{
for(int j = 0; j < rows; j++)
{
if(i == 0 && j == 0)
{
Node<Seat> *headNode = new Node<Seat>(Seat(j, letter, c));
headNode->up = NULL;
headNode->left = NULL;
setFirst(headNode);
continue;
}
scanner >> c;
letter = ('A' + i);
Node<Seat> *curNode = new Node<Seat>(Seat(j, letter, c));
std::cout << "Row, seat, ticket: " << curNode->getPayload().getRow() << curNode->getPayload().getSeat() << curNode->getPayload().getTicketType() << std::endl;
}
}
}
}
Right now I am creating a single new node and printing it out to verify that it works (and it does), but I am unsure about how to go about creating a new, seperate node for each of the characters in the file. (If I can simply create them, I think I'll be able to figure out how to connect them.)
A,BandC. You then create anadd_node()function that will allocate and initialize each node when it is read from the file and add it to the proper list (based on column number). Trying to allocate everything first is like trying to cram a square peg in a round hole -- not really what you want to do.add()example can be found here Singly Linked List (node only, no wrapper), template example C++ Template Singly-Linked List w/Sort