-
May 11th, 2013, 11:55 AM
#1
Linked List problem
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int data;
struct Node *next;
}node;
void insert(node **ptrtohead, int val)
{
node *temp=(node*)malloc(sizeof(node));
*ptrtohead=temp;
temp->data=val;
temp->next=NULL;
if(*ptrtohead != NULL)
{
temp->next=*ptrtohead;
*ptrtohead=temp;
}
}
void display(node *start)
{
while(start!=NULL)
{
printf("%d ",start->data);
start= start->next;
}
}
int main(int argc, char *argv[])
{
node *head=NULL;
insert(&head,10);
insert(&head,20);
display(head);
system("PAUSE");
return 0;
}
I don't know where is the problem, the output is infinite number of 10 !!!! Help is very much appreciated.
-
May 11th, 2013, 12:08 PM
#2
Re: Linked List problem
For starters, you need to indent your code properly, e.g.,
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int data;
struct Node *next;
} node;
void insert(node **ptrtohead, int val)
{
node *temp = (node*)malloc(sizeof(node));
*ptrtohead = temp;
temp->data = val;
temp->next = NULL;
if (*ptrtohead != NULL)
{
temp->next = *ptrtohead;
*ptrtohead = temp;
}
}
void display(node *start)
{
while (start != NULL)
{
printf("%d ", start->data);
start = start->next;
}
}
int main(int argc, char *argv[])
{
node *head = NULL;
insert(&head, 10);
insert(&head, 20);
display(head);
system("PAUSE");
return 0;
}
It looks like your insertion always overwrites the head instead of inserting at the tail because your while loop to find the tail happens after you assign to the head. Besides, I'm not sure that changing the head is the right thing to do in that loop.
Oh, and you should free what you malloc.
-
May 11th, 2013, 12:11 PM
#3
Re: Linked List problem
What debugging of the code have you done? Have walked through the code drawing diagrams of the nodes and the links to see where it deviates from your design?
Note that in insert
Code:
if(*ptrtohead != NULL)
*ptrtohead here will never be NULL as you have assigned temp to it a few statements back!
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
May 11th, 2013, 12:46 PM
#4
Re: Linked List problem
Originally Posted by 2kaud
What debugging of the code have you done? Have walked through the code drawing diagrams of the nodes and the links to see where it deviates from your design?
Note that in insert
Code:
if(*ptrtohead != NULL)
*ptrtohead here will never be NULL as you have assigned temp to it a few statements back!
You don't seem to understand a simple Linked List program, before inserting the 1st node head is always null because (*ptrtohead)=&head=null, and after insertion it will have the address of the 1st node, then insertion will be done from the front, which is what this code does -> if(*ptrtohead != NULL). And who says I'm a starter? Indention is useless for practicing, if you are not sure about what you are saying then don't comment.
Last edited by dotnet13; May 11th, 2013 at 12:48 PM.
-
May 11th, 2013, 01:05 PM
#5
Re: Linked List problem
Originally Posted by dotnet13
You don't seem to understand a simple Linked List program, before inserting the 1st node head is always null because (*ptrtohead)=&head=null, and after insertion it will have the address of the 1st node, then insertion will be done from the front, which is what this code does -> if(*ptrtohead != NULL).
You quoted 2kaud, but 2kaud's observation is pretty much the same as mine: your bug is right there concerning the insertion. I assumed that your intention was to insert at the tail, but either way your code is still wrong because you are overwriting the head even when there is already an existing node. If you want to insert at the head, then you must first get the new node's next pointer to point to the current head before making this new node the head.
The code that you point out does not do what you say it does: it checks if *ptrtohead is not a null pointer, but as 2kaud noted, you already assigned the return value of malloc to a pointer that is then assigned to *ptrtohead, hence *ptrtohead will surely not be a null pointer unless malloc fails.
Oh yeah: don't accuse others of not understanding a simple linked list program when you are asking for help concerning a simple linked list program. Having seen 2kaud's other posts, I am reasonably certain that he/she does understand linked lists.
Originally Posted by dotnet13
And who says I'm a starter?
I'm saying that "for starters", i.e., before you do anything else. I believe the phrase refers to the "starters" of a meal, i.e., the course that you eat before anything else. That said, it does appear that you are a "starter", as in a beginner, but that's fine: everyone was a beginner at some point. There is no shame in that.
Originally Posted by dotnet13
Indention is useless for practicing,
Sorry, but you are wrong: indentation is very useful, even essential, for visually tracing the flow of control through your code. Hence, you should always indent your code properly, whether it is just practice code or something that you will submit for an assignment.
Originally Posted by dotnet13
if you are not sure about what you are saying then don't comment.
Programming is my profession, and I got there not only through my own experience, but also by keeping an open mind concerning the expertise of others who have trod the path before me. Free feel to believe that I am not sure about what I am saying. You can then remain a newbie forever... or at least a much longer time than if you would listen to good advice.
Last edited by laserlight; May 11th, 2013 at 01:16 PM.
-
May 11th, 2013, 02:53 PM
#6
Re: Linked List problem
Originally Posted by dotnet13
I don't know where is the problem, the output is infinite number of 10 !!!! Help is very much appreciated.
You post some code that doesn't work, you state you don't know where is the problem and you ask for help. Yet when experienced programmers such as laserlight and myself provide specific guidance regarding your problem all you do is insult by saying that we don't know what we're talking about. I don't know laserlight's history but I have a Computer Science degree and have been programming for over 30 years (including teaching c/c++) - so yes, like laserlight, I do know what I'm saying.
I could have provided the correct code for your simplistic single-link head insert function rather than giving you guidance as to where is your problem. However, you learn more by debugging your own code. Becoming a good programmer involves more than just knowing the syntax of a particular programming language. Debugging code is one such skill that needs to be learnt - together with listening and learning from more experienced people.
If you keep displaying the attitude you have demonstrated here, you will remain a 'newbie' programmer. You need to be able to take advice, learn and advance.
I endorse all that laserlight said in post #5 and am disappointed and saddened by your unwarranted comments. With this attitude, how much future help do you think will be provided to you?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
May 11th, 2013, 07:42 PM
#7
Re: Linked List problem
I'm going to chime in and say that I agree with 2kaud and laserlight's assessment of where the bug likely lies with your code. Getting angry at them and claiming they don't understand what a linked list is does nothing but come off as rude. Why should they/we help someone who is unappreciative of advice giving. You posted on these forums. You asked for help regarding code which isn't working. Obviously that means there's some bug with your code. Why get defensive when they point you to the line where the bug is likely to be?
If you have just started programming, learning to use the debugger is a must. It lets you step through the code and you can see where the program starts to deviate from the behavior you expect.
-
May 11th, 2013, 09:19 PM
#8
Re: Linked List problem
Originally Posted by dotnet13
Indention is useless for practicing,
You'd be amazed at the number of problems posted here that become immediately obvious when the code is formatted properly.
If you want help, insulting people with way more experience than you isn't a good way to go about getting it.
-
May 11th, 2013, 10:11 PM
#9
Re: Linked List problem
Originally Posted by dotnet13
You don't seem to understand a simple Linked List program, before inserting the 1st node head is always null because (*ptrtohead)=&head=null, and after insertion it will have the address of the 1st node, then insertion will be done from the front, which is what this code does -> if(*ptrtohead != NULL).
So why doesn't your code work? Why isn't the compiler listening to you? Are you going to insult the compiler for not following your orders?
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|