13

The problem is mainly caused by the if statement on line 69 if (temp->word != NULL) { // some code } valgrind is angry because you never really initialized the member word of your struct pointed to by temp, yet you're trying to check whether it has NULL as a value. While the latter might be true, technically there's no guarantee that word will be ...


5

Assuming you're reading the word correctly into new_node -> word and looking at the insertion part of your code if (hashtable[hashvalue]->next == NULL) hashtable[hashvalue]->next = new_node; else new_node->next = new_node; there's a few things that I want to point out here. First, if hashtable[hashvalue] is equal to NULL, there won't ...


4

If we're NOT using a sorted linked-list, then every time we search for a node in that list, we have to traverse through the whole list in the worst case (if the node we're searching for is the last node in the list or it's not in the list at all). The advantage of using a sorted linked-list is that we can quit searching as soon as we hit a larger/smaller ...


3

I think you have a basic misconception. Here is what your linked list should look like: [head] -> [value1, link1] -> [value2, link2] -> ... The head should only be a link (i.e. pointer) to the first node. You correctly coded, that if (head == NULL) create a new node and make start (i.e. head) point to that node. So you would have: [head] -> [...


3

Your first problem is here: for(node* ptr = head ; ptr != NULL ; ptr = ptr->next ) The for loop never executes because head is null when it hits the first time and the code to assign the first element in the list is inside the loop. This means that the for loop will never execute. Once this is fixed, there are other issues. Try testing with 6 ...


2

So the problem relates to your char* oneword variable. You have one chunk of memory for all your char*s and with every loop your fgets function overwrites the old data with the new word. So conceptually what you probably want to do, is on each iteration create a new chunk of memory and copy the data from oneword into that new chunk and then have your node ...


2

When we have a SLI with five values {1, 3, 4, 6, 7}, every node points to the next one (given that it is not a double linked list) an the node containing 7 points to NULL. Correct, even though if you wanted to be more technical, you would say each node's link member, points to the next node, not the node itself, because a node has two members. A value and a ...


2

You have only implemented part of your goal. This code will walk down a linked list to the last element, but will only free memory when it finds that the next element is null, i.e., it is at the end of the list. To free memory as you go (beginning to end), you should create a temp pointer to hold the address of the next element, and then free the current ...


2

TL; DR: Change if (fscanf(input, "%45s", new_node -> word) != EOF) to if (fscanf(input, "%45s", new_node -> word) == EOF) and new_node -> next = hashtable[n] -> next; hashtable[n] -> next = new_node; to new_node -> next = hashtable[n]; hashtable[n] = new_node; Too Many Words: Firstly, it seems the conditions are flipped on ...


2

Your question is unclear, but I'll take a shot at it. In a linked list, HEAD is usually a var name, not a data type. HEAD is usually a pointer or possibly a data structure that serves as the pointer to the first element in the linked list. In your code, node1 serves as a head. However, if you wanted to insert a new node at the beginning of the list, you'd ...


2

head is passed per value, therefore it's a copy of the pointer you try to update. For updating the original value, either return the new head and update global head by assignment, or pass a reference (& in front of variable name in function signature, I would not recommend that), or pass a pointer to the head pointer instead. [edit] One possible ...


2

Use a while loop to keep going through all of the characters (using fread) until end of file, and check if the char is a digit. If it is a digit, append it to your variable to hold the number and go on to the next char. If it is a space, load the previous number into your array or linked list and go on to next char. Same thing for \n.


1

You seem to ignore the value of found. Just disables the duplicate check. You don't have a loop for printing elements, and you seem to use ptr before initialisation. I would expect a loop very similar to that one searching for the element, or that one searching for the last node of the list. Like for (node*ptr=numbers;ptr!=NULL;ptr=ptr->next) ...


1

Without seeing exactly what the new code looks like (certain important aspects may be missing from your description), it's still pretty obvious what happened. In the pointer implementation, a new space in memory is created for each word. The address of that memory is stored in the pointer word, and later, that address is moved into new_node->data with the ...


1

You allocate space for one word. Then you copy its address to all your value pointers. Of course it's all the same word, as you re-use the same space all the time. I used a char value[LENGTH + 1] instead, and strcpy. Or you could malloc a new one each time you fscanf. You have multiple other problems in your code. For example, in your check function, you ...


1

Believe it or not, your problem isn't with linked lists, it's with pointers. Let's see if I can walk you through this. First, every word (line) is being stored in the char array line[100] as its being processed. Now, look at the node structure. char * word; means that word is defined as a pointer to a char (or char string or char array). Now, think about ...


1

Hash tables are basically arrays of pointers, and the hash determines the index to use. Each of those pointers points to the first node of a linked list (or similar). A node of a linked list consists of data (the string) and a pointer to the next element. By convention, a NULL pointer means end of list. So you loop until you find the last element of the ...


1

new->word = dWord; I'd say that the address of dWord is being copied into the pointer(s) new->word for every node created, and not the actual word. When every word is processed, it is written into dWord and every node points to it. So, when the last word is processed, every node "contains" the last word. Instead of using a pointer for word in the ...


1

It's always the simple ones that hide in plain sight. ;-) Look very carefully at this: Node* temp = malloc(sizeof(Node*)); The code allocates memory space that is sizeof what? A node or a pointer? As a side note, why does the code increment i inside the for loop and as the index of the for loop? If this answers your question, please click on the check ...


1

Deleting a node from a linked list requires changing the pointer to this node. So it might be useful to have a pointer to the head pointer, like void del(struct node **head_ptr, int data) { while (*head_ptr != NULL && (*head_ptr) -> data != data) { head_ptr = &((*head_ptr) -> next); } if (*head_ptr != NULL) { ...


1

You assign to temp, which is a local variable, the node never ends up in the linked list. I'd use pointers to pointers all the time, for example like void create(struct node **head, int data) { struct node **temp = head; while (*temp != NULL) { temp = &(*temp) -> next; } (*temp) = (struct node*)malloc(sizeof(struct node));...


1

fscanf(file , "%s" , word) By design, fscanf automatically put '\0' in the end. So if it reads cat\n, the result is cat\0. By doing this: word[strlen(word) - 1] ='\0' you overwrite the last char of the word. So cat\0 transforms to ca\0\0. --Updated after comments--- Yes, there is another mistake in load(). When you add new word and try to insert new ...


1

The problem is that you are trying to make assignments that are dependent on runtime values outside of main or any other function. This is not allowed. Global vars can be declared outside of main, and certain elementary assignments like assigning constants like NULL or constants. The thing these all have in common is that these actions are all things that ...


1

well, no where to start, first see the hash tablenode* hashtable[ALPHA], is an array of pointers to node, are pointers we will use to store our words according to their index hash, to equal hash index equal pointer, for these pointers do not need malloc, your loop to check does not work, possibly segmentation fault. hashtable[k] == NULL, it is unnecessary ...


1

first, I wanna make it clear that there is a difference between the address of a pointer and the address stored in a pointer (aka the address that the pointer points to). pointers are special type of variables that can store memory addresses. where does a pointer store an address? in a memory location somewhere that also has an address. int i = 10; // ...


1

I think the issue is that all of your nodes' word elements are pointing to the same chunk of memory. Let's take a look. In main() you ask for a new pointer with this line: char* word = malloc(sizeof(char) * 45); Then, each time you execute the fscanf() in your while loop, you go to the memory that word points to and update it with the next string from ...


1

although this could have been accomplished in a much more elegant way, I think the problem is in this part if( ptr->n <=i && ptr2->n >= i ) { new_node->next = ptr2; ptr2 = new_node; return; } so here you're trying to insert new_node between ptr and ptr2. what you're doing is setting new_node->next to ptr2 which is ...


1

First of all let's clear something. head and last should be just pointers to nodes, not nodes themselves. So your linked list should look like that: [head] | v [value1, link1] | v [value2, link2] | v [valuen, linkn] <- [tail] If you want to add to the beginning of ...


1

Here's how it works. Your hashtable is an array of node*s (an array of pointers to nodes). Let's say that hashtable[1] is pointing to a node containing the word "apple" at address 0x1001. You have just created a new node for the word "apples" (at address 0x1002) and it hashes to the same bucket (1). So you need to add that node to the linked list. The ...


1

What do you mean if I don't set it to NULL then the head of the list will always keep the value of zero. Yes the reason is because first malloc assigns the memory you request it, it returns the pointer to it, and you assign it to hashTable. Then you assign NULL to hashTable losing effectively the memory you allocated in the previous line, and then ...


Only top voted, non community-wiki answers of a minimum length are eligible