Inserting code at the begenning of the linked list
Adarsh
Posted on July 17, 2022
#include <stdlib.h>
#include <stdio.h>
struct Node
{
int data;
struct Node *next;
};
// struct Node *head;
Node *insertVal(Node *head, int data)
{
Node *temp = (Node *)malloc(sizeof(struct Node));
temp->data = data;
temp->next = head;
head = temp;
return head;
};
void printData(Node *head)
{
struct Node *temp = head;
printf("List is : ");
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
};
int main()
{
Node *head = NULL;
printf("How many numbers ? \n");
int n, x, i;
scanf("%d", &n);
printf("The number that u have entered is %d", n);
for (i = 0; i < n; i++)
{
printf("Enter the data for the list \n");
scanf("%d", &x);
head = insertVal(head, x);
printData(head);
}
return 0;
}
π πͺ π
π©
Adarsh
Posted on July 17, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
privacy Caught in the Crunch My Journey from Snacks to 2 Million Exposed Users Privacy
November 30, 2024