dimanche 7 février 2021

Linked List - Implementation

I'm basically trying to implement LinkedList in c but I still can't get it I will leave my code below so you can see what I'm missing :

#include <stdio.h>
#include <stdlib.h>
struct Node {
    int val;
    struct Node* Next;
};
struct Node* head ;
void insert(int x);
void print();
int main()
{
   head =NULL;
   int d,i=0,f;
   printf("How many number you want to insert:\n");
   scanf("%d",&d);
   for(i;i<d;i++){
       printf("Enter your number:\n");
       scanf("%d",&f);
       insert(f);
       print();
   }
    return 0;
}
void insert(int x){
    struct Node* A =(struct Node*)malloc(sizeof(struct Node));
    if(head ==NULL){
    A->val=x;
    A->Next=head;
    head=A;
    return;
    }
     A->val=x;
     A->Next=NULL;
}
void print(){
    printf("[");
    struct Node* B = head;
    while(B->Next!=NULL)
    {
         B = B->Next;
        printf("%d",B->val);
    }
    printf("]\n");
}

I need some pointers to where I missed up.

Aucun commentaire:

Enregistrer un commentaire