C++. Can you compete the code * Lab to practice recursion using a particular linked list. #include #include

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

C++. Can you compete the code * Lab to practice recursion using a particular linked list. #include #include

Post by answerhappygod »

C++. Can you compete the code
* Lab to practice recursion using a particular linked
list.

#include <iostream>
#include <cstdlib>
using namespace std;
/*
* Node Struct
*/
struct Node {
Node(int data) : m_data(data), m_next(nullptr) {} //
Overloaded constructor
int m_data; // Data in node
Node* m_next; // Pointer to next node
};
/*
* Name: InsertArray(int* arr, int size, Node* head)
*
* Desc: This recursive function converts an array to a linked
list
*
* Preconditions: A valid array and its respective size
* are passed to the function. A pointer to the head of
* the linked list is also given.
*
* Postcondition: All items from the passed array are
* inserted in order into the linked list. The head of
* the updated linked list is returned.
*
* Hint: Be careful with the order of the recursive calls
* and the inserts.
*
* Hint 2: Plan your recursive case and base case before
* coding.
*
* Hint 3: The InsertArray function should insert nodes
to
* the front of the linked list and should start at the end of the
array.
*/
// IMPLEMENT INSERTARRAY HERE - some hints are below
Node* InsertArray(int* arr, int size, Node* head) {
// Recursive case (until no items remain, size != 0)
// Create new node (with data from array) and insert node
into list
// Set m_next in node
// Update head
// Insert next item (recursively)
// Base case (when size == 0)
// Return the final head
}
/*
* main()
* DO NOT EDIT
*/
int main() {
const int ARR_SIZE = 6; // Size of the array being
inserted into the linked list
int arrToInsert[ARR_SIZE] = { 1, 2, 3, 4, 5, 6 }; //
Array to populate linked list
Node* head = nullptr; // Pointer to the first node in
the linked list, head node
// Print items in array
cout << "The following array items inserted
into the linked list in the following order:\n";
for (int i = 0; i < ARR_SIZE; i++) {
cout << arrToInsert << "
";
}
cout << "\n\n";
// Populate linked list with the given array
head = InsertArray(arrToInsert, ARR_SIZE, head);
// Print all nodes
cout << "The following linked list will be the
same values in order as the array above:\n";
Node* curr = head; // current (cursor for the linked
list)
while (curr) {
cout << curr->m_data <<
(curr->m_next ? "->" : "->END\n\n");
curr = curr->m_next;
}
// Deallocate all nodes
cout << "Deallocating all nodes. There should
be no memory leaks or errors.\n";
while (head) {
curr = head;
head = head->m_next;
delete curr;
}
// Exit successfully
return 0;
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply