last day (15 days later) » 

7:38 PM
0
Q: Insert node not values in end of linked lists

Abhishek SinghLets say I have some linked lists already available and I want to store first node of each individual list into another list so that I can recall this list to display the original lists. I think we have to use two different structures. I am already successful in retaining original lists and displ...

 
You will need a second list node type, struct list_of_nodes { Node *list; struct list_of_nodes *next; }; and you'll need your second set of list management functions to handle that. Or you have to play some fancy tricks, such as changing struct node so that it holds a void *data; instead of int number;, and then arrange for the 'list of ints' code to work with this, and add the 'list of nodes' code too. Or migrate to C++ and use a templated list type specialized to the different types.
 
@JonathanLeffler I have the same idea but can you apply the same in the above code and show me. I have tried that myself, but didn't quiet work for me. Also inserting node function is available. Please try and let me know. I will also be trying from my end. I can't use C++ sorry.
 
Show us what you tried that didn't work, and we can help you fix it. You've got a list mechanism that you say works (I've not tested it, so I reserve judgement on that). If you're happy with it, you should be able to clone it with edits such that it deals with the currently hypothetical struct list_of_nodes type, and instead of taking an int value, it takes a Node *value. It should just be a systematic edit. Of course, you already have working code to print lists, so your 'list_of_nodes' code will swiftly have working code that prints lists of lists. I understand "cannot use C++".
 
@JonathanLeffler Please debug the code, just copy and paste it into Dev C or codeblocks. You will understand how it works. Next, I cannot use C++ because I need this program for my assignment in C programming. I am trying from my end. You can easily try out, I will rate you up as well. Thanks in advance.
 
I understand how the code you've shown works (or is supposed to work), yes. I don't understand how the code you've not shown doesn't work — primarily because you've not shown it. It seems sufficiently straight-forward that I'm not sure why you're having problems, but that's often the case. Once you show the erroneous code, the problems can be diagnosed efficiently. Until then, I'm not going to write your code for you — I disagree with enough of the structure of what I see that I'd want to rewrite it all, and then it wouldn't be your code after all. […continued…]
[…continuation…] For instance, you don't check that the input operations succeed. If the user types 'a' instead of 1, everything else is going to be working with indeterminate inputs. You repeatedly prompt for a number, and then scan for it. You should have a function that does that; it can handle EOF and data format errors too. And you can use it in a while loop instead of needing the CHECKER: lable.
 
7:38 PM
Ok I used your structure definition. I am having an error.
if(val==0) head->list=lists[listNo]; head=head->next; Error: invalid argument '->'
If I use Nodex *head, then I see output crashing after first input i.e. at val==0. What is Nodex? typedef struct list_of_nodes Nodex;
You need code snippet? Here: dropbox.com/s/ea3byril6xcwizc/news.txt?dl=0
 
Since you've not updated the question with the revised / extra code, I'm still flying blind.
It makes it hard to guess what you're doing wrong.
Oh, I've just seen the snippet message...brain seems to be fuzzy; to much blood in my caffeine stream, I fear.
 
Shall I? The code is in the dropbox link is what I am doing now. Ok I will update it in question as well.
I basically added a structure definition
and new Nodex structure variable
and then trying to store the first node
 
Incidentally, #include <malloc.h> is redundant; <stdlib.h> declares malloc() et al.
 
i.e. at val==0 in for loop
Yes seems so
 
Where's the function for inserting a Node * into a Nodex *?
 
7:46 PM
No function
if(valNo==0){
head->list=lists[listNo];
head=head->next;
}
So for every individual list
first node is stored in the above manner
but it doesn't work as I mentioned the program crashes
 
Oh, I see. So much for symmetry and uniformity...
 
If I can help you some way, please let me know but I need this problem solved.
Do I use insertion in end definition?
I think we need to allocate memory for head->list
So is it right if we say
head->list=malloc(sizeof(Nodex);
before equating it to the first node of individual list?
 
Probably. You should post your updated code into the question so other people can see it too, and explain where you're having problems. I'll look at it, and will probably work out a fix and post something.
 
Ok I will post updated code. One sec
 
You'll need to work out where all the elements in the list of Nodex's are allocated.
Which is why I think you need an insertList() function parallel to the insertValue() function.
 
7:54 PM
crashing even after adding malloc
 
The fundamental things that are different between the two types is that the list of nodes takes a pointer instead of a simple integer value. All the other aspects of list management remain the same. You need to allocate Nodex structures to be included in the list pointed at by head.
I'm intrigued that you chose a recursive printing solution instead of an iterative one. It's not wrong, but eliminating tail recursion into a loop is easy.
 
Ok I can edit and change recursion to iterative method. Not an issue
New display function

void display(Node*nodex){ // display node values in list

while(nodex)
{
printf("%d ->",nodex->number);
nodex=nodex->next;
}
}
 
You could add the 'current list' to the list of lists outside the inner loop, so you wouldn't need the if (valNo == 0) condition. Further, until you write a function to do the insert into the Nodex list, I think we're getting nowhere.
The rewritten display looks about right; somewhere along the line you probably want to add a newline.
 
Ok I will create a new function to add to end of list and send you now. Please wait
 
You'll also need a function to display a list of nodes, which iterates down the Nodex list and calls the other display function for each list in turn.
Waiting...
 
8:04 PM
Adding to end of list example:

struct node
{int data;
struct node *next;
}*head;


void addnode_end(int a)
{ struct node *temp, *prev;
temp= (struct node*) malloc(sizeof(node)) ;
if(temp== NULL)
cout<<"Not enough memory";
else
{ node->data = a;
node->next = NULL;
prev= head;

while(prev->next != NULL)
prev= prev->next;

prev->next= temp;
}
}
If we could just add node to end of list, rest will be easy as cake
But I do not know how to add nodes to end of list using a function
Please try with the code above
Adding number to end of list while creating a new node seems easy
The individual node also has next pointer pointing to its own individual list
 
There are probably a thousand (maybe more) questions about how to add a node to the end of a list. Your insertValue() code contains the key point; you need to return the head of the list. For the rest, you iterate through the list until the next pointer is null; then you add the new node there. Watch for the empty list.
 
Yes I understand
But please try using my code and try implementing it. I need this solved. It will great of you.
I am trying from my end now
also if I use if(val==0) I am only adding a node without next to list. May be.
I will try creating a function now.
I guess it should be like this head=insertNode(node);
 
head = insertNode(head, node); I believe — at least, that's what I'm expecting.
Where the node might be list[i] or &list[i] — I'd need to look to be sure.
 
Should it be *head or head
How is this function definition:
Nodex insertNode(Nodex* head, Node* node){
Node *newNode, *m;
newNode = malloc(sizeof(Nodex));
newNode->list=node;

if(list == NULL)
{
newNode->next=NULL; // inserting first node
return newNode;
}

m = list;
newNode->next = m->next; // inserting new node
m->next = newNode;
return list;

}
 
8:20 PM
I'm assuming: Nodex *head = 0; at the start. Then it is: head = insertNode(head, list[i]);
And I think the Nodex name is not the best choice; maybe ListNode or NodeList would be better.
 
Yes
you are right
I changed the name to ListNode
Now trying to create function
 
Don't forget to error check malloc(); it has a habit of failing when it is least appropriate...
OK; good.
 
How is this function?
ListNode insertNode(ListNode* head, Node* node){
Node *newNode, *m;
newNode = malloc(sizeof(ListNode));
newNode->list=node;

if(newNode == NULL)
{
newNode->next=NULL; // inserting first node
return newNode;
}

m = newNode;
newNode->next = m->next; // inserting new node
m->next = newNode;
return newNode;

}
I think this one is more apt:
Node* insertValue(Node * list, int value) // function to insert node in ordered manner into list
{
Node *newNode, *m;
newNode = malloc(sizeof(Node));
newNode->number=value;

if(list == NULL)
{ListNode insertNode(ListNode* head, Node* node){
Node *newNode, *m;
newNode = malloc(sizeof(ListNode));
newNode->list=node;

if(newNode == NULL)
{
newNode->next=NULL; // inserting first node
return newNode;
}

m = newNode;
while(m->next) // checking for right position in ordered list for new node
{
m = m->next;
 
It needs to return a ListNode *, rather than a ListNode value, doesn't it? Unless you went and put a pointer into the typedef (succinct summary — don't — but see Is it a good idea to typedef pointers.
The second one looks close to what I'd expect.
Keeping three different lots of SO code straight at the same time is hard work.
 
Ok one sec
Just check the final insert node function
 
8:29 PM
When you update the question with the revised code, I'll copy'n'paste and poke at it.
Is it working? Have you run it under the debugger?
Also questions that are valid to ask…
 
No its crashing
 
Often, when I use the debugger, it is only transiently — to find out where the crash occurs. Sometimes I'll make a few runs with it, but I don't often spend protracted sessions looking at the debugger step-by-step — though that does have some merit as a technique.
OK; it crashes — have you identified which line causes the crash?
Is it a null pointer dereferencing crash, or something else?
 
I haven't tried the function yet
 
Do you have valgrind available to you?
 
whats that?
 
8:32 PM
Can you keep up with the rate at which I spew forth comments :D
Valgrind is a tool for debugging memory abuse, especially dynamically allocated memory.
It runs on Linux and other *nix systems, but I'm not sure it runs on Windows.
 
Ok cool. I will try that
But whats issue with codeblocks. Dude I think you are a pro
Please spend a few mins on this you can save lives :D
Code for function:

ListNode insertNode(ListNode* head, Node* node){
Node *newNode, *m;
newNode = malloc(sizeof(ListNode));
newNode->list=node;

if(newNode == NULL)
{
newNode->next=NULL; // inserting first node
return newNode;
}

m = head;
while(m->next) // checking for right position in ordered list for new node
{
m = m->next;
}
newNode->next = m->next; // inserting new node
m->next = newNode;
return head;

}
Ok now its crashing after using the function
I am updating the question
Ok so I have updated the question
and highlighted
where it crashes
By crash I mean codeblocks crash
The cmd crashes and says Looking for solutions
If you know what I mean
Do you provide paid support
I can pay you for this
Not much though
Check the updated question
Is the function definition correct?
 
8:51 PM
OK; I'll take a look now at the updated material. You can send a contribution by Paypal to my email address (see my profile) if you wish, but it isn't essential.
It'd be a first from So.
SO, that is...
 
Yes I will but after my question is resolved by you.
 
When I run with valgrind, it says:

Enter the number of lists (1 to 100):

Enter the number of inputs to the list 1:
==7449== Invalid read of size 8
==7449== at 0x100000C2D: main (ll7.c:93)
==7449== Address 0x8 is not stack'd, malloc'd or (recently) free'd

That looks like you're dereferencing head->next before anything's allocated. I did reformat the code so my line numbers might not be the same as yours. It's in insertNode():

`92 m = head;
93 while (m->next) // checking for right position in ordered list for new node
Drat, I forgot that chat doesn't really support markdown very well..
So, on the first call, you have to simply return the new node as the head if the current head is null.
 
yes
Thats what I am doing
within the same function
if(newNode == NULL)
{
newNode->next=NULL; // inserting first node
return newNode;
}
 
86 if (newNode == NULL)
87 {
88 newNode->next = NULL; // inserting first node
89 return newNode;
90 }
 
9:01 PM
No; that's checking whether the malloc succeeded...and then newNode->next = NULL; in the error return path is a 'guaranteed crash' before you hit the return.
 
ok you mean
if(head == NULL)
{
newNode->next=NULL; // inserting first node
return newNode;
}
 
if (newNode == NULL) return NULL;
And then the if (head == NULL) as you wrote.
 
ok trying to debug now with that
 
Or you can decide to report 'out of memory' and exit if the allocation fails.
 
crashes again
ListNode* insertNode(ListNode* head, Node* node){
ListNode *newNode, *m;
newNode = malloc(sizeof(ListNode));
newNode->list=node;
if (newNode == NULL) return NULL;

if(newNode == NULL)
{
newNode->next=NULL; // inserting first node
return newNode;
}

m = head;
while(m->next) // checking for right position in ordered list for new node
{
m = m->next;
}
newNode->next = m->next; // inserting new node
m->next = newNode;
return head;

}
Oh sorry. 1 sec
 
9:03 PM
Where's the 'if (head == NULL)' test? You need two tests...one for the malloc, which you've got, and one for head...which you left out, as I think you've just spotted.
With both those fixes in (the head check being crucial), it worked for me under valgrind.
Leaking like a sieve since there's nary a free in sight...
I created ll7.c from your code, and ll7.data containing:

3
6 26 22 83 96 89 69
10 87 33 5 36 85 34 0 25 57 99
5 49 44 27 75 82

Then I ran 'valgrind ll7 < ll7.data' and it said:
Enter the number of lists (1 to 100):

Enter the number of inputs to the list 1:
Enter node value 1:Enter node value 2:Enter node value 3:Enter node value 4:Enter node value 5:Enter node value 6:
The list 1 is: 22 ->26 ->69 ->83 ->89 ->96 ->

Enter the number of inputs to the list 2:
Enter node value 1:Enter node value 2:Enter node value 3:Enter node value 4:Enter node value 5:Enter node value 6:Enter node value 7:Enter node value 8:Enter node value 9:Enter node value 10:
The list 2 is: 0 ->5 ->25 ->33 ->34 ->36 ->57 ->85 ->87 ->99 ->
 
I think I am close
 
Because the input came from a file, you don't see the values as they're read (that requires terminal input, and I'm too lazy to input from a terminal.
I generated random numbers between 0 and 99; the second line has both 0 and 99 in it. That's a fluke.
 
I think I am close
I think I got it
 
Yup, you're basically there.

I have:

80 ListNode *insertNode(ListNode *head, Node *node)
81 {
82 ListNode *newNode, *m;
83 newNode = malloc(sizeof(ListNode));
84 newNode->list = node;
85 newNode->next = NULL;
86
87 if (newNode == NULL)
88 {
89 fprintf(stderr, "Out of memory in %s\n", __func__);
90 exit(1);
91 }
92
93 if (head == NULL)
94 return newNode;
95
96 m = head;
97 while (m->next) // checking for right position in ordered list for new node
You might need to fix the comment on line 97.
 
yes
:D
Yes its working
I am voting you up
I am sending you the final code
Can you tell me if its reliable
Code:

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

struct node{
int number;
struct node*next;
};

typedef struct node Node;
Node* insertValue(Node * list, int value);
void display(Node*);

struct list_of_nodes {
Node *list;
struct list_of_nodes *next;
};

typedef struct list_of_nodes ListNode;

ListNode* insertNode(ListNode* head,Node* node);

int main()
{
ListNode *head=NULL;
Node *globalList = NULL, *lists;
int nbrOfLists, listNo, nbrOfVal, valNo, val,i=0,k;

CHECKER:
printf("\n\n Enter the number of lists (1 to 100):");
 
9:17 PM
It's someone else who answered in the question...
Not a problem.
 
9:36 PM
I've added my answer with the working code, sample run, and some sarky comments. I hope that all helped. I think you'll have learned a fair bit while doing the thinking.
As far as it goes, it seems solid. Obviously, you'll need to write some list freeing code, rather carefully. You probably will free the list of nodes in one function, thereby also releasing lists within the list of nodes (another function), and then there's the global list to delete. I did a second test with 60 lists with 10-30 elements with values 0..999 in each list. It survived OK under valgrind (no crash, just leaked memory).
 

last day (15 days later) »