C++. Please, test your program before you submit the answer.
Background:
Circular Linked List: A circular linked list is a linked list where a non-empty list has no null pointers.
Unless empty, each node has a pointer to the next node, and the last node points back to the first
node.
Assigment:
Write a cpp program that will serve the purpose of a test driver for previously written CLinkedList.h
file. Your program will:
1) Create a choice menu and prompt the user for input. Must be case insensitive!
2) Read the input and call for functions outside of main, in order to perform operations chosen by
the user. These functions involve: Inserting Interger, Removing Interger, Forward Print and
Backward Print.
Requirements and Notes:
1. Forwards Print does not require a subfunction in order to produce output. All the other
operations must call corresponding subfunctions.
2. Nodes of the circular list must be arranged in ascending order.
3. User Input must be processed case insensitive, and implemented using the loop.
4. Document your code completely.
5. CLinkedList.h file contains 3 classes.
- Circular linked lists class with following member functions: constructor, copy constructor,
destructor, assignment operator, insert and remove.
- Node class included in the file header.
- A list iterator class provides a public interface to the list. The pointer points at nodes in the linked
list. Its member functions are as follows:
Constructor: Assigns the linked list data member to the parameter, a reference to a circular linked
list.
begin() sets the iterator to the first node of the linked list (or NULL if the list is empty)
isEmpty() returns whether or not the wrapped linked list is empty
isFirstNode() returns whether the present node is the first node of the linked list
isLastNode() returns whether the present node is the last node of the linked list
operator*() returns the data of the node currently pointed at. (You need 2)
operator++() pre-increment operator advances the pointer to the next node in the list, if there is
one
operator++(int) post-increment operator advances the pointer to the next node in the list, if there
is one
6. Insert function must give feedback where it says the position of where the int was inserted for
example: "Inserting 25 in the middle."
7. Remove function must give either one of these types of feedback:
- "Removing 25" if the int given by the user was found in the list
-"Cannot find 25" if it was not. The program will then move the user back to the choice menu.
Files:
// File: Node.h
// Singly-linked list node definition/implementation (also good for circular)
#ifndef NODE_H
#define NODE_H
// Need to prototype template classes if they are to be friends
template <typename eltType> class CLinkedList;
template <typename eltType> class CListItr;
template <typename eltType> class Node
{ private:
Node(eltType info, Node* link = NULL ) :
data(info), next(link)
{};
eltType data;
Node *next;
friend class CLinkedList<eltType>;
friend c.
C++ Please test your program before you submit the answer.pdf
1. C++. Please, test your program before you submit the answer.
Background:
Circular Linked List: A circular linked list is a linked list where a non-empty list has no null pointers.
Unless empty, each node has a pointer to the next node, and the last node points back to the first
node.
Assigment:
Write a cpp program that will serve the purpose of a test driver for previously written CLinkedList.h
file. Your program will:
1) Create a choice menu and prompt the user for input. Must be case insensitive!
2) Read the input and call for functions outside of main, in order to perform operations chosen by
the user. These functions involve: Inserting Interger, Removing Interger, Forward Print and
Backward Print.
Requirements and Notes:
1. Forwards Print does not require a subfunction in order to produce output. All the other
operations must call corresponding subfunctions.
2. Nodes of the circular list must be arranged in ascending order.
3. User Input must be processed case insensitive, and implemented using the loop.
4. Document your code completely.
5. CLinkedList.h file contains 3 classes.
- Circular linked lists class with following member functions: constructor, copy constructor,
destructor, assignment operator, insert and remove.
- Node class included in the file header.
- A list iterator class provides a public interface to the list. The pointer points at nodes in the linked
list. Its member functions are as follows:
Constructor: Assigns the linked list data member to the parameter, a reference to a circular linked
list.
begin() sets the iterator to the first node of the linked list (or NULL if the list is empty)
isEmpty() returns whether or not the wrapped linked list is empty
isFirstNode() returns whether the present node is the first node of the linked list
isLastNode() returns whether the present node is the last node of the linked list
operator*() returns the data of the node currently pointed at. (You need 2)
operator++() pre-increment operator advances the pointer to the next node in the list, if there is
one
operator++(int) post-increment operator advances the pointer to the next node in the list, if there
is one
6. Insert function must give feedback where it says the position of where the int was inserted for
example: "Inserting 25 in the middle."
7. Remove function must give either one of these types of feedback:
- "Removing 25" if the int given by the user was found in the list
-"Cannot find 25" if it was not. The program will then move the user back to the choice menu.
Files:
2. // File: Node.h
// Singly-linked list node definition/implementation (also good for circular)
#ifndef NODE_H
#define NODE_H
// Need to prototype template classes if they are to be friends
template <typename eltType> class CLinkedList;
template <typename eltType> class CListItr;
template <typename eltType> class Node
{ private:
Node(eltType info, Node* link = NULL ) :
data(info), next(link)
{};
eltType data;
Node *next;
friend class CLinkedList<eltType>;
friend class CListItr<eltType>;
};
#endif
// File: CLinkedList.h
// Circular Linked List class with List Iterator class
#ifndef CLINKEDLIST_H
#define CLINKEDLIST_H
#include "Node.h"
#include <iostream>
template<class T>
class ListIterator;
template<class T>
class CLinkedList {
private:
Node<T> *last;
public:
CLinkedList() : last(nullptr) {}
CLinkedList(const CLinkedList<T>& other);
~CLinkedList();
CLinkedList<T>& operator=(const CLinkedList<T>& other);
void insert(const T& value);
void remove(const T& value);
friend class ListIterator<T>;
};
template<class T>
CLinkedList<T>::CLinkedList(const CLinkedList<T>& other) {
if (other.last == nullptr) {
3. last = nullptr;
}
else {
last = new Node<T>(other.last->data);
Node<T>* current = other.last->next;
while (current != other.last) {
insert(current->data);
current = current->next;
}
insert(current->data);
}
}
template<class T>
CLinkedList<T>::~CLinkedList() {
if (last != nullptr) {
Node<T>* current = last->next;
last->next = nullptr;
delete current;
}
}
template<class T>
CLinkedList<T>& CLinkedList<T>::operator=(const CLinkedList<T>& other) {
if (this != &other) {
CLinkedList<T> temp(other);
std::swap(last, temp.last);
}
return *this;
}
template<class T>
void CLinkedList<T>::insert(const T& value) {
Node<T>* newNode = new Node<T>(value);
if (last == nullptr) {
newNode->next = newNode;
last = newNode;
}
else {
Node<T>* current = last->next;
Node<T>* prev = last;
while (current != last && current->data < value) {
prev = current;
current = current->next;
}
4. if (current == last && current->data < value) {
last = newNode;
}
prev->next = newNode;
newNode->next = current;
}
}
template<class T>
void CLinkedList<T>::remove(const T& value) {
if (last != nullptr) {
Node<T>* current = last->next;
Node<T>* prev = last;
while (current != last && current->data < value) {
prev = current;
current = current->next;
}
if (current->data == value) {
if (current == last && current->next == last) {
last = nullptr;
}
else {
prev->next = current->next;
if (current == last) {
last = prev;
}
}
current->next = nullptr;
delete current;
}
}
}
template<class T>
class ListIterator {
private:
const CLinkedList<T>& list;
Node<T>* current;
public:
ListIterator(const CLinkedList<T>& _list) : list(_list), current(list.last) {}
bool hasNext() const { return current != nullptr && current->next != list.last->next; }
void next() { current = current->next; }
T& value() const { return current->data; }
};
5. #endif
The program so far. It shows the basics of what it is supposed to do. You may use it as you wish.
//File testLL.cpp
/*! Test Driver for the file CLinkedList.h */
#include <iostream>
#include <string>
#include "Node.h"
#include "CLinkedList.h"
using namespace std;
//Function to insert an interger to the list in the ascending order
void intInsert(){}
//Functiion to remove an interger from the list
void intRemove(){}
//Function to print the list backwards
void printBackwards(){}
//Function to print out the menu choices,
//Created in order to make the main function more readable
//Function type: Export
void printOptions(){
cout<<"-------------------------"<<endl;
cout<<"I - Insert Interger"<<endl;
cout<<"R - Remove Interger"<<endl;
cout<<"F - Forward Print"<<endl;
cout<<"B - Backword Print"<<endl;
cout<<"E - Exit"<<endl;
cout<<"--------------------------"<<endl;
}
int main(){
string choice = "";
int interger = 0;
string exit = "False";
cout<<"This program is a test driver for the file CLinkedList.h"<<endl;
cout<<"Please select one of the following operations on a Circular Linked List"<<endl;
cout<<"------------------------------------------------------------"<<endl;
printOptions();
cout<<"Your Choice: ";
cin>>choice;
cout<<endl<<endl;
while (exit != "True"){
if (choice == "I" or choice == "i"){
cout<<"Iterger to insert: ";
cin>>interger;
6. intInsert();
cout<<endl<<"Inserting "<< interger << endl<<endl;
}
else if (choice == "R" or choice == "r"){
cout<<"Iterger to remove: ";
cin>>interger;
intRemove();
cout<<endl<<"Removing "<< interger << endl<<endl;
}
else if (choice == "F" or choice =="f"){
cout<<"Printing Forwards"<<endl<<endl;
}
else if (choice == "B" or choice == "b"){
cout<<"Printing Backwords"<< endl<<endl;
printBackwords();
}
else if (choice == "E" or choice == "e"){
cout<<"Exitting the program..."<<endl;
return 0;
}
else{
cout<<"Wrong input format"<<endl<<endl;}
cout<<"Please enter another operation: "<<endl;
printOptions();
cin>>choice;
cout<<endl;
}
}