I have the following code which is a part of doubly linked list implementation. Then I have to use my ADT implementation to create a table of the form value (which is a string), address (which is of type uint32_t) (so a 2 columns table).
typedef struct {
char *label; // is a string like "mov".
uint32_t labelAddress; // the address of the label.
} individualEntry;
typedef void( *FreeList )( void* );
typedef struct listEntries {
individualEntry newInstr;
struct listEntries *prev;
struct listEntries *next;
} listEntries;
listEntries *head;
/*
* HELPER FUNCTIONS
*/
listEntries *createNewList() {
listEntries *newListPtr = malloc(sizeof(listEntries));
return newListPtr;
}
listEntries *createNewEntry( char *label, uint32_t address ) {
listEntries *newEntry = (listEntries*) malloc(sizeof(listEntries));
newEntry->prev = NULL;
newEntry->next = NULL;
newEntry->newInstr.label = label;
newEntry->newInstr.labelAddress = address;
return newEntry;
}
void insert( char *label, uint32_t address ) {
listEntries *temp = head;
listEntries *newEntry = createNewEntry(label, address);
if ( head == NULL ) {
head = newEntry;
return;
}
while ( temp->next != NULL ) {
temp = temp->next;
}
temp->next = newEntry;
newEntry->prev = temp;
}
I need to first create this table and then add to this the records.
My difficulty is on the implementation of a function that inserts to this specific table the values to be added. Do I need another insert function, or do I have to edit the one I have?
If I need a new function that takes as parameters: a pointer to the new table of type struct listEntries, string and address of the record to be added, do I need to consider in the parameters the previous and next records of my list?
I'm not sure how to handle the previous and next pointers after the insertion.
Can someone post an implementation of such a function?
insertfunction – then you only have to get it right once. If you want to insert at the end (as yourinsertdoes), consider maintaining atailas well ashead, to obviate the scan to find the end. In this case, you could put head+tail into astruct list. If you want the list (as represented by the head) to be an argument to your second new function, you should make that function your basicinsertfunction.