A linked list of integers is built using the following struct: struct node { int
ID: 3607623 • Letter: A
Question
A linked list of integers is built using the following struct:
struct node {
int data;
struct node *next;
};
Write the definition of a function with the following prototype:
void add(struct node* a[], int index, int value);
The first argument is an array of lists (i.e., an array of struct node pointers, each of which points to the head of a list). The second argument is an index into the array, and the third argument is a value to be added. This function inserts the specified value at the head of the indexed list. There is no return value.
Example: add(a, 3, 10) prepends a node containing the integer 10 to the list stored in element 3 of array a.
All I need is the function.
Explanation / Answer
Please find below implementaion of linked list.
// A linked list node
struct node
{
int data;
struct node *next;
};
void add(struct node* a[], int index, int value)
{
if(index < 0)
return;
// creating new node
struct *newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = value;
// adding new node at head
newNode->next = a[0];
a[0] = newNode;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.