// File: DDL.h // Student name: // Date: // Description: Definition of a doubly-
ID: 3910074 • Letter: #
Question
// File: DDL.h
// Student name:
// Date:
// Description: Definition of a doubly-linked-list class using template
#ifndef _DLL_H_
#define _DLL_H_
#include <cstdlib>
#include <stdexcept>
#include <string>
#include <iostream>
using namespace std;
// template class for doubly-linked list node
template <class T>
class Node
{
public:
T data;
//string data;
Node<T>* prev;
Node<T>* next;
// default constructor
//template <class T>
Node(T value)
{
data = value;
prev = NULL;
next = NULL;
}
};
// DLinkedList class definition
template <class T>
class DLinkedList
{
private:
// DLinkedList private members
int size; // number of items stored in list
Node<T>* front; // references to the front
Node<T>* back; // and back of the list
// helper function for deep copy
// Used by copy constructor and operator=
void CopyList(const DLinkedList& ll);
// helper function for deep delete
// Used by destructor and copy/assignment
void DeleteList();
public:
// default constructor
DLinkedList();
// constructor with array argument
DLinkedList(T array[],int size);
// copy constructor, performs deep copy of list elements
DLinkedList(const DLinkedList& ll);
// destructor
~DLinkedList();
// Print the element data of the list
// Output "The list is empty" if there is no element
void printDLL();
//MUTATORS
// Inserts an item at the front of the list
// POST: List contains item at position 0
// PARAM: item = item to be inserted
void InsertFront(T item);
// Inserts an item at the back of the list
// POST: List contains item at back
// PARAM: item = item to be inserted
void InsertBack(T item);
// Inserts an item in position p (0-indexed)
// Throws exception for invalid index
// PRE: 0 <= p <= size
// POST: List contains item at position p
// PARAM: item = item to be inserted, p = position where item will be inserted
void InsertAt(T item, int p);
// Removes and returns an item from position p (0-indexed)
// Throws exception if list is empty or index invalid
// PRE: 0 <= p < size
// POST: Item is removed from list
// PARAM: p = position from where item will be removed
T RemoveAt(int p);
// Removes duplicates from the list, preserving existing order of remaining items.
// The first occurrence of any duplicate (relative to the front of the list)
// is the one which remains.
// We have not yet learned about efficiency so you may implement this in any way
// as long as the resulting list satisfies the requirement above.
// PRE:
// POST: List contains no duplicates, front and back point to the appropriate nodes
// PARAM:
void RemoveDuplicates();
// ACCESSORS
// Returns size of list
int Size() const;
// Returns whether the list is empty
bool IsEmpty() const;
// Returns existence of item
bool Contains(T item) const;
// Returns item at index (0-indexed)
// Throws exception for invalid index
T ElementAt(int p) const;
// OVERLOADED OPERATORS
// overloaded assignment operator
// must work in the following cases:
// list2 = list1 -> general case
// list2 = list2 -> should do nothing
DLinkedList& operator=(const DLinkedList& ll);
};
#include "DLL.cpp"
#endif
------------------------------------------------------------------------
// File: DLL_test.cpp
// Studnet name:
// Date:
// Description: testing doubly-linked-list class
#include "DLL.h"
int main(){
//In this section you test integer type
//In this section you test float type
//In this section you test string type
return 0;
}
Explanation / Answer
here is your files : -------------->>>>>>>>>
DLL.h : --------->>>>>>>
#ifndef _DLL_H_
#define _DLL_H_
#include <cstdlib>
#include <stdexcept>
#include <string>
#include <iostream>
using namespace std;
// template class for doubly-linked list node
template <class T>
class Node
{
public:
T data;
//string data;
Node<T>* prev;
Node<T>* next;
// default constructor
//template <class T>
Node(T value)
{
data = value;
prev = NULL;
next = NULL;
}
};
// DLinkedList class definition
template <class T>
class DLinkedList
{
private:
// DLinkedList private members
int size; // number of items stored in list
Node<T>* front; // references to the front
Node<T>* back; // and back of the list
// helper function for deep copy
// Used by copy constructor and operator=
void CopyList(const DLinkedList& ll);
// helper function for deep delete
// Used by destructor and copy/assignment
void DeleteList();
public:
// default constructor
DLinkedList();
// constructor with array argument
DLinkedList(T array[],int size);
// copy constructor, performs deep copy of list elements
DLinkedList(const DLinkedList<T>& ll);
// destructor
~DLinkedList();
// Print the element data of the list
// Output "The list is empty" if there is no element
void printDLL();
//MUTATORS
// Inserts an item at the front of the list
// POST: List contains item at position 0
// PARAM: item = item to be inserted
void InsertFront(T item);
// Inserts an item at the back of the list
// POST: List contains item at back
// PARAM: item = item to be inserted
void InsertBack(T item);
// Inserts an item in position p (0-indexed)
// Throws exception for invalid index
// PRE: 0 <= p <= size
// POST: List contains item at position p
// PARAM: item = item to be inserted, p = position where item will be inserted
void InsertAt(T item, int p);
// Removes and returns an item from position p (0-indexed)
// Throws exception if list is empty or index invalid
// PRE: 0 <= p < size
// POST: Item is removed from list
// PARAM: p = position from where item will be removed
T RemoveAt(int p);
// Removes duplicates from the list, preserving existing order of remaining items.
// The first occurrence of any duplicate (relative to the front of the list)
// is the one which remains.
// We have not yet learned about efficiency so you may implement this in any way
// as long as the resulting list satisfies the requirement above.
// PRE:
// POST: List contains no duplicates, front and back point to the appropriate nodes
// PARAM:
void RemoveDuplicates();
// ACCESSORS
// Returns size of list
int Size() const;
// Returns whether the list is empty
bool IsEmpty() const;
// Returns existence of item
bool Contains(T item) const;
// Returns item at index (0-indexed)
// Throws exception for invalid index
T ElementAt(int p) const;
// OVERLOADED OPERATORS
// overloaded assignment operator
// must work in the following cases:
// list2 = list1 -> general case
// list2 = list2 -> should do nothing
DLinkedList<T>& operator=(const DLinkedList<T>& ll);
};
#include "DLL.cpp"
#endif
DLL.cpp : ---------->>>>>>>
#ifndef _DDD_LL
#define _DDD_LL
#include "DLL.h"
template <class T>
DLinkedList<T>::DLinkedList()
{
front= nullptr;
back = nullptr;
size = 0;
}
template<class T>
DLinkedList<T>::DLinkedList(const DLinkedList<T> &ll){
*this = ll;
}
template<class T>
DLinkedList<T>& DLinkedList<T>::operator=(const DLinkedList<T> &ll){
CopyList(ll);
return *this;
}
template<class T>
void DLinkedList<T>::CopyList(const DLinkedList<T> &ll){
if(size != 0){
DeleteList();
}
Node<T> *cur = ll.front;
while(cur != NULL){
InsertBack(cur->data);
cur = cur->next;
}
}
template<class T>
void DLinkedList<T>::DeleteList(){
if(IsEmpty()){
return;
}
int n = size;
for(int i = 0;i<n;i++){
RemoveAt(0);
}
front = NULL;
back = NULL;
size = 0;
}
template<class T>
void DLinkedList<T>::InsertBack(T item){
if(front == NULL){
front = new Node<T>(item);
back = front;
size = 1;
return;
}
back->next = new Node<T>(item);
back->next->prev = back;
back = back->next;
size++;
}
template<class T>
T DLinkedList<T>::RemoveAt(int p){
if(IsEmpty() || p >= size || p < 0){
return T{};
}
Node<T> *cur = front;
for(int i = 0;i<p;i++){
cur = cur->next;
}
T d = cur->data;
if(cur->prev != NULL)
cur->prev->next = cur->next;
if(cur->next != NULL)
cur->next->prev = cur->prev;
if(p == 0){
front = cur->next;
}
size--;
delete cur;
return d;
}
template<class T>
DLinkedList<T>::DLinkedList(T array[],int size){
for(int i = 0;i<size;i++){
InsertBack(array[i]);
}
}
template<class T>
T DLinkedList<T>::ElementAt(int p)const{
if(IsEmpty() || p >= size || p < 0){
return T{};
}
Node<T> *cur = front;
for(int i = 0;i<p;i++){
cur = cur->next;
}
T d = cur->data;
return d;
}
template<class T>
void DLinkedList<T>::InsertAt(T item,int p){
if(IsEmpty() || p >= size || p < 0){
return;
}
Node<T> *cur = front;
for(int i = 0;i<p;i++){
cur = cur->next;
}
Node<T> *temp = new Node<T>(item);
temp->next = cur;
temp->prev = cur->prev;
if(temp->prev != NULL)
temp->prev->next = temp;
cur->prev = temp;
if(p == 0){
front = temp;
}
size++;
}
template<class T>
void DLinkedList<T>::InsertFront(T item){
if(front == NULL){
front = new Node<T>(item);
back = front;
size = 1;
return;
}
InsertAt(item,0);
}
template<class T>
bool DLinkedList<T>::IsEmpty()const{
return size == 0 && front == NULL;
}
template<class T>
void DLinkedList<T>::printDLL(){
if(IsEmpty()){
return;
}
Node<T> *cur = front;
cout<<"[";
while(cur != NULL){
cout<<cur->data;
if(cur->next != NULL){
cout<<", ";
}
cur = cur->next;
}
cout<<"]";
}
template<class T>
int DLinkedList<T>::Size()const{
return size;
}
template<class T>
DLinkedList<T>::~DLinkedList(){
DeleteList();
}
template<class T>
bool DLinkedList<T>::Contains(T item)const{
if(IsEmpty()){
return false;
}
Node<T> *cur = front;
while(cur != NULL){
if(cur->data == item){
return true;
}
cur = cur->next;
}
return false;
}
template<class T>
void DLinkedList<T>::RemoveDuplicates(){
if(IsEmpty()){
return;
}
Node<T> *cN = front;
Node<T> *cur = NULL;
while(cN != NULL){
cur = cN->next;
while(cur != NULL){
if(cN->data == cur->data){
if(cur->next != NULL)
cur->next->prev = cur->prev;
if(cur->prev != NULL)
cur->prev->next = cur->next;
size--;
Node<T> *temp = cur;
cur = cur->next;
delete temp;
continue;
}
cur = cur->next;
}
cN = cN->next;
}
}
#endif
Main.cpp : --------->>>>>>>>
#include "DLL.h"
int main(){
//Integer Type
DLinkedList<int> din;
din.InsertBack(23);
din.InsertBack(12);
din.InsertFront(45);
din.InsertBack(23);
din.InsertBack(45);
cout<<" List = ";
din.printDLL();
din.RemoveDuplicates();
cout<<" Removed Duplicates List : ";
din.printDLL();
//Float Type
DLinkedList<float> din1;
din1.InsertBack(23.24);
din1.InsertBack(12.87);
din1.InsertFront(45.90);
din1.InsertBack(23.14);
din1.InsertBack(45.90);
din1.InsertAt(23.24,3);
cout<<" List = ";
din1.printDLL();
din1.RemoveDuplicates();
cout<<" Removed Duplicates List : ";
din1.printDLL();
//string Type
DLinkedList<string> din2;
din2.InsertBack("Dkp");
din2.InsertBack("Rahul");
din2.InsertFront("Neha");
din2.InsertBack("Prabhu");
din2.InsertBack("Rahul");
din2.InsertAt("Neha",4);
cout<<" List = ";
din2.printDLL();
din2.RemoveDuplicates();
cout<<" Removed Duplicates List : ";
din2.printDLL();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.