1

I'm trying to build a singly-linked list using a struct with 2 data types: char* and int, as well as next to point to other nodes of course.

I have two functions: addToList, and printList as well as the main method to run everything.

What my code is supposed to do is add a node after the head, and check to see if another node with the same data has already been added. If so, it does not add the new node, while incrementing the count data of the already-linked node.

Then, printList() prints the count data and the char* data of each node.

The first issue is that my char comparison doesn't seem to be working, since duplicate nodes are still added. Then, my printList function does not print the char* data correctly. Here's my code (I made sure to comment it so it's easy to follow along):

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

// Struct for node. Has an extra data variable to keep track of which node is next
// in a singly-linked list.
typedef struct node {
    char *str;
    unsigned int count;
    struct node *next;
} node;

// Creates a HEAD node with NULL data.
node *head = NULL;

// Adds a new node and populates the data.
struct node* addToList(struct node* Listptr, char* word){
    struct node* new = malloc(sizeof(struct node));
    new->str = malloc(sizeof(char) * 34);

    strcpy(new->str, word);

    new->count = 1;
    new->next = Listptr;

    // If the head is NULL, sets the new node as the head.
    if(head == NULL){
        head = new;
        return new;
    }

    // Sets a node iterator "cur" to the first position.
    node *cur = head;

    // Sets a node iterator to be one place behind cur.
    node *prev;

    // Loops through the linked list, in order to count the previous words and determine
    // if it is necessary to add another node.
    // Otherwise, it sets cur to the next node, and prev to the node cur was just at.
    while(cur != NULL){
        if(cur->str == new->str){
            cur->count++;
            new = NULL;
            return new;
        } else{
            prev = cur;
            cur = cur->next;
        }
    }

    // Checks to see if cur is NULL, if so, sets the previous node.next to the one we're adding.
    if(cur == NULL){
        prev->next = new;
        return new;
    }
}

// Prints out the count and word values of each node.
void printList(){
    node* cur = head;
    while(cur != NULL){
        printf("%d %c\n", cur->count, cur->str);
        cur = cur->next;
    }
}

int main() {

    node* Z = NULL;

    char *a = "hello";
    char *b = "world.";
    char *c = "hello";

    addToList(Z, a);
    addToList(Z, b);
    addToList(Z, c);

    printList(Z);

    return 0;
}

I expect to get:

2 hello
1 world

But in the console, I get:

1 l
1 (weird symbol)
1 
ESM
  • 175
  • 10

1 Answers1

2

Don't use == to compare strings rather use strcmp().

Change if (cur->str == new->str) to this:

if (strcmp(cur->str, new->str) == 0)

Read this to know more on string compare: How do I properly compare strings?

UkFLSUI
  • 5,509
  • 6
  • 32
  • 47