Reputation: 741
I want put value in linked list. But I have a problem, becouse I want get this: 123 but I get this 123 123 123 . . to infinity. What I doing wrong ?
#include<stdio.h>
#include<stdlib.h>
struct lista{
int data;
struct lista *next;
struct lista *prev;
};
struct lista *push(struct lista *head, struct lista *x){
x->next = head;
x->prev = NULL;
if(head!=NULL)
head->prev=x;
return x;
}
void show(struct lista *head){
while(head!=NULL){
printf("%d\n", head->data);
}
head = head->next;
}
int main(){
struct lista *head = NULL, *x = NULL;
x = (struct lista*)malloc(sizeof(struct lista));
x->data = 123;
head = push(head, x);
show(head);
return 0;
}
Upvotes: 2
Views: 107
Reputation: 1045
you made slight but sevior mistake of "infinity loop".
Always notice 2 key points in the infinity case :- Point 1. "Condition" on the Control variable (i.e. *head) loop must be proper enough. Point 2. "Updation" on Control variable must be given to reach the end point of loop from the initialization of control var.
okey, now you wouldn't be confuse in this problem to find the solution by self. :)
So, now you're able to troubleshoot and testing of problem.
void show(struct lista *head){
while(head!=NULL){ //1.Condtion on *head look might be proper
printf("%d\n", head->data);
}
head = head->next; //2. update on *head out of loop which gives *infininty*
}
Solution:-
void show(struct lista *head){
while(head!=NULL){
printf("%d\n", head->data);
head = head->next; //now the update work in loop *to break infinity*
}
}
Upvotes: 3
Reputation: 28911
In show(), just move the line to advance head to within the while loop:
void show(struct lista *head){
while(head!=NULL){
printf("%d\n", head->data);
head = head->next; /* moved this line */
} /* usual indentation for } */
}
C function calls pass parameters by value (not by reference), so the show function will not modify the value of head in main() when it calls show().
Shivam Sharma appears to have answered before I did, although my edit may have changed the order of answers. He should get the "check" for the answer.
Upvotes: 0
Reputation: 175
Your increment to the pointer head
is outside the loop so actually head
never changes and so it keeps on printing same value.
Change your method show
to the following:
void show(struct lista *head){
while(head!=NULL){
printf("%d\n", head->data);
head = head->next;
}
}
Now after each printf
statement your pointer head
is moving to the next element of the list and thus it will work as expected.
Upvotes: 0
Reputation: 5009
Change this :
void show(struct lista *head){
while(head!=NULL){
printf("%d\n", head->data);
}
head = head->next;
}
to :
void show(struct lista *head){
lista *temp = head;
while (temp != NULL){
printf("%d\n", temp->data);
temp = temp->next;
}
}
Reason for this modification :
You change head
pointer outside the while
loop, so head
will always be the same. In your case it is not NULL
so it will infinitely be printing the data
in the node that head
currently shows.
Also, it is better to not modify the head
of your list directly, because you will not be able to access it again.
Upvotes: 1
Reputation:
You might need to change the show method the following way and not change the head;
void show(struct lista *head){
lista *temp = head;
while (temp != NULL){
printf("%d\n", temp->data);
temp = temp->next;
}
//head = head->next;
}
Upvotes: 0