in C++ Code
Part I - The Essentials You will create a class to manage a doubly linked list of integers. Your class should only have public member functions and member variables only accessible through accessors and mutators. Each node in the list should be represented by a class or struct containing the data element, a pointer to the previous element, and a pointer to the next element. The list class should contain pointers to the head and tail of the list. Your class should implement the following public member functions: void push(int data); - appends a new node on the end of the list void void pop(); - removes the last element of the list int size(); - returns the number of elements in the list void print(); - prints all elements in the list to the console int& at(int idx); - returns a reference to the data at the requested index, "idx" Part II - Linked Lists Specialty Functions Extend the functionality of your class with the following functions: void insert(int data, int pos); - inserts a new node containing data at the position "pos" in the list. All elements at that position are then shifted forward. (example: you have the following list: {5, 7, 2}. You call list.insert(3, 1) to put a 3 at position 1 (the second element, remember computers count starting at O). Your list should then contain: {5, 3, 7, 2}) void remove(int pos); - removes the element at the specified position
Part III - The Final Touches Add a destructor to your class that cleans up any data allocated via "new" to prevent memory leaks. Add copy constructors and override the copy assignment operator to prevent shallow copies. Extend your class to a "template" class so that you can use any data type, rather than just integers to hold data. How to Test Use the following main.cpp file contents to test your class: #include <iostream> #include "doublylinkedlist.h" using namespace std; void test() { DoublyLinkedList dll; dll.push(1); dll.push(2); dll.push(3); DoublyLinkedList dllCopy = dll; d11Copy.push(4); d11Copy.remove(1); d11Copy.remove(0); dllCopy.insert(5, 1); dll.print(); d11Copy.print(); dll = dllCopy; dll.print(); } int main() { test(); return 0; } Note that your class will need to exist in a header file called "DoublyLinkedList.h" and your class will need to be called "DoublyLinked List" to use this code to test. If you name your class something different, you will need to make the appropriate changes here.
Requirements • The code should be well formatted with proper indentation, variable names, and function structure • The Node Class/Struct is properly defined • The node contains members for the data, a pointer to the next node, and a pointer to the previous node • The Doubly Linked List Class is properly defined • The class manages pointers to the head, tail, and contains all members and functions described in the description section • The push function properly appends a new node to the end of the list • The pop function properly removes a node from the end of the list • The size function returns the number of elements in the list • The insert function properly adds a new node at the specified index within the list, according to the behavior described in the description • The remove function properly removes a node at the specified index • The class should properly handle "garbage collection" • when the class goes out of scope, no data created on the heap associated with this class should still be allocated • The class should properly handle "shallow copies" • when the class is copied through a copy assignment or copy constructor, it should properly re-allocate and copy data for the new linked list class (Bonus) The class should be modified to handle all types of data (other than just int) through the use of a template class • All data "int" instances in the class should be replaced with a templated variable
Part I - The Essentials You will create a class to manage a doubly linked list of integers. Your class should only have
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
Part I - The Essentials You will create a class to manage a doubly linked list of integers. Your class should only have
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!