#include<conio.h>
#include<stdio.h>
#include<malloc.h>
struct node // structure
{
int data;
struct node *next;
};
void Display(struct node *n) // Function to display the link list
{
while (n != NULL)
{
printf(" %d ", n->data);
n = n->next;
}
}
void Push_Tail(struct node** head_ref, int new_data) // Function to add a node at the end of the link list
{
struct node* new_node = (struct node*) malloc(sizeof(struct node));
struct node *last = *head_ref;
new_node->data = new_data;
new_node->next = NULL;
if (*head_ref == NULL)
{
*head_ref = new_node;
return;
}
while (last->next != NULL)
last = last->next;
last->next = new_node;
return;
}
struct node* copyList(struct node *n) // Copy List Function
{
struct node* temp = NULL; // Used for temporary node creation
struct node* prev = NULL; // Using this variable to keep a track of the previous node
struct node* head = NULL; // return this head pointer
int i=0;
while (n != NULL)
{
temp = (struct node*)malloc(sizeof(struct node)); // Create a temporary node
temp->data =n->data;
temp->next = NULL;
if(i==0) // Point head to the first node
head=temp;
if(prev!=NULL) // Link the previous node to the next node
prev->next=temp;
prev=temp; // Keep track of the previous node
n = n->next; // Look for the next node
i++; // i is used just to copy the address of the 1st node to head
}
return head; // return the head node
}
int main()
{
struct node* head;
struct node* list;
Push_Tail(&head,10);
Push_Tail(&head,23);
Push_Tail(&head,35);
Push_Tail(&head,47);
Push_Tail(&head,59); // List is 10->23->35->47->59
printf("\nORIGNAL LIST\n");
Display(head);
printf("\nCOPY LIST\n");
list=copyList(head);
Display(list);
getch();
return 0;
}
OUTPUT
Write a function with the following prototype struct node* copyList(struct node* 1ist) The struct node is...
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.
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...
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...
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.
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...
Write a C++ function to add a node to the beginning of a linked list. Your function takes two arguments - the head of the linked list and the value num to be added. Note that the list may be empty! Your function should modify the head of the linked list to point to the new node, and set the new node to point to the rest of the list (if not empty). Example: Initial Array: 4->2->3, key = 5...
2) (10 pts) Write a function that takes in a pointer to a linked list of nodes storing integers and a variable named value, and returns the number of nodes in the list storing that value. For example, if a list pointed to by listPtr stores 2, 6, 2, 3, 4, 2, 6, and 6 and value = 6, your function should return 3, since 6 appears in the list 3 times. Please use the struct and function prototype provided...
A linked list of integers is built using the following struct: struct node { int data; struct node *next; }; Define a function named max that returns the maximum integer in a list. The function takes one arguments, a pointer to the head of the list. The function returns an integer, which is the maximum value. If the list is empty, return zero. NOTE: You know nothing about the values in the list. They could all be negative!
Language:C++ only numbers 4 and 5 please 2. 1. Use the Node class from the slides to create a linked list of int's. Create a Node pointer called ptrHead that points to the head of the list. Make the list 5 nodes long. Generate random values to populate the list's data. All nodes should be in the heap. Add a function called addNodeFront that takes a pointer to the head of a list as an input parameter and that adds...
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...