LAb 2 / Exercise 2

PHOTO EMBED

Wed Oct 25 2023 18:35:05 GMT+0000 (Coordinated Universal Time)

Saved by @Mohamedshariif #java

//İnsertion at beginning of linklist.

struct Node
{
    int data;
    struct Node* next;
};

struct Node* head;

void insert (int x)
{
    Node* temp = (Node*)malloc(sizeof(struct Node));
    temp -> data = x;
    temp -> next = head;
    head = temp;
}

void print()
{
    struct Node* temp = head;
    printf("List is: ");
    while(temp != NULL)
    {
        printf("%d ", temp -> data);
        temp = temp -> next;
    }
    printf("\n");
}
 
int main() {
    
    head = NULL;
    printf("How many numbers? \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();
    }
    return 0;
}
content_copyCOPY