16 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 ...
  • 17.6k
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 ...
  • 7,366
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 ...
  • 66.7k
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 ...
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 ...
  • 7,366
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 ...
  • 66.7k
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 (...
  • 20.8k
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 ...
  • 1,211
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 ...
  • 66.7k
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]...
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(...
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 ...
  • 5,201
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 ...
  • 17.6k
1 vote
Accepted

Pset 5 Speller Linked List Nodes

Maybe there are a couple things that you haven't grasped yet. First, creating a var and initializing a var are two very different actions. A statement like node *pnode; creates a node var called ...
  • 66.7k
1 vote
Accepted

on pset4 speller load() struggling to understand if populating dictionary correctly and how to check hashtable

Start with a smaller dataset (dictionary). Keeping track of 143K+ nodes is a job for a computer, not a human. And remember, the same file can be used as both dictionary and text, so dictionaries/small ...
1 vote
Accepted

Declaring a typedef struct for linked list

The requirement is that you give the struct a name so you can use it in the declaration. Whether or not that is a different name isn't important. Both of those declarations are fine.
  • 18.5k
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 ...
  • 20.8k
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 ...
  • 66.7k
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] ...
  • 20.8k
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 ...
  • 66.7k
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 ...
  • 20.8k
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 ...
  • 66.7k
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?...
  • 66.7k
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) { ...
  • 20.8k
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) { ...
  • 20.8k
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 ...
  • 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 ...
  • 66.7k
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; ...
  • 17.6k
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] | ...
  • 7,366
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 ...
  • 18.5k

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