C++ (please use comment on each line of code to shows what does it do and what i
ID: 3889495 • Letter: C
Question
C++ (please use comment on each line of code to shows what does it do and what its' mean) Thank you!
Project 2 is an individual assignment that focuses on object-oriented programming and lists. The list will be a doubly-linked list and it will be implemented using nodes and pointers. The data contained in the list will objects from a class.
Project data: You will choose the following class to define: Food. You may also choose a sub-class of one of the previous choices. Your class code will include a .h class interface file and a .cpp class definition file. You will choose the data attributes of your class.
Project data structure: Additionally, you will create classes for a doubly-linked list that will manage a collection of the objects from section (a). The list will use dynamically allocated nodes.
For a user to work with your data and list, you should write a fully functional menu with the following options:
Adding to the front of the list
Adding to the back of the list
Searching and displaying a specific item in the list
Edit a specified item from the list
Removing an identified item from the list
Displaying the entire list
Ending the program
The following files are required:
The class driver similar to WorkingWithBooks.cpp (c).
The class declaration similar to Book.h (a).
The class implementation file similar to Book.cpp (a).
A header file for the node class similar to Node.h (b).
A header file for the list similar to SinglyLinkedList.h (b).
A class implementation file for the list class similar to SinglyLinkedList.cpp (b).
Additionally follow all these project instructions:
Your project folder name should contain your last name in it.
Your node file should contain your class name in it (e.g., HobbyHode.h)
Your list file name should contain your class name in it (e.g., HobbyList.h and HobbyList.cpp)
Your program should run. Comment out any part of your file that does not work and briefly explain why you think it does not work.
Submit a .zip zipped project folder (not a .rar folder) which will include all the project’s files.
Grading [100 points total]:
Code should be documented and use meaningful identifiers 10 points
Code should be well-structured and easy to read 10 points
Files named properly and project folder zipped up properly 10 points
Adding to the doubly-linked list 15 points
Removing from the list 15 points
Searching the list 15 points
Editing the list 15 points
Overall running of the program
Explanation / Answer
Notes:
I think you've mixed up doubly linked list questions with doubly linked list questions. Most part of question speaks of doubly liked list, so I am gonna go with doubly linked list.
Code:
Dlist.h
====
#ifndef DLIST_H_
#define DLIST_H_
#include <iostream>
#include "RjExceptions.cpp"
template <typename T>
struct Node {
Node<T> *prev;
Node<T> *next;
T val;
};
/**
* Creates a doubly linked list with a **head** and a **last**. **head** will be
* used to store the address of the first node ever created and the latest will
* be stored in the last.
*/
template <typename T>
class Dlist {
// This will store the first node's address.
Node<T> *head;
// This is going to hold the latest node's address.
Node<T> *last;
public:
// CONSTRUCTOR
Dlist();
// DESTRUCTOR
~Dlist();
// Adds a val to list.
void add(T val);
// Add object at given index.
void add(int indx, T val);
// Add to start.
void add_front(T val);
// Iterate.
void iterate();
// Reverse iterate.
void iterate_reverse();
// Get the element at index.
T at(int indx);
// Add object at given index.
void update(int indx, T val);
// Delete
void remove(int indx);
void pop_front();
void pop_back();
};
#endif /* DLIST_H_ */
Dlist.cpp
====
#include "Dlist.h"
/**
* Constructor function
* This function Initializes head and last to NULL.
*/
template <typename T>
Dlist<T>::Dlist() : head{ NULL }, last{ NULL }
{};
/**
* Destructor function
* This function Initializes head and last to NULL.
*/
template <typename T>
Dlist<T>::~Dlist()
{};
/**
* Adds a val to the list.
* @param { T } val - val of the object to add.
*/
template <typename T>
void Dlist<T>::add(T val) {
// If it is the first node
// Just normal initializing does not produce new objects every time the method is
// called.
Node<T> *nd1 = new Node<T>;
nd1->next = NULL;
if (head == NULL) {
nd1->prev = NULL;
head = nd1;
}
else {
nd1->prev = last;
last->next = nd1;
}
nd1->val = val;
last = nd1;
};
/**
* @Overload
* Add given object at a given index.
* @param { int } indx - Index to append at.
* @param { T } val - Object to append.
*/
template <typename T>
void Dlist<T>::add(int indx, T val) {
Node<T> *temp{ head };
int i{ 0 };
do {
if (i == indx) {
Node<T> *nd1 = new Node<T>;
// Copy Previous one's.
nd1->prev = temp->prev;
nd1->next = temp;
nd1->val = val;
// Setting nd1 on the left and right side blocks.
temp->prev->next = nd1;
temp->prev = nd1;
}
++i;
temp = temp->next;
} while(temp != NULL);
}
/**
* Iterates over the list from start to end and prints to the screen.
*/
template <typename T>
void Dlist<T>::iterate() {
if (head == NULL) {
list_un_inited err;
throw err;
}
// Iterate through the list until you hit a NULL.
Node<T> *temp{ head };
do {
std::cout << temp->val << std::endl;
temp = temp->next;
} while(temp != NULL);
};
/**
* Iterates over the list from end to start and prints to the screen.
*/
template <typename T>
void Dlist<T>::iterate_reverse() {
if (last == NULL) {
list_un_inited err;
throw err;
}
// Iterate until you hit NULL.
Node<T> *temp{ last };
do {
std::cout << temp->val << std::endl;
temp = temp->prev;
} while(temp != NULL);
};
/**
* Returns the object at the given index.
* @param { int } indx - Index of the object to get.
*/
template <typename T>
T Dlist<T>::at(int indx) {
Node<T> *temp{ head };
int i{ 0 };
do {
if (i == indx) {
return temp->val;
}
++i;
temp = temp->next;
} while(temp != NULL);
out_of_bounds_index err;
throw err;
};
/**
* Adds an object to the start of an array.
* @param { T } val - Object to add.
*/
template <typename T>
void Dlist<T>::add_front(T val) {
// Create a new node.
Node<T> *nd1 = new Node<T>;
// Set its prev to NULL.
nd1->prev = NULL;
nd1->next = head;
nd1->val = val;
// Change others
head->prev = nd1;
head = nd1;
};
/**
* Adds an object to the start of an array.
* @param { int } indx - Index of object to change.
* @param { T } val - Object to change index to.
*/
template <typename T>
void Dlist<T>::update(int indx, T val) {
Node<T> *temp{ head };
int i{ 0 };
do {
if (i == indx) {
temp->val = val;
}
++i;
temp = temp->next;
} while(temp != NULL);
};
/**
* Deletes an object at given index
* @param { int } indx - Index of object to remove.
*/
template <typename T>
void Dlist<T>::remove(int indx) {
Node<T> *temp{ head };
int i{ 0 };
do {
if (i == indx) {
temp->next->prev = temp->prev;
temp->prev->next = temp->next;
}
++i;
temp = temp->next;
} while(temp != NULL);
};
/**
* Deletes the first element of the array.
*/
template <typename T>
void Dlist<T>::pop_front() {
head->next->prev = NULL;
head = head->next;
};
/**
* Deletes the last element of an array.
*/
template <typename T>
void Dlist<T>::pop_back() {
last->prev->next = NULL;
last = last->prev;
};
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.