13. Given the following structure struct node { struct node
*next; int id; };
Write a code to insert a new node into the linked list as the last
node of the linked list
struct node *insertLast(stuct node **head, int newId)
{
}
14. Given the following structure struct node { struct node *next;
int id; };
Write a code to insert a new node into the linked list after a node
with the same id as the input parameter id
struct node *insert(stuct node **head, int id, int newId) {}
15. Given the following structure struct node {
struct node *next;
int id;
};
Write a code to delete the first node from the linked list. Copy
the id from the deleted node to the parameter id
struct node *delete(stuct node **head, int *id)
{
}
16. Given the following structure
struct node {
struct node *next;
int id;
};
Write a code to delete a node from the linked list that has same id
as the input parameter id
struct node *deleteById(stuct node **head, int id) { }
17. Given the following structure
struct node {
struct node *next;
int id;
};
Write a code to delete the last node from the linked list. Copy the
id from the deleted node to the parameter id
struct node *deleteLast(stuct node **head, int *id) {}
13. Given the following structure struct node { struct node *next; int id; };
Write a code to insert a new node into the linked list as the last node of the linked list
struct node *insertLast(struct node **head, int newId)
{
struct node *temp = *head;
//create a new node
struct node *newnode = (struct
node*)malloc(sizeof(struct node));
newnode->id = newId;
newnode->next = NULL;
//check is linked list empty
if(*head==NULL)
{
*head = newnode;
return *head;
}
while(temp->next!=NULL)
{
temp = temp->next;
}
temp->next = newnode;
return *head;
}
14. Given the following structure struct node { struct node *next;
int id; };
Write a code to insert a new node into the linked list after a node with the same id as the input parameter id
struct node *insert(struct node **head, int id, int newId)
{
struct node *temp = *head;
//create a new node
struct node *newnode = (struct
node*)malloc(sizeof(struct node));
newnode->id = newId;
newnode->next = NULL;
//check is linked list empty
if(*head==NULL)
{
*head = newnode;
return *head;
}
while(temp->id!=id)
{
temp = temp->next;
}
struct node *nextnode = temp->next;
temp->next = newnode;
newnode->next = nextnode;
return *head;
}
15. Given the following structure struct node {
struct node *next;
int id;
};
Write a code to delete the first node from the linked list. Copy
the id from the deleted node to the parameter id
struct node *delete(struct node **head, int *id)
{
struct node *temp = *head;
*head = temp->next;
temp->next = NULL;
*id = temp->id;
//deallocate memory
free(temp);
return *head;
}
16. Given the following structure
struct node {
struct node *next;
int id;
};
Write a code to delete a node from the linked list that has same id as the input parameter id
struct node *deleteById(struct node **head, int id)
{
struct node *temp = *head;
struct node *ptemp = NULL;
while(temp->id!=id)
{
ptemp = temp;
temp = temp->next;
}
ptemp->next = temp->next;
temp->next = NULL;
*id = temp->id;
//deallocate memory
free(temp);
return *head;
}
17. Given the following structure
struct node {
struct node *next;
int id;
};
Write a code to delete the last node from the linked list. Copy
the id from the deleted node to the parameter id
struct node *deleteLast(struct node **head, int *id)
{
struct node *temp = *head;
struct node *ptemp = NULL;
while(temp->next!=NULL)
{
ptemp = temp;
temp = temp->next;
}
ptemp->next = NULL;
temp->next = NULL;
*id = temp->id;
//deallocate memory
free(temp);
return *head;
}
13. Given the following structure struct node { struct node *next; int id; }; Write a...
Given the following linked list structure called node: struct node { int val; struct node * ptrNext; }; Assume we have a single list created from this structure with a head pointer called ptrFirst which is declared in the global scope. a. Write a complete C function called CountEven to count all the even values in this singly linked list of arbitrary number of nodes using an iterative (non-recursive) approach. The function takes as parameter the pointer to the starting...
Write a function with the following prototype struct node* copyList(struct node* 1ist) The struct node is the same used in question 18. The function should create a new, separate copy of the linked list pointed to by the list parameter and return a pointer to the head of the newly created linked list copy.
Given the node structure and the head pointer (headptr) of a linked list write a code to move the last element to the head of the list. struct node { int value; struct node *next; };
In C++ Assuming: struct node { int data; node * next; }; and copyList(node * head); Write a recursive function (copyList(node * head)) that will create a copy of the singly linked list.
C++ Consider the following structure of node and linked list. struct Node { int key; Node *next; }; 10 -> 20 -> 30 -> 10 -> 10 -> 50 -> 10 -> NULL What will be the output of following pseudo-code? Consider head is the pointer to the first node of above linked list. Node *walker = head; int count = 0; while(walker!= NULL && count < 3) { if(walker->key == 10) { count = count + 1; } walker...
Structure struct Node int Manth; // Mont h double dAvg: 1/ Average struct Node pNext // with, the linked İist 3hown above the function will return gven that the average is 3.8 Ptr to next -Node; Ret (3,3.8) (4,2.5) (20pts)( Recursive function) Show the code for a function that receives a pointer to the head of an ordered singly linked list that uses the structure in the top left. The function will return the pointer node that shows the highest...
Consider the following C++ statements: struct node Type int info; node Type *link; nodeType *head, *p, q, *newNode; newNode = new node Type; 1. Write C++ statement to store 50 in the info field of the newNode. 2. Write C++ statement to set the link field of the newNode to NULL. 3. Write C++ statement to make head pointer points to newNode. 4. Write C++ statement to delete the first node in the linked list. (the first an the only...
C++ Assume that you have a structure Node as follows: struct Node { int value; Node* next; Node(int v, Node* n = nullptr) { // constructor value = v; next = n; } }; Question 1 (20 points) Write a function "count" that counts the number of elements in a given linked list. The prototype of this function is as follows: int count_elements(Node*); Question 2 (20 points) Write a function “average" that calculates the average of all the elements in...
C++ program, inventory.cpp implementation Mostly need the int load(istream&) function. Implementation: You are supposed to write three classes, called Item, Node and Inventory respectively Item is a plain data class with item id, name, price and quantity information accompanied by getters and setters Node is a plain linked list node class with Item pointer and next pointer (with getters/setters) Inventory is an inventory database class that provides basic linked list operations, delete load from file / formatted print functionalities. The...
Define a struct Node to represent a node in a double linked-list that stores a string as data. Write a function to insert a node to the head of the linked list. The function takes two arguments: a pointer to the first node in the double linked list and a string value. It should create a new node with the given value to the head of the double linked list.