Linked List Append Implementation In C updating all node values - c

As part of an assignment that is now past due, I was to read a list of search terms from a text file and store these in memory for searching. I decided to use a linked list to store the terms and my implemenation of struct node (stored in myheader.h) looks like this:
struct Node{
char * term;
int termLength;
struct Node *next;};
To save the rootNode as the head of my list, I had a separate function for creating it called startList that is defined as such:
struct Node * startList(char * sterm){
struct Node * rootNode;
rootNode=(struct Node *)malloc(sizeof(struct Node));
assert(rootNode != NULL);
memset(rootNode,0,sizeof(struct Node));
rootNode->term=sterm;
rootNode->termLength = strlen(sterm);
rootNode->next=NULL;
return rootNode;
}
This seems to work fine, the trouble arises when I try to add a new node onto this rootNode, which is supposed to be done with this function:
void insert_another_node( struct Node * headNode, char * sterm){
struct Node * newNode = (struct Node *) malloc(sizeof(struct Node));
newNode->term=sterm;
newNode->next=NULL;
newNode->termLength=strlen(sterm);
while (headNode->next != NULL){
headNode=headNode->next;}
headNode->next=newNode;
}
These functions are all called in this for loop:
while ((fgets(search_wrd,41,list)) != NULL){
strtok(search_wrd, "\n");
if (count==0){
rootNode=startList(search_wrd);}
else{
insert_another_node(rootNode,search_wrd);}
count++;
}
fclose(list);
}
Say I am trying to store a list of planets in this list, the last planet being Neptune. The insert_another_node function will update the terms stored in ALL of the nodes to the most recent term (including the rootNode). The result is the right number of nodes, but they all store "Neptune" at someNode->term.
All of the insert to the end of a linked list implementations I've seen for a linked list in c follow my logic, so I can't understand how this weird update is happening let alone a way to fix it. Any help would be greatly appreciated!

You are just assigning sterm each time, all the assignments point to the same original buffer. You need to make a copy each time.
Use strdup like this:
rootNode->term=strdup(sterm)
and
newNode->term= strdup(sterm);

You need to allocate new memory for each sterm. If you reuse the same memory location they will all have the same value and if you change one you will change them all (because they are the same).

Related

Freeing dynamically allocated graph nodes in C

I want to build a graph that creates a new parent node by merging two child nodes. The code below is supposed to merge node a and b into a parent node c. Then, nodes a and c to create a parent node d:
a b
|---|
|
a c
|---|
|
d
When I try to free the graph starting at node d I get a segmentation fault and I don't know why. Somehow it works if I don't use the same node twice in the graph. However, I want to be able to use the same node more than once. Can someone please tell me what am I missing here?
#include <stdlib.h>
struct Node {
int data;
struct Node *child1;
struct Node *child2;
};
struct Node *NewNode(double data) {
struct Node *node = NULL;
node = malloc(sizeof(*node));
if (node == NULL) {
return node;
}
node->data = data;
node->child1 = NULL;
node->child2 = NULL;
return node;
}
struct Node* merge(struct Node *self, struct Node *other) {
struct Node *node = NewNode(-1);
node->child1 = self;
node->child2 = other;
return node;
}
void free_graph(struct Node **node) {
if (*node != NULL) {
free_graph(&(*node)->child1);
free_graph(&(*node)->child2);
free(*node);
*node = NULL;
}
}
int main(void){
struct Node *a = NewNode(1);
struct Node *b = NewNode(2);
struct Node *c = merge(a, b);
struct Node *d = merge(a, c);
free_graph(&d);
}
It does not work because your "tree" does not match your illustration, and is in fact technically not a tree. What you have looks like this:
You need to make a copy instead of reusing a node if you want a tree.
In order to free everything in a graph like this, I'd suggest having a separate linked list to keep track of everything you need to free.
If you don't want to do that, or cannot do that for some reason, it gets more complicated. Performing an operation an all nodes in a tree is trivial, but for a general directed graph it's slightly more complicated. I guess this answer could help, and if not, it at least gives you an idea about what to search for:
Finding list of all nodes in a directed graph
I assume you could do something like this pseudo:
getAllNodes(root, nodes)
if root // NULL check
if not node in nodes // If it's the first time we visit the node
// Add this node to the list of visited nodes
nodes = nodes + [root]
// And then call this function recursively on the children
getAllNodes(root->left, nodes)
getAlLNodes(root->right, nodes)
nodes = []
getAllNodes(root, nodes)
for node in nodes
free(node)
Trees have the nice feature that they never contain loops. But directed graphs do, so you have to have some check to see if a node is already visited. Note that in order for this to work, it has to be called from the root. Or to be more precise, every node needs to be reachable from the node. But that's not so different from a tree.
I guess you could somehow move the free inside to create a freeAllNodes() function, but this is more flexible. Maybe you want a list for other purposes. So my suggestion in that case is to just make freeAllNodes() call getAllNodes().
I could write an implementation for the above, but since C does not provide library functions for linked lists, that would mean including a lot of extra code.
You put a into the intended tree twice, so free_graph attempts to free it twice. Calling free twice on the same address from the same original allocation is improper.
If you want to have a true tree, do not put any node into it twice. If you want to have a data structure that can have the same node in it twice, either use separate copies of the node (e.g., two different allocations for struct Node with the same value for data) or make provisions in the data structure to avoid freeing it twice (for example, add a reference count to struct node to count how many times it is currently in the tree, and free the node only when its reference count reaches zero).

How to print linked list node top to bottom

I am trying to make linked list. I can add node one by one but I could not print the linked list as i wanted. How to print linked list node from top to bottom
#include<stdio.h>
#include<stdlib.h>
struct node{
int N;
struct node *next;
};
struct node* newNode(int number, struct node *next) {
struct node *new = malloc(sizeof(*new));
new->N = number;
new->next = next;
return new;
}
void show(struct node *head){
struct node *c;
c = head;
while (c!=NULL){
printf("%d\n",c->N);
c = c->next;
}
}
int main (void ) {
struct node *head = NULL;
head = newNode(10, head);
head = newNode(20, head);
head = newNode(30, head);
head = newNode(40, head);
show(head);
return 0;
}
Output
40
30
20
10
I am trying to print node like below
10
20
30
40
How to get above output ?
Since I understand this is probably part of an excercise, I will attempt to answer it in the style of assistance, while still giving a comprehensive answer.
I let aside the fact that you insert your elements in the head - which I am not sure it is what you want to do, and I consider the question as "How to print it backwards, once I have entered the elements correctly?".
We have to examine possible solutions on this:
1) Create a method void addToTail(Node* head, int value); That traverses the list and adds elements to the tail of the list instead of the head. Side note: this operation is time costly, as it requires O(N) time complexity. About complexities, read more here. Se also this StackOverflow question.
2) You mention the term "linked list". By what you say, you do not specify if it is a singly linked, or a doubly linked. And since you have access to the node implementation, I suggest that you add a pointer to each node that points to the previous element, thus converting your singly-linked list to a doubly-linked one.
struct node{
int N;
struct node *next;
struct node *prev;
};
And, of course, you need to update this node respectively in the operations of your list - otherwise it will not work - I let this to you.
That way, you will be able to easily iterate the list backwards then in order to print the number in the order desired.
3) You could implement a function Node * reverseList(Node* head); that "reverses" a list via iteration, and then use it to print the list reversed.
And again, I let the implementation to you. Of, course, you need to consider the list state on each time, as also if you need to reverse the list in-place or return a pointer to a new, reversed list (as the function contract above indicates).
What you need to do now, is re-read your excercise brief, stop for a moment and think: "Do I really need these solutions? Is this what is requested from me?".
If you are just entering the data in the wrong order, probably not.
But if it is specifically required from you to print the list elements backwards, then you have some good hints on how to proceed.
Recursive approach works great here:
void show(struct node *c){
if (c == NULL)
return;
show(c->next);
printf("%d\n", c->N);
}

Creating a link in linked lists in C

Here is the code to create a link in the list which i read off the internet to try understand linked lists in c:
//insert link at first location
void insertFirst(int key, int data) {
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;
//point it to old first node
link->next = head;
//point first to new first node
head = link;
}
I am really not understanding how the following line works as a whole:
struct node *link = (struct node*) malloc(sizeof(struct node));
and more specifically:
(struct node*)
because my understanding is that the asterisk must come before the pointer name and yet its at the end of the struct name. Please correct me if I'm wrong and please if you can, explain how this works?
Casting the return value of malloc is unnecessary.
As per standard 7.22.3.4
The malloc function returns either a null pointer or a pointer to the
allocated space.
You should check its return value to know whether it succeeded or not.
What malloc does?
The malloc function allocates space for an object whose size is specified by size and whose value is indeterminate.
my understanding is that the asterisk must come before the pointer name and yet its at the end of the struct name
Here you are not dereferencing anything. Rather you are typecasting. (which is again I would repeat that unnecessary).
struct node* here is a type of the pointer variable. Same way int or double is a type, this is also a type.
Assume you have to create database of a students having some entities.
struct node
{
int id;
char name [100];
};
Next thing,
struct node *link = (struct node*) malloc(sizeof(struct node));
How above line works ? you need to create one node or memory, So how will you create it, use malloc(). Next things how much memory you are going to create , equal to total size of your data members of structure, so for that its using
struct node *link = malloc(sizeof(struct node));
here link is nothing but name of dynamic memory we created to put some data into that. once memory is created put some data into memory.
scanf("%d %s\n",&link->id,link->name);
similarly you can do above task no of times.
typecasting is not compulsory or not advised to do. So below statement is correct.
struct node *link = malloc(sizeof(struct node));
In order to create a linked list you have to allocate the units -in your case :
struct node
so either you pre-allocate the memory for the list or dynamically allocate them every time you add a node to the list, the memoery for the node has to be recruited from somewhere, in this case - malloc does that.
Hope that helps

List of a List in C

I am trying to dynamically allocate list of pointers to pointers of structs which each contain a list of structs with the following definition:
struct node {
char *text;
struct node *next; };
I am also using a typedef:
typedef struct node Node;
I am creating a list of nodes to hold each list by declaring the following:
Node **list;
list = (struct node**) malloc(sizeof(struct node*) * arraySize);
At each of the nodes of list I will have another list which I am creating as follows:
list[i] = malloc(sizeof(struct node*) * subArraySize);
I have several questions:
Is this the correct implementation for such a problem?
How can I access the text data member of the first node of each list within the larger list?
If my question is vague please let me know. My confusion on this topic makes it difficult to word my question properly.
Assuming your memory is allocated and filled up correctly, you'd access the text data member of the first node of each node* array within the larger node** array via:
list[i]->text
But, to answer your question about the implementation being correct, it's not really. You'd probably be happier doing this:
struct node **list;
list = (struct node**) calloc(sizeof(struct node*) * arraySize);
for (int i=0; i<arraySize; ++i) {
list[i] = (struct node*) malloc(sizeof(struct node));
list[i]->text = ""; // or whatever you want to start with
list[i]->next = NULL;
}
This creates a node** array (called list, since you wanted to call it that, but I would call it something else indicates it's an array of node-based linked lists), then instantiates a block of memory for each head node so that list is full of valid node*s and you can start working with them.
When you go to append something to each of your linked lists, you'd do something like this (this appends to the end, it's up to you to implement insertion based on comparing values if you want sorted data, etc.):
int i = index_where_you_want_to_insert;
struct node *currNode = list[i];
struct node *newNode = (struct node*) malloc(sizeof(struct node));
newNode->text = ""; // or whatever you want to start with
newNode->next = NULL;
while(currNode->next != NULL) {
currNode = currNode->next;
}
currNode->next = newNode;
It may also help you to see this question about using typedef with linked lists.
If I've made any syntactic or semantic errors in my code, I apologize, it's been a while since I wrote C and I may be rusty with how to refer to structs. I welcome corrections.

More than 1 linked list - Adding elements

I need to make a program that has (at most) 50 linked lists. Basically, my program generates some messages and based on a indicator that comes in the front of my string, I need to put the message in the right linked list.
I don't know if it is clear enough but I will try to show part of my code (the important part). The function I made to add a new element (on the top) of the linked list is the following:
void InsertLL (News p, char M[]) {
char * text = malloc(strlen(M)+1);
strcpy(text, M);
News s,t;
t = malloc(sizeof(struct List));
t-> Text = text;
s = p;
p = t;
p-> next = s;
}
My struct List (the type of the elements of my lists) contains a char pointer (called text) and a pointer to the next element of the list.
Simulating my program, suppose that I received a message that needs to be put in the linked list where the begin is pointed by the pointer p[0]. So I create a new element (forget the case that the list is empty, I already made this one) and add in the top of my list using the function I've shown.
Now, suppose that I received another message that needs to be put in the next pointer p[1]. If I print p[0] -> Text, I get the text of p[1]->Text.
I mean, if I add a new element in the list pointed by p[i], all the previous texts p[i] -> Texts gets the new text of this new element. I have no idea what am I doing wrong.
I don't know if it is enough to help me, if more information is needed, just tell me.
Problem with your code is you are not maintaining the list of nodes. you are overwriting the same node again and again.
Algo:
If no node then create one.
If nodes are present then navigate to end node.(You can use tail
pointer for quicker access).
Append the node at the end.
Creating Node:
Declare a node type pointer.
Allocate memory to it.
Update the content of the node.
Set the next pointer of the node to null.
Add this node to end of the list.
ex. inserting node 3 in the list of node 1 and node 2.
This is the general approach that you can use
typedef struct node{
int val; //you can use your text here
struct node* next;
}NODE;
struct node* head=0;
int addNode(int v){
if(head==0){ //checking for empty node.
struct node* n=malloc(sizeof(NODE));
n->val=v;
head=n;
}
else{
struct node* temp=head;
while(temp->next != 0) //Navigating till end
{
temp=temp->next;
}
struct node* n=malloc(sizeof(NODE)); //allocating memory
n->val=v; //you need to use strcpy for string here.
temp->next=n; //adjusting pointers
n->next=0;
}
}
You can check this demo created by me for Double linked List http://ideone.com/s6TtUX

Resources