A-1)
int count_elements(Node* head, int search_for)
{
Node* current = head;
int count = 0;
while (current != NULL) {
if (current->data == search_for)
count++;
current = current->next;
}
return count;
}
A-2)
double average(struct Node* head)
{
// if head = NULL
if (!head)
return -1;
int count = 0; // Initialize count
int sum = 0;
float avg = 0.0;
struct Node* current = head; // Initialize current
while (current != NULL) {
count++;
sum += current->data;
current = current->next;
}
// calculate average
avg = (double)sum / count;
return avg;
}
A-3)
void insert_sorted(struct Node** head_ref, struct Node* new_node)
{
struct Node* current;
/* Special case for the head end */
if (*head_ref == NULL || (*head_ref)->data >= new_node->data)
{
new_node->next = *head_ref;
*head_ref = new_node;
}
else
{
/* Locate the node before the point of insertion */
current = *head_ref;
while (current->next!=NULL &&
current->next->data < new_node->data)
{
current = current->next;
}
new_node->next = current->next;
current->next = new_node;
}
}
A-4)
void delete_all_even(Node** head_ref)
{
Node* ptr = *head_ref;
// Node* next;
while (ptr != NULL) {
// next = ptr->next;
// If true, delete node 'ptr'
if (ptr->data % 2 == 0)
deleteNode(head_ref,
ptr->data);
ptr = ptr->next;
}
}
A-5)
int main()
{
struct Node* head = NULL;
// create linked list 7->6->8->4->1
push(&head, 7);
push(&head, 6);
push(&head, 8);
push(&head, 4);
push(&head, 1);
cout << "Count No. of nodes = " << count_elements(head);
cout << "Average of nodes = " << avgerage(head);
cout << "Sorting of nodes = " << insert_sorted(head);
cout << "Delete Number of nodes = " << delete_all_even(head);
return 0;
}
C++ Assume that you have a structure Node as follows: struct Node { int value; Node*...
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...
CODE IN C Objectives: Queue operations. Data structure: typedef struct { int width; int height; }Rect; typedef struct node { Rect* r; struct node* next; }Node; typedef struct { Node* head; Node* tail; }Queue; Specification: In this lab six Queue-related operation functions need to be implemented by using the given function prototypes and data structures. 1. List* createQueue(void); This function initializes an empty “Queue” with the “Queue” data structure and returns an empty queue. 2. int enQueue(List*); This function receives...
Consider a Linked List program with the following class: typedef int datatype; struct node { datatype data; node *tail; }; class LinkedList{ private: node *head; node *current;public: //constructors LinkedList(); LinkedList(int i); //destructor ~LinkedList(); bool start(); //sets list postion to header bool nextNode(); //increments to next node in list int getCurrent(); //returns data from current node void insertNode(int i); //inserts node after current node //then sets current node to new node bool deleteNode();//deletes currentnode void deleteAll(); //deletes all nodes };...
CODE IN C Objectives: Queue operations. Data structure: typedef struct { int width; int height; }Rect; typedef struct node { Rect* r; struct node* next; }Node; typedef struct { Node* head; Node* tail; }Queue; Specification: In this lab six Queue-related operation functions need to be implemented by using the given function prototypes and data structures. 1. List* createQueue(void); This function initializes an empty “Queue” with the “Queue” data structure and returns an empty queue. 2. int enQueue(List*); This function receives...
14. p contains the elements 66, 9, 14, 52, 87, 14 and 17, in that order. Consider running the following line of code: p = question4(p); where question4 is the function defined below. Show the contents of p after the function call. struct node* question4(struct node *list) { struct node* a = list; struct node* b = list; struct node* c; if (a == NULL) return NULL; while ( a->next != NULL) a = a ->next; a->next = b; c...
14. p contains the elements 66, 9, 14, 52, 87, 14 and 17, in that order. Consider running the following line of code: p = question4(p); where question4 is the function defined below. Show the contents of p after the function call. struct node* question4(struct node *list) { struct node* a = list; struct node* b = list; struct node* c; if (a == NULL) return NULL; while ( a->next != NULL) a = a ->next; a->next = b; c...
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.
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.
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...