Question

In: Computer Science

Create a C++ integer linked list program that performs the following methods below: Please create these...

Create a C++ integer linked list program that performs the following methods below:

Please create these three source files: intList.h, intList.cpp, & intListTest.cpp.

Implement recursive routines in the intList class to do the following:

  • Print the list in reverse order
  • Return the value in the middle node
  • Return the average of all the odd values
  • Remove every node containing an odd value

Modify main (in IntListTest.cpp) so it does the following:

  1. Insert the numbers 1, 3, 4, 6, 7, 10, 15, 20, 33.
  2. Find the sum of all values.
  3. Print the list in reverse order.
  4. Print the value of the middle node.
  5. Print the average of all the odd values.
  6. Remove every node containing an odd value.
  7. Print the list in reverse order.
  8. Print the value of the middle node.

Steps 3-8 must use recursion.

Please follow requirements.

Solutions

Expert Solution

Note: The language used is C++. The code might not be indented in this editor, so refer to code snippets for clarity.

Code:

1. intListTest.cpp File

#include<iostream>
#include "intList.h"
using namespace std;

int main()
{
intList List;
List.insertNode(1);
List.insertNode(3);
List.insertNode(4);
List.insertNode(6);
List.insertNode(7);
List.insertNode(10);
List.insertNode(15);
List.insertNode(20);
List.insertNode(33);

cout<<"Sum of values in Linked List = "<<List.getSum()<<endl;
List.printReverseList();

cout<<"The middle element is "<<List.getMiddle()<<endl;

cout<<"The average of all odd nodes is "<<List.getAverageOfOddValues()<<endl;

cout<<"\nRemoving odd values...\n";
List.removeOddValues();

cout<<endl;
List.printReverseList();

cout<<"The middle element is "<<List.getMiddle()<<endl;

return 0;
}

2. intList.h File

#ifndef INTLIST_H
#define INTLIST_H


struct Node{
int data;
Node *next;
};

class intList
{
public:
intList();

void insertNode(int data);

int getSum();
void printReverseList();
int getMiddle();
float getAverageOfOddValues();
void removeOddValues();

private:
Node *head;
Node* createNode(int data);
void printReverseListRecur(Node *head);
void getMiddleRecur(Node *head, int &n, Node* &middle);
void getSumOfOddValues(Node *head, int & sumOfOddValues, int & n);
void removeOddValuesUtil(Node* &head);
};

#endif // INTLIST_H

3. intList.cpp File

#include<iostream>
#include "intList.h"
using namespace std;

intList::intList()
{
head = NULL;
}


Node* intList :: createNode(int data)
{
Node *newNode = new Node;
newNode->data = data;
newNode->next = NULL;
return newNode;
}

void intList :: insertNode(int data)
{
Node *newNode = createNode(data);
if(head == NULL){
head = newNode;
return;
}
Node *curr = head;
while(curr->next != NULL)
curr = curr->next;

curr->next = newNode;
}

void intList :: printReverseList()
{
cout<<"The reversed list is :\n";
printReverseListRecur(head);
cout<<"\n";
}

void intList :: printReverseListRecur(Node *head)
{
if(head == NULL)
return;
printReverseListRecur(head->next);
cout<<head->data<<" ";
}


int intList :: getSum()
{
Node *curr = head;
int sum = 0;
while(curr != NULL){
sum = sum + curr->data;
curr = curr->next;
}
return sum;
}

int intList :: getMiddle()
{
int n = 1;
Node *middle = NULL;
getMiddleRecur(head, n, middle);
return middle->data;
}

void intList :: getMiddleRecur(Node *head, int &n, Node* &middle)
{
if(head == NULL){
n = n/2;
return;
}
n++;
getMiddleRecur(head->next, n, middle);

n--;
if(n == 0)
middle = head;
}


float intList :: getAverageOfOddValues()
{
int n = 0;
int sumOfOddValues = 0;
getSumOfOddValues(head, sumOfOddValues, n);
float avg = sumOfOddValues*1.0/n;
return avg;
}

void intList :: getSumOfOddValues(Node *head, int & sumOfOddValues, int & n)
{
if(head == NULL)
return;
if(head->data % 2 != 0){
sumOfOddValues += head->data;
n++;
}
getSumOfOddValues(head->next, sumOfOddValues, n);
}


void intList :: removeOddValues()
{
//Node *curr = head;
//Node *prev = NULL;
removeOddValuesUtil(head);
}

void intList :: removeOddValuesUtil(Node* &head)
{
if(head == NULL)
return;

Node *curr = head;
Node *prev = NULL;

while(curr){
//cout<<curr->data<<endl;
if(curr->data % 2 != 0){
if(curr == head){
head = curr->next;
delete curr;
curr = head;
}else{
Node *temp = curr;
curr = curr->next;
prev->next = curr;
delete temp;
}
}else{
prev = curr;
curr = curr->next;
}
}
}

Code Snippets:

1. intListTest.cpp File

2. intList.h File

3. intList.cpp File

Sample Output:


Related Solutions

Create a C program that performs the following (please comment the codes): a) Create a Stack...
Create a C program that performs the following (please comment the codes): a) Create a Stack ADT. Stack should be implemented using the linked list. b) Enter 10 random integer numbers between 0 to 50 in the stack. c) After pushing each element, print the content of the top of the stack. c) Then pop out those 10 integer numbers and print those numbers. d) Finally destroy the Stack.
Write in C++: create a Doubly Linked List class that holds a struct with an integer...
Write in C++: create a Doubly Linked List class that holds a struct with an integer and a string. It must have append, insert, remove, find, and clear.
/* *fix the below C Program to Display the Nodes of a Linked List in Reverse...
/* *fix the below C Program to Display the Nodes of a Linked List in Reverse */ #include <stdio.h> #include <stdlib.h> struct node { int visited; int a; struct node *next; }; int main() { struct node *head = NULL; generate(head); printf("\nPrinting the list in linear order\n"); linear(head); printf("\nPrinting the list in reverse order\n"); display(head); delete(head); return 0; } void display(struct node *head) { struct node *temp = head, *prev = head; while (temp->visited == 0) { while (temp->next !=...
1) a. Write down a C++ program which will create a list (simple linear linked list)...
1) a. Write down a C++ program which will create a list (simple linear linked list) of nodes. Each node consists of two fields. The first field is a pointer to a structure that contains a student id (integer) and a gradepoint average (float). The second field is a link. The data are to be read from a text file. Your program should read a file of 10 students (with student id and grade point average) and test the function...
Write down a C program which will create a list (simple linear linked list) of nodes....
Write down a C program which will create a list (simple linear linked list) of nodes. Each node consists of two fields. The first field is a pointer to a structure that contains a student id (integer) and a grade-point average (float). The second field is a link. The data are to be read from a text file. Your program should read a file of 10 students (with student id and grade point average) and test the function you wrote...
Create a Linked List and conduct the following operations. Portion of the program is given. The...
Create a Linked List and conduct the following operations. Portion of the program is given. The operations are: Add an “H” to the list Add an “I” to the list Add 100 to the list Print the content of the list and its size Add a “H” to the first place of the list Add a “R” to the last place of the list Get the element of position 3 and print it Get the last element and print it...
Create a ValueGet() method that takes a linked list as input and an integer index and...
Create a ValueGet() method that takes a linked list as input and an integer index and returns the value stored in the node at that index position. Sample Input: 01->100->300->214, index = 2 Output: 300 At index 2 the node has a value of 300 give me the full code. Give the full code with c++
1.Please write a C++ program that counts the nodes in a linked list with the first...
1.Please write a C++ program that counts the nodes in a linked list with the first node pointed to by first. Also please explain. 2. Write a program to determine the average of a linked list of real numbers with the first node pointed to by first. 3. Determine the computing times of the algorithms in question 1 and 4. Write a program to insert a new node into a linked list with the first node pointed to by first...
**JAVA** Create a Linked List and conduct the following operations. Portion of the program is given....
**JAVA** Create a Linked List and conduct the following operations. Portion of the program is given. The operations are: Add an “H” to the list Add an “I” to the list Add “100” to the list Print the content of the list and its size Add a “H” to the first place of the list Add a “R” to the last place of the list Get the element of position 3 and print it Get the last element and print...
C++ Write a program to create a linked list which stores the details of employees(Employee number,...
C++ Write a program to create a linked list which stores the details of employees(Employee number, employee name, rate, hours worked). Create a menu to manage the emoployee data. MENU 1. ADD EMPLOYEE DETAILS 2. DELETE EMPLOYEE 3. SEARCH EMPLOYEE 4. PRINT EMPLOYEE PAYROLL 5. EXIT When the user selected option #4 the program should print the following pay report for each employee: EmpNo.     Name      Rate    Hours    Regular Pay      Overtime Pay     Gross Pay Any hours worked above 40 hours are...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT