0

I have the following program which reads words from a text file and creates a single linked list, with each node containing: word, count, next.

When a word already exists in the linked list the count is updated, otherwise, a node is created at the end of the linked list.

All of my functions work, except for the one where I am adding a word to the end of the linked list. There is likely an error with linkage of the nodes?

with my following text file: line1 "my name is natalie", line 2 "my dog is niko" I should be getting the following output: my(2), name(1), is(2), natalie(1), dog(1), niko(1) but I am getting: my(2), dog(2), s(1), iko(1), is(1), niko(1)

WHERE IS MY ERROR?

//function to add word to linked list
struct WordNode *addWord(char* word, struct WordNode *wordListHead){
        //create new node
        struct WordNode *current = malloc(sizeof(struct WordNode));
        current->word = word;
        current->count = 1;
        current->next = NULL;

        //
        while(1){
                if((wordListHead != NULL)&&(wordListHead->next ==NULL)){
                        //connect 
                        wordListHead->next=current;
                        break;
                }
                wordListHead = wordListHead->next;
        }

}

called here in main:

char *filename = argv[1];
        FILE *fp = fopen(filename, "r");
        printf("%s\n", filename);
        if (fp == NULL){
                printf("Error: unable to open the file ");
                printf("%s", filename);
                return 1;
        }

        else {

                char *delim = " \t\n,:;'\".?!#$-><(){}[]|\\/*&^%@!~+=_"; // These are our word delimiters.
                char line[1000];
                char * token;
                int count = 0;//count used so that first word of the doc can be created as head node
                //create head pointer
                struct WordNode *wordListHead = NULL;
                //create current pointer 
                struct WordNode *current = NULL;

                //iterate through each line in file
                while(fgets(line, 1000, fp)){
                        //seperate each word

                        //first word of line
                        token = strtok(line, delim);
                        printf("%s\n", token);

                        if(count == 0){
                                //first word of document, create first wordnode (head node)
                                wordListHead = malloc(sizeof(struct WordNode));
                                wordListHead->word = token;
                                wordListHead->count = 1;
                                wordListHead->next = NULL;
                        }

                        else{
                                //check if first word of line exists in linked list
                                if((doesWordExist(token, wordListHead)) == 0){
                                        //update count
                                        updateCount(token, wordListHead);
                                }
                                else{
                                        //create node
                                        addWord(token, wordListHead);
                              }
                        }


                        //iterate through all the other words of line
                        while ((token=strtok(NULL, delim)) != NULL){
                                printf("%s\n", token);

                                //check if name is in linked list
                                if((doesWordExist(token, wordListHead)) == 0){
                                        //update count
                                        updateCount(token, wordListHead);
                                }
                                else{
                                        //create node
                                        addWord(token, wordListHead);
                                }
                        }
                        count++;
                }
                printWordList(wordListHead);

        }

}


struct defined here:

//structure definition of linked list node
#ifndef WORDLISTH
#define WORDLISTH
struct WordNode{
        char *word;
        unsigned long count;
        struct WordNode *next;
};
void printWordList( struct WordNode *wordListHead);

struct WordNode *addWord(char* word , struct WordNode *wordListead);

#endif

other functions for reference:

//function to check if word is in linked list
bool doesWordExist(char* myword, struct WordNode *wordListHead){

        while (wordListHead != NULL){
                if(strcmp(wordListHead->word, myword) == 0){
                        return 0;
                }
                wordListHead= wordListHead-> next;
        }
        return 1;
}

//function to update the count of word 
void updateCount(char* myword, struct WordNode *wordListHead){

        while (wordListHead != NULL){
                if(strcmp(wordListHead->word, myword) == 0){
                        //update count value
                        //capture current count and add 1
                        int curcount = wordListHead->count;
                        int newcount = curcount + 1;
                        wordListHead->count = newcount;
                        //printf("%d\n", newcount);
                }
                wordListHead= wordListHead-> next;
        }

}
//function to print word list
//takes head node as argument
void printWordList( struct WordNode *wordListHead){
        //WordNode *toyptr = wordListHead;
        while (wordListHead != NULL){
                printf("%s\n", wordListHead->word);
                printf("%ld\n", wordListHead->count);
                wordListHead = wordListHead -> next;
        }
}
3
  • //create node addWord(token, wordListHead); never changes wordListHead. addWord() needs to be used and implemented differently to change wordListHead. Commented Dec 9, 2020 at 19:12
  • doesWordExist() and addWord() each traverse the list, the first to find out if the word exists in the list, the second to add-at-end in case it doesn't. That can all be done in the addWord() function. As you are iterating to place the new node at the end of the list, compare words as you go. If a match is found, increment count, free(current) and break the loop at that point. while(fgets(line, 1000, fp)) don't use MagicNumbers in your code, instead while(fgets(line, sizeof line, fp)) Commented Dec 9, 2020 at 20:38
  • Example of how to combine node create and duplicate check Good for 30 days. Commented Dec 9, 2020 at 22:18

2 Answers 2

1

When you are storing token into your struct, you are using a pointer that is part of the input buffer.

On a new input line, the tokens gathered from previous lines will be corrupted/trashed.

You need to allocate space to store the token in the struct on the heap. Use strdup for that.

So, in addWord, you want:

current->word = strdup(word);

And in main, you want:

wordListHead->word = strdup(token);

UPDATE:

That's the primary issue. But, your code does a bunch of needless replication.

addWord doesn't handle an empty list. But, if it did there would be no need for main to have separate [replicated] code for the first word and subsequent words on the line.

The strcmp can be incorporated into addWord and it can "do it all". (i.e. a single scan of the list)

For doesWordExist, it returns a bool on a match. If it returned the pointer to the element that matched, updateCount would just have to increment the count [and not rescan the list]. I've updated those functions accordingly, but they are no longer needed due to the changes to addWord

Here's how I would simplify and refactor the code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef int bool;

#ifdef DEBUG
#define dbgprt(_fmt...) \
    printf(_fmt)
#else
#define dbgprt(_fmt...)     do { } while (0)
#endif

//structure definition of linked list node
#ifndef WORDLISTH
#define WORDLISTH
struct WordNode {
    char *word;
    unsigned long count;
    struct WordNode *next;
};
void printWordList(struct WordNode *wordListHead);

#endif

//function to add word to linked list
struct WordNode *
addWord(char *word, struct WordNode **list)
{
    struct WordNode *curr;
    struct WordNode *prev = NULL;
    struct WordNode *newnode = NULL;

    for (curr = *list;  curr != NULL;  curr = curr->next) {
        if (strcmp(curr->word,word) == 0) {
            newnode = curr;
            break;
        }
        prev = curr;
    }

    // create new node
    do {
        // word already exists
        if (newnode != NULL)
            break;

        newnode = malloc(sizeof(struct WordNode));
        newnode->word = strdup(word);
        newnode->count = 0;
        newnode->next = NULL;

        // attach to tail of list
        if (prev != NULL) {
            prev->next = newnode;
            break;
        }

        // first node -- set list pointer
        *list = newnode;
    } while (0);

    // increment the count
    newnode->count += 1;

    return newnode;
}

//function to check if word is in linked list
struct WordNode *
findWord(char *myword, struct WordNode *head)
{
    struct WordNode *curr;

    for (curr = head;  curr != NULL;  curr = curr->next) {
        if (strcmp(curr->word,myword) == 0)
            break;
    }

    return curr;
}

//function to update the count of word
void
updateCount(char *myword, struct WordNode *head)
{
    struct WordNode *match;

    match = findWord(myword,head);
    if (match != NULL)
        match->count += 1;
}

//function to print word list
//takes head node as argument
void
printWordList(struct WordNode *head)
{
    struct WordNode *curr;

    for (curr = head;  curr != NULL;  curr = curr->next) {
        printf("%s", curr->word);
        printf(" %ld\n", curr->count);
    }
}

int
main(int argc, char **argv)
{
    char *filename = argv[1];
    FILE *fp = fopen(filename, "r");

    printf("FILE: %s\n", filename);
    if (fp == NULL) {
        printf("Error: unable to open the file ");
        printf("%s", filename);
        return 1;
    }

    // These are our word delimiters.
    char *delim = " \t\n,:;'\".?!#$-><(){}[]|\\/*&^%@!~+=_";

    char line[1000];
    char *token;

    // create head pointer
    struct WordNode *wordListHead = NULL;

    // iterate through each line in file
    while (fgets(line, sizeof(line), fp)) {
        // seperate each word

        // first word of line
        char *bp = line;

        while (1) {
            token = strtok(bp, delim);
            bp = NULL;

            if (token == NULL)
                break;

            dbgprt("TOKEN1: %s\n", token);

            addWord(token,&wordListHead);
        }
    }

    printWordList(wordListHead);

    return 0;
}

UPDATE #2:

Note that addWord and findWord replicate code. The first part of addWord is [essentially] duplicating what findWord does.

But, addWord can not just use findWord [which would be desirable] because findWord, if it fails to find a match returns NULL. In that case, it doesn't [have a way to] communicate back the last element (i.e. the "tail" of the list) which addWord needs to append to.

While we could add an extra argument to findWord to propagate this value back, a better solution is to create a different struct that defines a "list".

In the existing code, we are using a "double star" pointer to the head word node as a "list". Using a separate struct is cleaner and has some additional advantages.

We can just pass around a simple pointer to the list. We no longer need to worry about whether we should be passing a double star pointer or not.

Although we're only using a singly linked list, a separate list struct helps should we wish to convert the list to a doubly linked list [later on].

We just pass around a pointer to the list, and the list can keep track of the head of the list, the tail of the list, and the count of the number of elements in the list.

Linked lists lend themselves well to sorting with mergesort. The list count helps make that more efficient because it is much easier to find the "midpoint" of the list [which a mergesort would need to know].

To show the beginnings of the doubly linked list, I've added a prev element to the word struct. This isn't currently used, but it hints at the doubly linked version.

I've reworked all functions to take a pointer to a list, rather than a pointer to the head node.

Because the list struct has a tail pointer, addWord can now call findWord. If findWord does not find a match, addWord can simply use the head/tail pointers in the list to find the correct insertion point.

To simplify a bit more, I've changed the word node [and the list struct] to use some typedef statements.

Also, it's more usual/idiomatic to have the dominant struct pointer be the first argument, so I've reversed the order of the arguments on some of the functions.

Anyway, here's the [further] refactored code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef int bool;

#ifdef DEBUG
#define dbgprt(_fmt...) \
    printf(_fmt)
#else
#define dbgprt(_fmt...)     do { } while (0)
#endif

//structure definition of linked list node
#ifndef WORDLISTH
#define WORDLISTH

// word frequency control
typedef struct WordNode Word_t;
struct WordNode {
    const char *word;
    unsigned long count;
    Word_t *next;
    Word_t *prev;
};

// word list control
typedef struct {
    Word_t *head;
    Word_t *tail;
    unsigned long count;
} List_t;

void printWordList(List_t *list);

#endif

// create a list
List_t *
newList(void)
{
    List_t *list;

    list = calloc(1,sizeof(*list));

    return list;
}

// function to check if word is in linked list
Word_t *
findWord(List_t *list,const char *myword)
{
    Word_t *curr;

    for (curr = list->head;  curr != NULL;  curr = curr->next) {
        if (strcmp(curr->word,myword) == 0)
            break;
    }

    return curr;
}

//function to add word to linked list
Word_t *
addWord(List_t *list,const char *word)
{
    Word_t *match;

    do {
        // try to find existing word
        match = findWord(list,word);

        // word already exists
        if (match != NULL)
            break;

        // create new node
        match = malloc(sizeof(*match));
        match->word = strdup(word);
        match->count = 0;
        match->next = NULL;

        // attach to head of list
        if (list->head == NULL)
            list->head = match;

        // append to tail of list
        else
            list->tail->next = match;

        // set new tail of list
        list->tail = match;

        // advance list count
        list->count += 1;
    } while (0);

    // increment the word frequency count
    match->count += 1;

    return match;
}

//function to update the count of word
void
updateCount(List_t *list,const char *myword)
{
    Word_t *match;

    match = findWord(list,myword);
    if (match != NULL)
        match->count += 1;
}

//function to print word list
//takes head node as argument
void
printWordList(List_t *list)
{
    Word_t *curr;

    for (curr = list->head;  curr != NULL;  curr = curr->next) {
        printf("%s", curr->word);
        printf(" %ld\n", curr->count);
    }
}

int
main(int argc, char **argv)
{
    char *filename = argv[1];
    FILE *fp = fopen(filename, "r");

    printf("FILE: %s\n", filename);
    if (fp == NULL) {
        printf("Error: unable to open the file ");
        printf("%s", filename);
        return 1;
    }

    // These are our word delimiters.
    char *delim = " \t\n,:;'\".?!#$-><(){}[]|\\/*&^%@!~+=_";

    char line[1000];
    char *token;

    // create list/head pointer
    List_t *list = newList();

    // iterate through each line in file
    while (fgets(line, sizeof(line), fp) != NULL) {
        // seperate each word

        // first word of line
        char *bp = line;

        while (1) {
            token = strtok(bp, delim);
            bp = NULL;

            if (token == NULL)
                break;

            dbgprt("TOKEN1: %s\n", token);

            addWord(list,token);
        }
    }

    printWordList(list);

    return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

THANKS SO MUCH :)
Get down Craig! That's an answer.
1

@Craig Estey has provided a great answer for you, so don't change your answer selection, but rather than just leave you a link in the comments, there are a couple of important ways of looking at list operations that may help, and you must use a memory/error checking program to validate your use of allocated memory, especially when dealing with list operations.

Take a node holding a string with a reference count of the additional number of times the string occurs, as in your case, e.g.

typedef struct node_t {     /* list node */
    char *s;
    size_t refcnt;
    struct node_t *next;
} node_t;

Iterating With Address of Node & Pointer to Node Eliminates Special Cases

Using both the address of the node and pointer to node is discussed in Linus on Understanding Pointers.

For example, where you need to check if (list->head == NULL) to add the first node to the list, if iterating with both the address of the node and pointer to node, you simply assign the allocated pointer to your new node to the address of the current node. This works regardless whether it is the first, middle or last node. It also eliminates having to worry about what the previous node was when removing nodes from the list. At the node to delete, you simply assign the contents of the next node to the current address and free the node that was originally there. This reduces your add node function to something similar to:

/** add node in sort order to list.
 *  returns pointer to new node on success, NULL otherwise.
 */
node_t *add_node (node_t **head, const char *s)
{
    node_t **ppn = head,                            /* address of current node */
            *pn = *head;                            /* pointer to current node */

    while (pn) {                                    /* iterate to last node */
        if (strcmp (pn->s, s) == 0) {               /* compare node string with string */
            pn->refcnt += 1;                        /* increment ref count */
            return *ppn;
        }
        ppn = &pn->next;                            /* ppn to address of next */
        pn = pn->next;                              /* advance pointer to next */
    }
    
    return *ppn = create_node (s);                  /* allocate and return node */
}

(note: by delaying allocation for the new node and string (create_node (s) above), you avoid allocating until you know the string needs to be added -- simplifying memory handling)

As mentioned in the comments above, this combines your doesWordExist() traversal of the list and your addWord() traversal to find the end into a single traversal. If there are hundreds of thousands of nodes in your list, you don't want to traverse the list multiple times.

Using strdup() is Fine, but know it's POSIX not standard C

strdup() is handy for duplicating strings and assigning the result to a new pointer, but strdup() is provided by POSIX, so not all implementation will provide it. Additionally, strdup() allocates memory, so just as with any function that allocates memory, you must check that the result is not NULL before using the pointer that is returned. You can avoid the potential portability issue by writing a short equivalent. For example in the create_node() shown above, it does:

/** helper function to allocate node, and storage for string.
 *  copies string to node and initializes node->next pointer NULL
 *  and node->refcnt zero. returns pointer to allocated node on success,
 *  NULL otherwise.
 */
node_t *create_node (const char *s)
{
    size_t len = strlen (s);                        /* get string length */
    node_t *node = malloc (sizeof *node);           /* allocate node */

    if (!node) {    /* validate EVERY allocation */
        perror ("create_node: malloc node");
        return NULL;
    }

    if (!(node->s = malloc (len + 1))) {            /* allocate for string */
        perror ("create_node: malloc node->s");
        free (node);        /* on failure, free node before returning NULL */
        return NULL;
    }

    memcpy (node->s, s, len+1);                     /* copy string to node */
    node->refcnt = 0;                               /* initialize ref count */
    node->next = NULL;                              /* initialze next NULL */

    return node;    /* return allocated & initialized node */
}

Freeing Allocated Memory

Any time you write code creating data structures, they need to be able to clean up after themselves in the event you want to remove a single node, or are done using the list. This becomes imperative when you create lists, etc. that are declared and used solely within functions below main() as the memory isn't freed on the function return. With main(), on return the program exits and will free all allocated memory. However if the list is created and used solely below main() a memory leak will result each time that function is called if the list memory is not freed before return. A function that frees all memory is short and easy to write, e.g.

/** delete all nodes in list */
void del_list (node_t *head)
{
    node_t *pn = head;                              /* pointer to iterate */

    while (pn) {                                    /* iterate over each node */
        node_t *victim = pn;                        /* set victim to current */
        pn = pn->next;                              /* advance pointer to next */
        free (victim->s);                           /* free current string */
        free (victim);                              /* free current node */
    }
}

(**no need to worry about the refcnt since you are deleting the list)

A short example including all of these points, as well as a function del_node() to remove a single node from the list (or reduce the refcnt without removing the node if the refcnt is non-zero) can be:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define MAXC 1024   /* if you need a constant, #define one (or more) */

typedef struct node_t {     /* list node */
    char *s;
    size_t refcnt;
    struct node_t *next;
} node_t;

/** helper function to allocate node, and storage for string.
 *  copies string to node and initializes node->next pointer NULL
 *  and node->refcnt zero. returns pointer to allocated node on success,
 *  NULL otherwise.
 */
node_t *create_node (const char *s)
{
    size_t len = strlen (s);                        /* get string length */
    node_t *node = malloc (sizeof *node);           /* allocate node */

    if (!node) {    /* validate EVERY allocation */
        perror ("create_node: malloc node");
        return NULL;
    }

    if (!(node->s = malloc (len + 1))) {            /* allocate for string */
        perror ("create_node: malloc node->s");
        free (node);        /* on failure, free node before returning NULL */
        return NULL;
    }

    memcpy (node->s, s, len+1);                     /* copy string to node */
    node->refcnt = 0;                               /* initialize ref count */
    node->next = NULL;                              /* initialze next NULL */

    return node;    /* return allocated & initialized node */
}

/** add node in sort order to list.
 *  returns pointer to new node on success, NULL otherwise.
 */
node_t *add_node (node_t **head, const char *s)
{
    node_t **ppn = head,                            /* address of current node */
            *pn = *head;                            /* pointer to current node */

    while (pn) {                                    /* iterate to last node */
        if (strcmp (pn->s, s) == 0) {               /* compare node string with string */
            pn->refcnt += 1;                        /* increment ref count */
            return *ppn;
        }
        ppn = &pn->next;                            /* ppn to address of next */
        pn = pn->next;                              /* advance pointer to next */
    }
    
    return *ppn = create_node (s);                  /* allocate and return node */
}

/** print all nodes in list */
void prn_list (node_t *head)
{
    if (!head) {                                    /* check if list is empty */
        puts ("list-empty");
        return;
    }

    for (node_t *pn = head; pn; pn = pn->next)      /* iterate over each node */
        printf ("%-24s %4zu\n", pn->s, pn->refcnt); /* print string an refcount */
}

/** delete node with string s from list (for loop) */
void del_node (node_t **head, const char *s)
{
    node_t **ppn = head;                            /* address of node */
    node_t *pn = *head;                             /* pointer to node */
    
    for (; pn; ppn = &pn->next, pn = pn->next) {
        if (strcmp (pn->s, s) == 0) {               /* does string match */
            if (pn->refcnt) {                       /* ref count > 0 */
                pn->refcnt -= 1;                    /* decrement ref count */
                return;                             /* done */
            }
            *ppn = pn->next;                        /* set content at address to next */
            free (pn);                              /* free original pointer */
            break;
        }
    }
}

/** delete all nodes in list */
void del_list (node_t *head)
{
    node_t *pn = head;                              /* pointer to iterate */

    while (pn) {                                    /* iterate over each node */
        node_t *victim = pn;                        /* set victim to current */
        pn = pn->next;                              /* advance pointer to next */
        free (victim->s);                           /* free current string */
        free (victim);                              /* free current node */
    }
}

int main (int argc, char **argv) {

    char buf[MAXC];                                 /* read buffer */
    const char *delim = " \t\n.,;?!()";             /* strtok delimiters */
    node_t *list = NULL;    /* pointer to list (must initialize NULL */
    /* use filename provided as 1st argument (stdin by default) */
    FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;

    if (!fp) {  /* validate file open for reading */
        perror ("file open failed");
        return 1;
    }

    while (fgets (buf, MAXC, fp))                   /* read all lines in file */
        /* tokenize line based on delim */
        for (char *p = strtok (buf, delim); p; p = strtok (NULL, delim)) {
            if (ispunct(*p))                        /* if word is punctionation, skip */
                continue;
            add_node (&list, p);                    /* add node or increment refcnt */
        }

    if (fp != stdin)                                /* close file if not stdin */
        fclose (fp);

    puts ("string                   refcnt\n"       /* heading */
          "-------------------------------");
    prn_list (list);                                /* print contents of list */
    del_list (list);                                /* free all list memory */

    return 0;
}

Take a look at the characters included in delim to use with strtok() as well as the use of ispunct() to skip tokens that end up beginning with punctuation (which allows hyphenated words, but skips hyphens used alone as sentence continuations, etc....)

Example Input File

$ cat dat/tr_dec_3_1907.txt
No man is above the law and no man is below it;
nor do we ask any man's permission when we require him to obey it.
Obedience to the law is demanded as a right; not asked as a favor.
(Theodore Roosevelt - December 3, 1907)

Example Use/Output

$ ./bin/lls_string_nosort_refcnt dat/tr_dec_3_1907.txt
string                   refcnt
-------------------------------
No                          0
man                         1
is                          2
above                       0
the                         1
law                         1
and                         0
no                          0
below                       0
it                          1
nor                         0
do                          0
we                          1
ask                         0
any                         0
man's                       0
permission                  0
when                        0
require                     0
him                         0
to                          1
obey                        0
Obedience                   0
demanded                    0
as                          1
a                           1
right                       0
not                         0
asked                       0
favor                       0
Theodore                    0
Roosevelt                   0
December                    0
3                           0
1907                        0

Memory Use/Error Check

In any code you write that dynamically allocates memory, you have 2 responsibilities regarding any block of memory allocated: (1) always preserve a pointer to the starting address for the block of memory so, (2) it can be freed when it is no longer needed.

It is imperative that you use a memory error checking program to ensure you do not attempt to access memory or write beyond/outside the bounds of your allocated block, attempt to read or base a conditional jump on an uninitialized value, and finally, to confirm that you free all the memory you have allocated.

For Linux valgrind is the normal choice. There are similar memory checkers for every platform. They are all simple to use, just run your program through it.

$ valgrind ./bin/lls_string_nosort_refcnt dat/tr_dec_3_1907.txt
==8528== Memcheck, a memory error detector
==8528== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==8528== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==8528== Command: ./bin/lls_string_nosort_refcnt dat/tr_dec_3_1907.txt
==8528==
string                   refcnt
-------------------------------
No                          0
man                         1
is                          2
above                       0
the                         1
law                         1
and                         0
no                          0
below                       0
it                          1
nor                         0
do                          0
we                          1
ask                         0
any                         0
man's                       0
permission                  0
when                        0
require                     0
him                         0
to                          1
obey                        0
Obedience                   0
demanded                    0
as                          1
a                           1
right                       0
not                         0
asked                       0
favor                       0
Theodore                    0
Roosevelt                   0
December                    0
3                           0
1907                        0
==8528==
==8528== HEAP SUMMARY:
==8528==     in use at exit: 0 bytes in 0 blocks
==8528==   total heap usage: 73 allocs, 73 frees, 6,693 bytes allocated
==8528==
==8528== All heap blocks were freed -- no leaks are possible
==8528==
==8528== For counts of detected and suppressed errors, rerun with: -v

Always confirm that you have freed all memory you have allocated and that there are no memory errors.

You already have a solution to your immediate problem, but going forward in your project consider some of the tips above to eliminate multiple traversals of your list, and the portability (and validation) issues surrounding strdup(). Good luck with your coding.

2 Comments

Wow! Thank you very very much for this :) Happy Holidays
You bet. I just remember how much fun learning lists were -- and then having to re-learn it when shown better ways to approach it. Always remember, the best way to learn linked-lists is to pull out an 8.5 x 11 sheet of paper and a pencil and then take your first few nodes and as each node is added, draw a box on the paper representing the node. Then follow though the code and fill in the pointers linking the nodes together as those links are defined. (much better than staring at the screen :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.