c++ - Linked list within a linked list (2D linked list?) -


i have txt file contains matrix of chars(1 or 2 @ each position in matrix)

c p o hr s a

n hw n l z r

w t o o ta a

i o s s e t

something this. managed create linked list , store every element of matrix in list (separately).

struct datanode{     char data[3];     struct datanode *nextdata; };  void initnode(datanode *head, char x[3]) {     for(int i=0; i<3; i++)         head->data[i]=x[i];     head->nextdata=null; }  void addnode(datanode *head, char x[3]) {     datanode *newnode = new datanode;     for(int i=0; i<3; i++)         newnode->data[i]=x[i];     newnode->nextdata=null;      datanode *curr = head;     while(curr) {         if(curr->nextdata==null) {             curr->nextdata = newnode;             return;         }         curr = curr->nextdata;     } }  int main() { char input[3]; if(in.is_open()) {         in>>input;         initnode(head,input);         for(int i=0; i<3; i++)             dieside[i]=input[i];          while(in>>input) {             addnode(head,input);         }         in.close();     } } 

so far, works should, , guess i'm happy it.

what need now, linked list, elements still char[3] types, there has first list containing row of 6 elements, , then, list, containing of 6 element lists.

i hope made myself clear wishes.

i'm thinking creating struct, next pointers each of 2 active lists, still not sure idea.

how recommend me go doing this?

edit

just little help, please... have re-implemented of functions suit struct (@daniel) suggested, , appear work. however, need way "reset" datanode* want use creating small lists. way entire matrix printed many times there lines in file. have is>

char input[3]; int counter=0; struct datanode *head = new datanode; //creates list of elements struct datanode *head_side = new datanode; //want use 1 create smaller lists struct diesidenode *head_die = new diesidenode; //creates list of smaller lists  if(in.is_open()) {         in>>input;         initnode(head,input);         initnode(head_side, input);         counter++;      while(in>>input) {         addnode(head,input);         addnode(head_side, input);         counter++;         if( counter == 6 ) {             initside(head_die, head_side);             head_side=0;         }else if(counter%6==0) {             addside(head_die, head_side);             head_side=0;         }     }     in.close(); } 

this code extracts first 6 elements, , puts first element of list, stops working there.

i'll give little hint started. know, linked-list node contains data , pointer next element of list. call "2-d linked list" implemented linked-list of linked-lists. each node in list points linked list. need define new type:

struct listnode {     datanode* datarowhead;     struct listnode* nextrow; }; 

what trying have 6 listnodes connected linked-list. each listnode contains pointer datanode head of linked-list row corresponds listnode points it.

i leave implementation you.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

Kivy: Swiping (Carousel & ScreenManager) -

jdbc - Not able to establish database connection in eclipse -