S
Sheldon
Hi,
I am trying to understand linked lists and the different ways to write
a linked list and double linked list. I have been trying to get this
function called insert_word to work but to no avail. The function
receives a string from main and then inserts in the list the new word
in alphabetical order. Here is my function:
struct word { // double linked list. Here the list is global
and is first built
struct word *next; // This is the pointer to the next node
struct word *prev; // Pointer to the previous node
char *word; // This is the node that contains the data: a
string
};
struct word *list = NULL; // This is the main header for the list
// smalloc
// replaces malloc and always returns a valid pointer.
void *
smalloc(size_t size)
{
void *newobj;
newobj = malloc(size);
if (!newobj) {
fprintf(stderr, "Cannot allocate memory\n");
exit(EXIT_FAILURE);
}
return newobj;
}
// ************* MY FUNCTION **************
void
insert_word(char* newword)
{
struct word* currentnode = list; // creating a counter to loop over
the list
struct word* newnode = NULL; // new node to be inserted
struct word* prevnode = NULL; // node before current in loop
struct word* afternode = NULL; //node after current in loop
newnode = (struct word*) smalloc(sizeof(struct word)); // alocates
memory for new node
printf("Newword = %s\n",newword);
/* Insert new word in correct alphabetical order
Iterate over the list with a local pointer */
while (1) {
if (currentnode == NULL) { //List is empty. Appending data
printf("List is empty. Inserting first word\n");
newnode->word = newword;
newnode->next = NULL;
newnode->prev = NULL;
list = newnode; // changes the head pointer to point to the new
node so that it is the fist node in the list: it updates the list
afternode = newnode;
prevnode = newnode;
break;
}
if (strcmp(newword,currentnode->word) < 0) { // goes before the
current node
printf("Inserting word before %s node\n", currentnode->word);
newnode->word = newword; // assings the data
newnode->next = currentnode; // links the new node to the
current node
if (prevnode != NULL) {
newnode->prev = prevnode; // links the new node to the previous
node
} else {
newnode->prev = NULL;
}
currentnode = newnode; // move the current to point to
the new node
break;
} else if (strcmp(newword,currentnode->word) > 0) { // insert
after element
printf("Inserting word after %s element\n", currentnode->word);
newnode->word = newword;
if (currentnode->next != NULL) {
newnode->next = afternode;
} else {
newnode->next = NULL;
}
newnode->prev = currentnode; // links the new node to the
previous node
break;
}
afternode = afternode->next;
prevnode = prevnode->next;
currentnode = currentnode->next;
}
}
Any help would be truly appreciated.
Thanks,
Sheldon
I am trying to understand linked lists and the different ways to write
a linked list and double linked list. I have been trying to get this
function called insert_word to work but to no avail. The function
receives a string from main and then inserts in the list the new word
in alphabetical order. Here is my function:
struct word { // double linked list. Here the list is global
and is first built
struct word *next; // This is the pointer to the next node
struct word *prev; // Pointer to the previous node
char *word; // This is the node that contains the data: a
string
};
struct word *list = NULL; // This is the main header for the list
// smalloc
// replaces malloc and always returns a valid pointer.
void *
smalloc(size_t size)
{
void *newobj;
newobj = malloc(size);
if (!newobj) {
fprintf(stderr, "Cannot allocate memory\n");
exit(EXIT_FAILURE);
}
return newobj;
}
// ************* MY FUNCTION **************
void
insert_word(char* newword)
{
struct word* currentnode = list; // creating a counter to loop over
the list
struct word* newnode = NULL; // new node to be inserted
struct word* prevnode = NULL; // node before current in loop
struct word* afternode = NULL; //node after current in loop
newnode = (struct word*) smalloc(sizeof(struct word)); // alocates
memory for new node
printf("Newword = %s\n",newword);
/* Insert new word in correct alphabetical order
Iterate over the list with a local pointer */
while (1) {
if (currentnode == NULL) { //List is empty. Appending data
printf("List is empty. Inserting first word\n");
newnode->word = newword;
newnode->next = NULL;
newnode->prev = NULL;
list = newnode; // changes the head pointer to point to the new
node so that it is the fist node in the list: it updates the list
afternode = newnode;
prevnode = newnode;
break;
}
if (strcmp(newword,currentnode->word) < 0) { // goes before the
current node
printf("Inserting word before %s node\n", currentnode->word);
newnode->word = newword; // assings the data
newnode->next = currentnode; // links the new node to the
current node
if (prevnode != NULL) {
newnode->prev = prevnode; // links the new node to the previous
node
} else {
newnode->prev = NULL;
}
currentnode = newnode; // move the current to point to
the new node
break;
} else if (strcmp(newword,currentnode->word) > 0) { // insert
after element
printf("Inserting word after %s element\n", currentnode->word);
newnode->word = newword;
if (currentnode->next != NULL) {
newnode->next = afternode;
} else {
newnode->next = NULL;
}
newnode->prev = currentnode; // links the new node to the
previous node
break;
}
afternode = afternode->next;
prevnode = prevnode->next;
currentnode = currentnode->next;
}
}
Any help would be truly appreciated.
Thanks,
Sheldon