Question

// I understand what I have to do on a fundamental level however I am having...

// I understand what I have to do on a fundamental level however I am having trouble just grasping how to implement them into the program. Explanation greatly appreciated!

The files section has a gutted List class for you. Just write all the methods. Easy peasy. Just remember that if your code never calls a template method, the compiler deletes it entirely. Make sure you test everything.

List's big three: Destructor, Copy, Assignment. Vanilla constructor is right - don't change it. PushFront: Add a new node as the new first piece of data.

Do NOT add anything before Head.

PopFront: Remove the first piece of data if there is one.

Front: Return the first piece of data.

PushBack: Add a new node as the new first piece of data. Do NOT add anything before Head.

PopBack: Remove the first piece of data if there is one.

Back: Return the first piece of data.

Size: How many things.

Clear: Remove all data, leave head and tail.

At: Return data at that position. 0 is first data

List.h

#pragma once

template <typename T>

class List

{

struct ListNode

{

ListNode()

{

mPrev = nullptr;

mNext = nullptr;

}

T mData;

ListNode* mPrev;

ListNode* mNext;

};

ListNode* mHead;

ListNode* mTail;

T mUndefined;

public:

List()

{

// Getting Head and Tail correct is not part of the Big 3. It is hella

mega required no matter what.

//mHead = nullptr;

//mTail = nullptr;// bleh. Full of crash.

mHead = new ListNode;

mTail = new ListNode;

mHead->mNext = mTail;

mHead->mPrev = nullptr;

mTail->mPrev = mHead;// This RULES. We always know we have 2 nodes, so

we never have to check for null.

mTail->mNext = nullptr;

}

List(const List& tOther)

{

}

List& operator = (const List& tRHS)

{

}

~List()

{

}

void PushFront(const T& tWhat)

{

}

void PopFront()

{

}

T& Front()

{

return mUndefined;

}

void PushBack(const T& tWhat)

{

}

void PopBack()

{

}

T& Back()

{

return mUndefined;

}

int Size() const

{

return 0;

}

void Clear()

{

}

T& At(int tWhere) const

{

}

};

/ ListStart.cpp : This file contains the 'main' function. Program execution begins

and ends there.

//

#include <iostream>

#include "List.h"

using namespace std;

int main()

{

List<int> tSomeInts;

tSomeInts.PushBack(14);// 14

tSomeInts.PushBack(1);// 14 1

tSomeInts.PushBack(54);// 14 1 54

tSomeInts.PushBack(25);// 14 1 54 25

List<int> tIntCopy = tSomeInts;// 14 1 54 25

tSomeInts.PopBack();// 14 1 54

cout << "This better say 4: " << tIntCopy.Size() << endl;

cout << "This better say 3: " << tSomeInts.Size() << endl;

cout << "This better say 14: " << tSomeInts.Front() << endl;

cout << "This better say 54: " << tSomeInts.Back() << endl;

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

List.h

==========================

#pragma once
template <typename T>
class List
{
   struct ListNode
   {
       ListNode()
       {
           mPrev = nullptr;
           mNext = nullptr;
       }
       T mData;
       ListNode* mPrev;
       ListNode* mNext;
   };
   ListNode* mHead;
   ListNode* mTail;
   T mUndefined; // why need this?
public:
   List()
   {
       // Getting Head and Tail correct is not part of the Big 3. It is hellamega required no matter what.
       //mHead = nullptr;
       //mTail = nullptr;// bleh. Full of crash.
       mHead = new ListNode;
       mTail = new ListNode;
       mHead->mNext = mTail;
       mHead->mPrev = nullptr;
       mTail->mPrev = mHead;// This RULES. We always know we have 2 nodes, so we never have to check for null.
       mTail->mNext = nullptr;
   }

   List(const List& tOther)
   {
       // use assignment operator for copy constructor
       *this = tOther;
   }

   List& operator = (const List& tRHS)
   {
       // initialize front and back
       mHead = new ListNode;
       mTail = new ListNode;
       mHead->mNext = mTail;
       mHead->mPrev = nullptr;
       mTail->mPrev = mHead;
       mTail->mNext = nullptr;
       // create iterator for RHS
       ListNode* itr = tRHS.mHead->mNext;
       // loop through RHS list and copy each node
       while (itr != tRHS.mTail) {
           // create a new node before tail
           ListNode* node = new ListNode;
           // copy data to new node
           node->mData = itr->mData;
           // link the node on left
           mTail->mPrev->mNext = node;
           node->mPrev = mTail->mPrev;
           // link the node on right
           node->mNext = mTail;
           mTail->mPrev = node;
           itr = itr->mNext;
       }
       // return the list
       return *this;
   }

   ~List()
   {
       ListNode* temp = mHead; // create iterator
       // remove each node from list
       while (mHead != mTail) {
           // move to next node and delete current node
           mHead = mHead->mNext;
           delete temp;
           temp = mHead;
       }
       // delete last node
       delete temp;
   }

   void PushFront(const T& tWhat)
   {
       // create new node
       ListNode* node = new ListNode;
       // get data
       node->mData = tWhat;
       // link the node on right
       mHead->mNext->mPrev = node;
       node->mNext = mHead->mNext;
       // link the node on left
       node->mPrev = mHead;
       mHead->mNext = node;
   }

   void PopFront()
   {
       // remove first node if it exist
       if (mHead != mTail) {
           ListNode* temp = mHead->mNext;
           // remove nodes link
           mHead->mNext->mNext->mPrev = mHead;
           mHead->mNext = mHead->mNext->mNext;
           delete temp;
       }
   }

   T& Front()
   {
       return mHead->mNext->mData;
   }

   void PushBack(const T& tWhat)
   {
       // create new node
       ListNode* node = new ListNode;
       // get data
       node->mData = tWhat;
       // link the node on left
       node->mPrev = mTail->mPrev;
       mTail->mPrev->mNext = node;
       // link the node on right
       node->mNext = mTail;
       mTail->mPrev = node;
   }

   void PopBack()
   {
       // remove first node if it exist
       if (mHead != mTail) {
           ListNode* temp = mTail->mPrev;
           // remove nodes link
           mTail->mPrev->mPrev->mNext = mTail;
           mTail->mPrev = mTail->mPrev->mPrev;
           delete temp;
       }
   }

   T& Back()
   {
       return mTail->mPrev->mData;
   }

   int Size() const
   {
       // create varaible to count nodes
       int count = 0;
       ListNode* itr = mHead->mNext; // create iterator
       // loop through list to count nodes
       while (itr != mTail) {
           count++;
           itr = itr->mNext;
       }
       return count;
   }

   void Clear()
   {
       ListNode* temp = mHead->mNext; // create iterator
       // remove each node from list
       while (temp != mTail) {
           // move to next node and delete current node
           mHead = mHead->mNext;
           delete temp;
           temp = mHead->mNext;
       }
       // delete last node
   }

   T& At(int tWhere) const
   {
       ListNode* itr = mHead->mNext; // create iterator
       // move to given index using for loop
       for (int i = 0; i < tWhere; i++) {
           itr = itr->mNext;
       }
       // return the element
       return itr->mData;
   }

};

=============================

ListStart.cpp

=============================

#include <iostream>
#include "List.h"
using namespace std;

int main()
{
   List<int> tSomeInts;
   tSomeInts.PushBack(14);// 14
   tSomeInts.PushBack(1);// 14 1
   tSomeInts.PushBack(54);// 14 1 54
   tSomeInts.PushBack(25);// 14 1 54 25
   tSomeInts.PushFront(18);//18 14 54 25
   cout << "This better say 18: " << tSomeInts.Front() << endl;
   tSomeInts.PopFront();// 14 1 54 25
   List<int> tIntCopy = tSomeInts;// 14 1 54 25
   tSomeInts.PopBack();// 14 1 54
   cout << "This better say 4: " << tIntCopy.Size() << endl;
   cout << "This better say 3: " << tSomeInts.Size() << endl;
   cout << "This better say 14: " << tSomeInts.Front() << endl;
   cout << "This better say 54: " << tSomeInts.Back() << endl;

   return 0;
}

let me know if you have any problem or doubt. thank you.

Add a comment
Know the answer?
Add Answer to:
// I understand what I have to do on a fundamental level however I am having...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C++, for the provided template linked list class create a derived class of it which...

    In C++, for the provided template linked list class create a derived class of it which adds the functionality to it to find the high and low value of any given data stored in the list. The derived class must be a template. LinkedList.h #pragma once #include <iostream> using namespace std; template <class T> class ListNode {    public:        T data;        ListNode<T>* next;        ListNode(T data)        {            this->data = data;...

  • C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you...

    C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...

  • C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of...

    C++ Assignment Project 1 - NodeList Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3, 1, 4, 5). Your function should not change l2and...

  • Implementing the vector class in the following code (It's C++, not Java). You just need to...

    Implementing the vector class in the following code (It's C++, not Java). You just need to implement all these methods: PushFront, PopFront, At, Erase, Insert, Clear, Reserve, Copy, Assign, and Destroy, please do not add any new properties. You cannot use std:: functions, && (unless being used for "AND"), or pass by reference. Your solutions must conform to the Big O notations next to each method. In the following code, I am going to match the names that STL uses...

  • What is the specific answer for 1. and 2. Thanks! Add a new method, find, to...

    What is the specific answer for 1. and 2. Thanks! Add a new method, find, to class SinglyLinkedList (defined here) that takes as input a “data” value and returns a pointer to a node. If the input data is present in the linked list, the returned pointer should point to that node; if not, the returned pointer is nullptr. Write the (single line) method declaration/specification. Write the method definition/implementation. Test by running the main() function below and capture the console...

  • I have the following c++ data structures assignment: Copy Constructors, Destructors, and Assignment Operators An understanding...

    I have the following c++ data structures assignment: Copy Constructors, Destructors, and Assignment Operators An understanding of how to implement copy constructors, destructors, and assignment operators is essential when working with data structures using dynamic memory allocation. Failure to implement these methods correctly can and probably will result in memory leaks. In this project, you are provided with a working implementation of a doubly-linked list in which the copy constructor, destructor, and assignment operator methods are not complete. To complete...

  • For the LinkedList class, create a getter and setter for the private member 'name', constructing your...

    For the LinkedList class, create a getter and setter for the private member 'name', constructing your definitions based upon the following declarations respectively: std::string get_name() const; and void set_name(std::string); In the Main.cpp file, let's test your getter and setter for the LinkedLIst private member 'name'. In the main function, add the following lines of code: cout << ll.get_name() << endl; ll.make_test_list(); ll.set_name("My List"); cout << ll.get_name() << endl; Output should be: Test List My List Compile and run your code;...

  • Fill the code in list.cpp app.cpp: #include "list.hpp" int main() {         LinkedList aList; bool success =...

    Fill the code in list.cpp app.cpp: #include "list.hpp" int main() {         LinkedList aList; bool success = false;         aList.evensFrontOddsEnd(3);         aList.evensFrontOddsEnd(10);         aList.evensFrontOddsEnd(6);         aList.evensFrontOddsEnd(9);         aList.evensFrontOddsEnd(17);         aList.evensFrontOddsEnd(12);         aList.printForward(); // output should be: 12 6 10 3 9 17         aList.printBackward(); // output should be: 17 9 3 10 6 12         success = aList.remove(17); if(success) cout << "17 Successfully Removed." << endl;         success = aList.remove(10); if(success) cout << "10 Successfully Removed." << endl; success = aList.remove(7); // 7 is not in the list, so success should...

  • Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should h...

    Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...

  • Design and implement your own linked list class to hold a sorted list of integers in...

    Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT