C++ problem
Part 1: Recursive Print (40 pts)
Please write the recursive List reverse print function, whose iterative version we wrote in class.
Below are the function signatures for the functions you are going to need:
public:
/**Additional Operations*/
void print_reverse();
//Wrapper function that calls the reverse helper function to print a list in reverse
//prints nothing if the List is empty
private:
void reverse(Nodeptr node);
//Helper function for the public printReverse() function.
//Recursively prints the data in a List in reverse.
Why do we need the private helper function here?
Since we are going to be reversing our list node by node, in a recursive fashion, we want to pass
a one node at a time to our reverse function.
However, since our nodes are private, we cannot access them if we call the function inside of
main.
Add these function signatures to your List.h file along with your other function prototypes inside
the class definition.
Make sure that you place the reverse function inside the private portion of your List class
definition and the print_reverse function prototype to the public portion of your List class
definition.
Now, implement these two functions inside of List.h, under your section for additional
operations.
Important: Test each function carefully inside of your ListTest.cpp to make sure that it is
working properly.
Part 2: Adding an Index to Your List Nodes (20 pts)
Next, you will add the following functions to your List.h
/**Accessor Functions*/
int get_index();
//Indicates the index of the Node where the iterator is currently pointing
//Nodes are numbered from 1 to length of the list
//Pre: length != 0
//Pre: !off_end()
...
int List::get_index()
{
//Implement the function here
}
/**Manipulation Procedures*/
void scroll_to_index(int index);
//Moves the iterator to the node whose index is specified by the user
//Pre: length != 0
...
void scroll_to_index(int index)
{
//Implement function here
}
Part 3: Implementing Search as Part of Your List (40 pts)
Now, we are going to add two search functions to our List so that we can search for elements in
our List.
The first of these functions is going to be a simple linear search function.
You will need to add the following function prototype and function definition to your List.h:
/**Additional Operations*/
int linear_search(listitem item);
//Searchs the list, element by element, from the start of the List to the end of the List
//Returns the index of the element, if it is found in the List
//Returns -1 if the element is not in the List
//Pre: length != 0
...
int List::linear_search(listitem item)
{
//Implement the function here
}
You are also going to add a function to perform recursive binary search on your List.
You will need to add the following function prototype and function definition to your List.h:
int binary_search(int low, int high, listitem item);
//Recursively searchs the list by dividing the search space in half
//Returns the index of the element, if it is fo.
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
1. C++ problem
Part 1: Recursive Print (40 pts)
Please write the recursive List reverse print function, whose iterative version we wrote in class.
Below are the function signatures for the functions you are going to need:
public:
/**Additional Operations*/
void print_reverse();
//Wrapper function that calls the reverse helper function to print a list in reverse
//prints nothing if the List is empty
private:
void reverse(Nodeptr node);
//Helper function for the public printReverse() function.
//Recursively prints the data in a List in reverse.
Why do we need the private helper function here?
Since we are going to be reversing our list node by node, in a recursive fashion, we want to pass
a one node at a time to our reverse function.
However, since our nodes are private, we cannot access them if we call the function inside of
main.
Add these function signatures to your List.h file along with your other function prototypes inside
the class definition.
Make sure that you place the reverse function inside the private portion of your List class
definition and the print_reverse function prototype to the public portion of your List class
definition.
Now, implement these two functions inside of List.h, under your section for additional
operations.
Important: Test each function carefully inside of your ListTest.cpp to make sure that it is
working properly.
Part 2: Adding an Index to Your List Nodes (20 pts)
Next, you will add the following functions to your List.h
/**Accessor Functions*/
int get_index();
//Indicates the index of the Node where the iterator is currently pointing
//Nodes are numbered from 1 to length of the list
//Pre: length != 0
2. //Pre: !off_end()
...
int List::get_index()
{
//Implement the function here
}
/**Manipulation Procedures*/
void scroll_to_index(int index);
//Moves the iterator to the node whose index is specified by the user
//Pre: length != 0
...
void scroll_to_index(int index)
{
//Implement function here
}
Part 3: Implementing Search as Part of Your List (40 pts)
Now, we are going to add two search functions to our List so that we can search for elements in
our List.
The first of these functions is going to be a simple linear search function.
You will need to add the following function prototype and function definition to your List.h:
/**Additional Operations*/
int linear_search(listitem item);
//Searchs the list, element by element, from the start of the List to the end of the List
//Returns the index of the element, if it is found in the List
//Returns -1 if the element is not in the List
//Pre: length != 0
...
int List::linear_search(listitem item)
{
//Implement the function here
}
You are also going to add a function to perform recursive binary search on your List.
You will need to add the following function prototype and function definition to your List.h:
int binary_search(int low, int high, listitem item);
//Recursively searchs the list by dividing the search space in half
//Returns the index of the element, if it is found in the List
3. //Returns -1 if the element is not in the List
//Pre: length != 0
//Pre: List is sorted (must test on a sorted list)
...
int List::binary_search(int low, int high, listitem item)
{
//Implement the function here
}
Important: For credit, you must implement the recursive version of binary search.
This is my current List.h file:
#ifndef LIST_H_
#include //for NULL
#include
#include
#include
using namespace std;
template
class List {
private:
struct Node {
listitem data;
Node* next;
Node* previous;
Node(listitem data) :
next(NULL), previous(NULL), data(data) {
}
};
typedef struct Node* NodePtr;
NodePtr start;
NodePtr end;
NodePtr cursor;
int length;
void reverse(NodePtr node);
//Helper function for the public printReverse() function.
//Recursively prints the data in a List in reverse.
public:
4. /**Constructors and Destructors*/
List();
//Default constructor; initializes and empty list
//Postcondition: list is initialized or is empty
~List();
//Destructor. Frees memory allocated to the list
//Postcondition: Memory in the list is deleted
List(const List &list);
//Copy construcor. Initializes list to have the same elements as another list
//Postcondition: Other lists have same elements as the main list
/**Accessors*/
listitem get_start();
//Returns the first element in the list
//Precondition: first element must not be empty
listitem get_end();
//Returns the last element in the list
//Precondition: last element must not be empty
listitem get_cursor();
//Returns the element pointed to by the iterator
//Precondition: the element pointed to by the iterator must not be empty
bool is_empty();
//Determines whether a list is empty.
bool off_end();
//Determines if the iterator is off the end of the list (i.e. whether cursor is NULL)
int get_length();
//Returns the length of the list
/**Manipulation Procedures*/
void begin_cursor();
//Moves the iterator to point to the first element in the list
//If the list is empty, the iterator remains NULL
//Postcondition: the iterator point to the first element in the list
void add_cursor(listitem data);
//Inserts a new element into the list in the position after the iterator
//Precondition: the iterator must point to an element
//Postcondition: a new element is inserted in the position after the iterator
void remove_end();
5. //Removes the value of the last element in the list
//Precondition: last element is not a null object
//Postcondition: the the value of the last element in the list is removed
void remove_start();
//Removes the value of the first element in the list
//Precondition: first element is not a null object
//Postcondition: the value of the first element in the list is removed
void add_end(listitem data);
//Inserts a new element at the end of the list
//If the list is empty, the new element becomes both start and end
//Postcondition: a new element is inserted at the end of the list
void add_start(listitem data);
//Inserts a new element at the start of the list
//If the list is empty, the new element becomes both start and end
//Postcondition: a new element is inserted at the start of the list
void remove_cursor();
//Removes the element pointed at by the iterator
//Precondition: the element pointed at by the iterator is not a null object
//Postcondition: the element pointed at by the iterator is removed
void move_cursor();
//Moves the iterator forward by one element in the list
//Precondition: the next element is not a null object
//Postcondition: the iterator moves forward by one element in the list
/**Additional List Operations*/
void print();
//Prints to the console the value of each element in the list sequentially
//and separated by a blank space
//Prints nothing if the list is empty
bool operator==(const List &list);
//Tests two lists to determine whether their contents are equal
//Postcondition: returns true if lists are equal and false otherwise
/**Additional Operations*/
void print_reverse();
//Wrapper function that calls the reverse helper function to print a list in reverse
//prints nothing if the List is empty
};
6. template
List::List() :
start(NULL), end(NULL), cursor(NULL), length(0) {
}
template
List::~List() {
cursor = start;
NodePtr temp;
while (cursor != NULL) {
temp = cursor->next;
delete cursor;
cursor = temp;
}
}
template
List::List(const List &list) :
length(list.length) {
if (list.start == NULL) //If the original list is empty, make an empty list for this list
{
start = end = cursor = NULL;
} else {
start = new Node(list.start->data); //using second Node constructor
NodePtr temp = list.start; //set a temporary node pointer to point at the start of the original
list
cursor = start; //set iterator to point to start of the new list
while (temp->next != NULL) {
temp = temp->next; //advance up to the next node in the original list
cursor->next = new Node(temp->data); //using node constructor to create a new node
with copy of data
cursor = cursor->next; //advance to this new node
}
end = cursor; //Why do I need this line of code?
cursor = NULL;
}
}
template
7. void List::print() {
NodePtr temp = start; //create a temporary iterator
while (temp != NULL) {
//What two lines of code go here?
cout << temp->data << " ";
temp = temp->next;
//Hint: One statement should contain a cout
}
cout << endl; //What does this do?
}
template
void List::add_start(listitem data) {
if (length == 0) //Why is this necessary?
{
start = new Node(data);
end = start;
} else {
NodePtr N = new Node(data); //create the new node by calling the node constructor
N->next = start; //set the new node's next field to point to the start
start->previous = N;
start = N; //make the start be the new node
}
length++;
}
template
void List::add_end(listitem data) {
if (length == 0) {
start = new Node(data);
end = start;
} else {
NodePtr N = new Node(data); //create the new node by calling the node constructor
end->next = N;
N->previous = end;
end = N;
}
length++;
13. NodePtr temp;
while (cursor != NULL) {
temp = cursor->next;
delete cursor;
cursor = temp;
}
}
template
List::List(const List &list) :
length(list.length) {
if (list.start == NULL) //If the original list is empty, make an empty list for this list
{
start = end = cursor = NULL;
}
else {
start = new Node(list.start->data); //using second Node constructor
NodePtr temp = list.start; //set a temporary node pointer to point at the start of the original list
cursor = start; //set iterator to point to start of the new list
while (temp->next != NULL) {
temp = temp->next; //advance up to the next node in the original list
cursor->next = new Node(temp->data); //using node constructor to create a new node with copy
of data
cursor = cursor->next; //advance to this new node
}
end = cursor; //Why do I need this line of code?
cursor = NULL;
}
}
template
void List::print() {
NodePtr temp = start; //create a temporary iterator
while (temp != NULL) {
//What two lines of code go here?
cout << temp->data << " ";
temp = temp->next;
//Hint: One statement should contain a cout
14. }
cout << endl; //What does this do?
}
template
void List::add_start(listitem data) {
if (length == 0) //Why is this necessary?
{
start = new Node(data);
end = start;
}
else {
NodePtr N = new Node(data); //create the new node by calling the node constructor
N->next = start; //set the new node's next field to point to the start
start->previous = N;
start = N; //make the start be the new node
}
length++;
}
template
void List::add_end(listitem data) {
if (length == 0) {
start = new Node(data);
end = start;
}
else {
NodePtr N = new Node(data); //create the new node by calling the node constructor
end->next = N;
N->previous = end;
end = N;
}
length++;
}
template
bool List::is_empty() {
return (length == 0);
}