15 votes
Accepted

How to fix the "conditional jump or move depends on uninitialized value" valgrind error?

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 ...
user avatar
  • 17.4k
6 votes
Accepted

implementation of hash table

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 = ...
user avatar
  • 17.4k
4 votes
Accepted

PSET6: Sorted List?

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 ...
user avatar
  • 17.4k
3 votes
Accepted

Insert data at head node linked list

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 ...
user avatar
  • 7,326
3 votes
Accepted

inserting elements in a linked list (sorted)

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 ...
user avatar
  • 64.2k
2 votes
Accepted

C Linked List not working

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 ...
user avatar
2 votes
Accepted

adding data to singly linked list

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 ...
user avatar
  • 7,326
2 votes
Accepted

Recursion Destroy Singly linked List, memory leak?

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 ...
user avatar
  • 64.2k
2 votes
Accepted

The load function in dictionary.c is segfaulting. 3 tfs have looked over my code and none can figure out what's going on. Can anyone help?

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]...
user avatar
2 votes
Accepted

How does HEAD in linked list work?

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 ...
user avatar
  • 64.2k
2 votes
Accepted

Insert a node at the beginning of an existing linked list

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 (...
user avatar
  • 20.7k
2 votes
Accepted

how to read integers (separated with spaces) from a file line-wise?

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 ...
user avatar
  • 1,211
1 vote
Accepted

Help in linked datastructure

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 ...
user avatar
  • 20.7k
1 vote
Accepted

pset4 Speller hash table basics

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 ...
user avatar
  • 64.2k
1 vote
Accepted

HashTable full of single value

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] ...
user avatar
  • 20.7k
1 vote
Accepted

Help with singly linked list basics

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 ...
user avatar
  • 64.2k
1 vote
Accepted

Two hash table questions

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 ...
user avatar
  • 20.7k
1 vote
Accepted

Struggling with hash table! Pset5

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 ...
user avatar
  • 64.2k
1 vote
Accepted

PSet5 Hash Tables

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?...
user avatar
  • 64.2k
1 vote
Accepted

deleting first element of a linked list

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) { ...
user avatar
  • 20.7k
1 vote
Accepted

Singly Linked list basic

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) { ...
user avatar
  • 20.7k
1 vote

I am working since past 4days on pset5 (speller) .All the words of dictionary are shown as mispelled. Please help me out. Thank You

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 ...
user avatar
  • 1,139
1 vote
Accepted

Having a bit of a problem with linked lists. Need someone to look at code

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 ...
user avatar
  • 64.2k
1 vote

Kinda confused with what needs memory allocated in pset5

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 ...
user avatar
  • 5,172
1 vote
Accepted

Trouble Interpreting Linked List Sample Code - PSET5

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 ...
user avatar
  • 17.4k
1 vote
Accepted

Linked list doesn't print out the correct values (when using fopen)

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(...
user avatar
1 vote
Accepted

inserting a node in the middle of a linked list failed

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; ...
user avatar
  • 17.4k
1 vote

inserting a node in the middle of a linked list failed

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] | ...
user avatar
  • 7,326
1 vote
Accepted

How to add a new node in the linked list

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 ...
user avatar
  • 18.4k
1 vote

Segmentation error with array of nodes

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 ...
user avatar
  • 7,326

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