I am trying to print the values stored in a linked list but i'm running into a infinite loop.Please could some one tell me what is wrong with my code. I am able to successfully collect the data of the nodes but when printing the list i run into a continuous loop. Any help would be greatly appreciated. Thanks in advance
#include <stdio.h>
#include <stdlib.h>
struct node {
int data; //4 bytes
struct node *next; // 4bytes 32 bit and 8 bytes i 64bit cp
};
int main(){
struct node *head,*newnode,*temp;
head = NULL ;
temp = head;
int count=0, choice=1;
while(choice){
newnode = (struct node *)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&newnode-> data);
newnode->next = head;
if(head == 0){
head = temp = newnode;
}
else{
temp->next = newnode;
temp = newnode;
}
printf(" %d ",temp->data);
printf("Do you want to continue? 0/1");
scanf("%d",&choice);
}
int i = 0;
temp = head;
while( temp!=NULL){
printf("%d ",temp->data);
temp=temp->next;
}
}
CodePudding user response:
When you create a new node, you set it's "next" node to the head node. That's how the loop is made. Set it to NULL.
And afterwards you set the new node as the "current node's next node" and the "current node" as well. I believe you wanted to set it to temp's next node and then move the temp onto it's next node.
Also please don't compare head with 0... If you expect it to be NULL compare to NULL.
CodePudding user response:
I doubt it's within the scope of the homework problem in question, but it's really helpful to break linked list tasks down into smaller problems.
We start with this node definition:
struct node {
int data; //4 bytes
struct node *next; // 4bytes 32 bit and 8 bytes i 64bit cp
};
Which I'm going to typedef to make my life slightly easier.
typedef struct node {
int data; //4 bytes
struct node *next; // 4bytes 32 bit and 8 bytes i 64bit cp
} node_t;
Let's find the last node for a given head. We'll create a node_t
pointer called current
and use it to walk the list until it's at the last node. We'll know it's the last because its next
member will be NULL
. Of course, if head
is NULL
, then we'll just return NULL
immediately.
node_t *last_node(node_t *head) {
if (head == NULL) {
return NULL;
}
node_t *current;
for (current = head; current->next != NULL; current = current->next);
return current;
}
Now, let's add a value to a list with a given head. We can provide a shortcut by returning a pointer the new last node. We'll also short-circuit a lot of work if head
is NULL
.
Otherwise we'll get the last node using the last_node
function we defined, set its next
to the new node, and return a pointer to the new node.
node_t *add_to_list(node_t *head, int value) {
node_t * new_node = malloc(sizeof(node_t));
new_node->data = value;
new_node->next = NULL;
if (head == NULL) {
return new_node;
}
node_t *last = last_node(head);
last->next = new_node;
return new_node;
}
And finally we can write a function to print the list. Given that you've already seen walking the list, this should look pretty familiar.
void print_list(node_t *head) {
for (node_t *current = head;
current->next != NULL;
current = current->next) {
printf("%d ", current->data);
}
}
Breaking down big problems into smaller problems is crucial. Practice it!