Question

C++ Implement a templated class list and listnode. You may add methods/functions as you see fit....

C++

Implement a templated class list and listnode. You may add methods/functions as you see fit. Test these classes. I have left all of the implementation as an exercise for you.

template< class NODETYPE > class List;  // forward declaration

template<class NODETYPE>

class ListNode

{

   friend class List< NODETYPE >; // make List a friend

public:

   ListNode( const NODETYPE &newData);  // copy constructor

   NODETYPE getData() const;      // return data in the node

private:

   NODETYPE data;                 // data

   ListNode< NODETYPE > *nextPtr; // next node in the list

};

template< class NODETYPE >

class List

{

public:

   List();      // constructor

   ~List();     // destructor

   void insertAtFront( const NODETYPE &newData );

   void insertAtBack( const NODETYPE &newData );

   bool removeFromFront( NODETYPE &removedData );

   bool removeFromBack( NODETYPE &removedData );

   bool isEmpty() const;

   void print() const;

private:

   ListNode< NODETYPE > *firstPtr;  // pointer to first node

   ListNode< NODETYPE > *lastPtr;   // pointer to last node

   // Utility function to allocate a new node

   ListNode< NODETYPE > *getNewNode( const NODETYPE &newData );

};

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

// C++ program to implement a templated class list and listnode.

#include <iostream>

using namespace std;

template< class NODETYPE > class List; // forward declaration

template<class NODETYPE>

class ListNode

{

   friend class List< NODETYPE >; // make List a friend

public:

   ListNode( const NODETYPE &newData); // copy constructor

   NODETYPE getData() const;      // return data in the node

private:

   NODETYPE data;                 // data

   ListNode< NODETYPE > *nextPtr; // next node in the list

};

template <class NODETYPE>

ListNode<NODETYPE>::ListNode(const NODETYPE &newData)

{

               data = newData;

               nextPtr = nullptr;

}

template <class NODETYPE>

NODETYPE ListNode<NODETYPE>::getData() const

{

               return data;

}

template< class NODETYPE >

class List

{

public:

   List();      // constructor

   ~List();     // destructor

   void insertAtFront( const NODETYPE &newData );

   void insertAtBack( const NODETYPE &newData );

   bool removeFromFront( NODETYPE &removedData );

   bool removeFromBack( NODETYPE &removedData );

   bool isEmpty() const;

   void print() const;

private:

   ListNode< NODETYPE > *firstPtr; // pointer to first node

   ListNode< NODETYPE > *lastPtr;   // pointer to last node

   // Utility function to allocate a new node

   ListNode< NODETYPE > *getNewNode( const NODETYPE &newData );

};

template <class NODETYPE>

List<NODETYPE>::List()

{

               firstPtr = nullptr;

               lastPtr = nullptr;

}

template <class NODETYPE>

List<NODETYPE>::~List()

{

               ListNode<NODETYPE> *curr;

               while(firstPtr != nullptr)

               {

                              curr = firstPtr;

                              firstPtr = firstPtr->nextPtr;

                              delete curr;

               }

               firstPtr = nullptr;

               lastPtr = nullptr;

}

template<class NODETYPE>

void List<NODETYPE>:: insertAtFront( const NODETYPE &newData )

{

               if(firstPtr == nullptr)

               {

                              firstPtr = getNewNode(newData);

                              lastPtr = firstPtr;

               }else{

                              ListNode<NODETYPE> *newNode = getNewNode(newData);

                              newNode->nextPtr = firstPtr;

                              firstPtr=newNode;

               }

}

template<class NODETYPE>

void List<NODETYPE>:: insertAtBack( const NODETYPE &newData )

{

               if(firstPtr == nullptr)

               {

                              firstPtr = getNewNode(newData);

                              lastPtr = firstPtr;

               }else{

                              ListNode<NODETYPE> *newNode = getNewNode(newData);

                              lastPtr->nextPtr = newNode;

                              lastPtr=newNode;

               }

}

template<class NODETYPE>

bool List<NODETYPE>:: removeFromFront( NODETYPE &removedData )

{

               if(firstPtr == nullptr)

               {

                              return false;

               }else if(firstPtr->nextPtr == nullptr)

               {

                              removedData = firstPtr->getData();

                              delete firstPtr;

                              firstPtr = nullptr;

                              lastPtr=nullptr;

                              return true;

               }

               else{

                              removedData = firstPtr->getData();

                              ListNode<NODETYPE> *curr = firstPtr;

                              firstPtr = firstPtr->nextPtr;

                              delete curr;

                              return true;

               }

}

template<class NODETYPE>

bool List<NODETYPE>:: removeFromBack( NODETYPE &removedData )

{

               if(firstPtr == nullptr)

               {

                              return false;

               }else if(firstPtr->nextPtr == nullptr)

               {

                              removedData = firstPtr->getData();

                              delete firstPtr;

                              firstPtr = nullptr;

                              lastPtr=nullptr;

                              return true;

               }

               else{

                              removedData = lastPtr->getData();

                              ListNode<NODETYPE> *curr = firstPtr;

                              while(curr->nextPtr != lastPtr)

                                             curr = curr->nextPtr;

                              lastPtr = curr;

                              curr = curr->nextPtr;

                              lastPtr->nextPtr = nullptr;

                              delete curr;

                              return true;

               }

}

template<class NODETYPE>

bool List<NODETYPE> ::isEmpty() const

{

               return(firstPtr == nullptr);

}

template<class NODETYPE>

void List<NODETYPE>::print() const

{

               if(isEmpty())

                              cout<<" List is Empty"<<endl;

               else{

                              cout<<" List contents : ";

                              ListNode<NODETYPE> *curr = firstPtr;

                              while(curr->nextPtr != lastPtr)

                              {

                                             cout<<curr->getData()<<" -> ";

                                             curr = curr->nextPtr;

                              }

                              cout<<curr->getData()<<" -> "<<lastPtr->getData()<<endl;

               }

}

template<class NODETYPE>

ListNode< NODETYPE > * List<NODETYPE>::getNewNode(const NODETYPE &newData)

{

               return(new ListNode<NODETYPE>(newData));

}

int main() {

               List<int> intList;

               for(int i=0;i<5;i++)

                              intList.insertAtFront(i);

               intList.print();

               for(int i=0;i<5;i++)

                              intList.insertAtBack(i);

               intList.print();

               int item;

               for(int i=0;i<5;i++)

               {

                              if(intList.removeFromFront(item))

                                             cout<<" Item removed : "<<item<<endl;

               }

               intList.print();

               for(int i=0;i<5;i++)

               {

                              if(intList.removeFromBack(item))

                                             cout<<" Item removed : "<<item<<endl;

               }

               intList.print();

               return 0;

}

//end of program

Output:

List contents:4-> 3 - 2-1->0 List contents: 4- 3 - 2-1 ->0-> 0- 1 -2->3-> 4 Item removed4 Item removed3 Item removed 2 Item removed 1 Item removed0 List contents:0- 1- 2-3- 4 Item removed4 Item removed3 Item removed 2 Item removed 1 Item removed0 List is Empty

Add a comment
Know the answer?
Add Answer to:
C++ Implement a templated class list and listnode. You may add methods/functions as you see fit....
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
  • 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...

  • Introduction In this lab, you are supposed to implement a graph class with the data structure...

    Introduction In this lab, you are supposed to implement a graph class with the data structure implemented before like linked list and queue. graph The class graph contains three member variables: linkedList *adjacentVertices; //an array of linked list. For a vertice i, adjacentVertices[i] stores the linked list that contains all other vertices connected to vertice i. int numVertices; //The number of vertices in the graph. int maxNumVertices; //The maximum number of vertices the graph can hold. Following public methods are...

  • C++ Implement a class template for a LinkedList.(doubly linked). Also, your class will have a tailPtr...

    C++ Implement a class template for a LinkedList.(doubly linked). Also, your class will have a tailPtr in addition to a headPtr along with methods to get, set, insert and remove values at either end of the list. Call these getFirst, getLast, setFirst, setLast, insertFirst, insertLast, removeFirst, removeLast. Don't forget, you also need a copy constructor and destructor plus getLength, isEmpty and clear methods. Overload the stream insertion operator as a friend function which outputs the list in format { 1,...

  • C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the...

    C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and then implement the new operations below. The class should have the following additional class methods: • A reverse method: this method will reverse the order of the doubly linked list. This method takes no parameters,...

  • IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as...

    IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as well as make sure the Big 3 are defined. IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined...

  • 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...

  • This is a c++ class utilizing class templates and linked lists and Nodes. I need to...

    This is a c++ class utilizing class templates and linked lists and Nodes. I need to implement the following member function(s) to LinkedBag.cpp. Node.hpp/cpp should be fine but if you feel like there needs to be a change for compilation or testing, feel free to do so but make sure to comment on why it was done. In this case, I need to join the original items with the user items(a_bag). So if the original has {1,2,3} and a_bag has...

  • Please use the linked approach implement the BST ADT, implement all the functions in the BSTree.cpp....

    Please use the linked approach implement the BST ADT, implement all the functions in the BSTree.cpp. Add the ouput of the implementation. use recursive functions to traverse the tree - read the implementation notes on using helper functions. Please use C++ programming ////////////////////////////////////////////////////////////// #include "BSTree.h" template <typename DataType, class KeyType> BSTree<DataType, KeyType>::BSTreeNode::BSTreeNode ( const DataType &nodeDataItem, BSTreeNode *leftPtr, BSTreeNode *rightPtr ) { } template < typename DataType, class KeyType > BSTree<DataType, KeyType>::BSTree () {    root = 0; } template...

  • Are based on the following Queue class code segment class QueueFull {/* Empty exception class */};...

    Are based on the following Queue class code segment class QueueFull {/* Empty exception class */}; Class Queue Empty {/* Empty exception class */}; struct Node//Node structure int data;//Holds an integer Node* next;//Pointer to next node in the queue}; Class Queue//Linked node implementation of Queue ADT {Private: Node* front;//Pointer to front node of queue Node* rear;//pointer to last node of queue Public: Queue ()://default constructor initializes queue to be empty -Queue ();//Deallocates all nodes in the queue Void Add (int...

  • // thanks for helping // C++ homework // The homework is to complete below in the...

    // thanks for helping // C++ homework // The homework is to complete below in the stack.h : // 1. the copy constructor // 2. the assignment operator // 3. the destructor // 4. Write a test program (mytest.cpp) to test copy and assignment // 5. Verify destructor by running the test program in Valgrind // This is the main.cpp #include <iostream> #include "stack.h" using namespace std; int main() { Stack<int> intStack; cout << "\nPush integers on stack and dump...

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
ADVERTISEMENT