Question: Hello, I need to make a program in c++ that makes a linked list and be able to add nodes to the head and tail
Hello, I need to make a program in c++ that makes a linked list and be able to add nodes to the head and tail of the list.
this is my main.cpp
#include "LinkedList.h" #include #include #include using namespace std;
void TestAddHead(); void TestAddTail();
int main() { int testNum; cin >> testNum; if (testNum == 1) TestAddHead(); else if (testNum == 2) TestAddTail();
system("pause");
return 0; }
/*======== TESTING FUNCTIONS ========*/ void TestAddHead() { cout << "=====Testing AddHead() functionality====" << endl; LinkedList data; for (int i = 0; i < 12; i += 2) data.AddHead(i); cout << "Node count: " << data.NodeCount() << endl; cout << "Print list forward:" << endl; data.PrintForward(); cout << "Print list in reverse:" << endl; data.PrintReverse(); }
void TestAddTail() { cout << "=====Testing AddTail() functionality====" << endl; LinkedList data; for (int i = 0; i <= 21; i += 3) data.AddTail(i); cout << "Node count: " << data.NodeCount() << endl; cout << "Print list forward:" << endl; data.PrintForward(); cout << "Print list in reverse:" << endl; data.PrintReverse(); }
and this is what I have in LinkedList.h so far
#pragma once #include #include #include #include #include
using namespace std;
template class LinkedList { public: struct Node { T data_; Node* next; Node* previous; };
void PrintForward() const; void PrintReverse() const; unsigned int NodeCount() const; void AddHead(const T &data); void AddTail(const T &data); LinkedList(); LinkedList(const LinkedList &list); ~LinkedList();
private: Node* head = new Node; Node* tail = new Node; unsigned int count; };
template LinkedList::LinkedList() { head = nullptr; tail = nullptr; count = 0; }
template LinkedList::LinkedList(const LinkedList &list) { this->head = list.head; this->tail = list.tail; this->count = list.count;
}
template LinkedList::~LinkedList() { delete head; delete tail; }
I need AddHead, AddTail, Print Forward, and PrintReverse to work so that the program outputs
=====Testing AddHead() functionality==== Node count: 6 Print list forward: 10 8 6 4 2 0 Print list in reverse: 0 2 4 6 8 10
when I use AddHead and to output:
=====Testing AddTail() functionality==== Node count: 8 Print list forward: 0 3 6 9 12 15 18 21 Print list in reverse: 21 18 15 12 9 6 3 0
when I use AddTail
NOTE: I am also not sure if my constructor, copy constructor and destructor are ok. can anyone help me please?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
