1
struct Node{
int data;
struct Node* next;
};
struct Node* head; 
void Insert(int x){
    struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
    temp->data=x;
    temp->next=NULL;
    head=temp;
}
void Print(){
    struct Node* temp=head;
    printf("List is: \n");
    do{
        printf("%d", temp->data);
        temp=temp->next;
    }while(temp!=NULL);
}
int main(){
    head = NULL; 
    printf("How many numbers do you want to put into a list? \n");
    int n, i, x;
    scanf("%d", &n);
    for (i=0; i<n; i++){
        printf("Enter the number: \n");
        scanf("%d", &x);
        Insert(x);
        Print(); 
    }
}  

this code only prints the last element. I've checked it multiple times, and i can't seem to find the mistake. I would really appreciate If anyone could tell me where did it go wrong.

thanks in advance

sstefan
  • 385
  • 4
  • 15
  • 1
    Do not cast the result of `malloc` & friends in C. – too honest for this site Dec 15 '15 at 17:21
  • Because you always make `head` point to the newly created node. The previous nodes are lost. It creates a memory leak also. – Haris Dec 15 '15 at 17:22
  • 1
    Welcome to Stack Overflow! It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Paul R Dec 15 '15 at 17:22
  • @Olaf why should I not cast result of malloc? Doesn't it return a pointer of type void? – sstefan Dec 15 '15 at 17:37
  • 1
    @StefanStipanovic see [Do I cast the result of malloc?](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – lurker Dec 15 '15 at 17:40
  • @StefanStipanovic: See the standard: http://port70.net/~nsz/c/c11/n1570.html#6.3.2.3p1 – too honest for this site Dec 15 '15 at 17:50

2 Answers2

2
head=temp;

This line will overwrite your previous head. That's why you can only print the last element.

In linked list, there are two types of data output : FIFO(First In First Out) and LIFO(Last In First Out).

For example:

Input = 1 2 3 4

FIFO

Output = 1 2 3 4

LIFO

Output = 4 3 2 1

FIFO

You should declare struct Node* tail;

After temp->next=NULL; you should add:

if(head==NULL){
    head=temp;
    tail=temp;
}
else{
    tail->next=temp;
    tail=temp;
}

Do note that the head must not be moved in FIFO. Therefore we need use tail to add other data.

LIFO

After temp->next=NULL; you should add:

temp->next=head;
head=temp;
azz
  • 23
  • 4
1

These two statements:

temp->next=NULL;
head=temp;

Will lose previously inserted data. Use

temp->next=head;
head=temp;

instead. This will put the new element at the head with the rest of the list afterwards.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • Got it, thanks! I meant to put if(temp!=0) temp->next=head that is where it went wrong. But if you write it like you did, it is neater. – sstefan Dec 15 '15 at 17:32