Homework Help Question & Answers

C++ program: Convert the classes to template classes #include <iostream> #include <string> using namespace std; class Node { private: int data; Node* next; public: Node(int...

C++ program:

Convert the classes to template classes

#include <iostream>
#include <string>
using namespace std;
class Node {
  private:
    int data;
    Node* next;
  public:
    Node(int data) {  
      this->data=data;
      this->next = 0;
    }
    int getData(){return data;}
    Node* getNext(){return next;}
    void setNext(Node* next){this->next=next;}
};
class LinkedList {
  private:
    Node* head = 0;
  public:
    int isEmpty() {return head == 0;}
    void print() {
      Node* currNode = head;      
      while(currNode!=0) {
        cout << currNode->getData() << endl;
        currNode = currNode->getNext();
      }
    }
    void append(int data) {
      if(head == 0)
        head = new Node(data);
      else{
        Node* lastNode = head;
        while(lastNode->getNext()!=0)
          lastNode = lastNode->getNext();

        Node* newNode = new Node(data);
        lastNode->setNext(newNode);
      }
    }
    void recursiveDeleteAll() {
      recursiveDeleteAll(head);
      head=0;
    }
    void recursiveDeleteAll(Node* currNode){
      if(currNode != 0){
        recursiveDeleteAll(currNode->getNext());
        delete currNode;
      }
    }
    ~LinkedList() {
      recursiveDeleteAll();
/*
      Node* nodePtr = head;
      while(head!=0) {
        head = head->getNext();
        delete nodePtr;
        nodePtr = head;
      }
*/
    }
};
int main() {
  LinkedList myList;
  myList.append(99);
  myList.append(4);
  myList.append(0);
  myList.append(5);
  myList.print();
}
0 0
Next > < Previous
ReportAnswer #1
private: 8 9 10 clang version 7.8.0-3-ubuntue.18.84.1 (tags/RELEASE 7e8/final) T data; Node next; clang++-7 -pthread -o ain m

#include <iostream>
#include <string>

using namespace std;

template<class T>
class Node {
  private:
    T data;
    Node* next;
  public:
    Node(T data) {  
      this->data=data;
      this->next = 0;
    }
    T getData(){return data;}
    Node* getNext(){return next;}
    void setNext(Node* next){this->next=next;}
};

template<class T>
class LinkedList {
  private:
    Node<T>* head = 0;
  public:
    int isEmpty() {return head == 0;}
    void print() {
      Node<T>* currNode = head;      
      while(currNode!=0) {
        cout << currNode->getData() << endl;
        currNode = currNode->getNext();
      }
    }
    void append(T data) {
      if(head == 0)
        head = new Node<T>(data);
      else{
        Node<T>* lastNode = head;
        while(lastNode->getNext()!=0)
          lastNode = lastNode->getNext();

        Node<T>* newNode = new Node<T>(data);
        lastNode->setNext(newNode);
      }
    }
    void recursiveDeleteAll() {
      recursiveDeleteAll(head);
      head=0;
    }
    void recursiveDeleteAll(Node<T>* currNode){
      if(currNode != 0){
        recursiveDeleteAll(currNode->getNext());
        delete currNode;
      }
    }
    ~LinkedList() {
      recursiveDeleteAll();
/*
      Node* nodePtr = head;
      while(head!=0) {
        head = head->getNext();
        delete nodePtr;
        nodePtr = head;
      }
*/
    }
};

int main() {
  LinkedList<int> myList;
  myList.append(99);
  myList.append(4);
  myList.append(0);
  myList.append(5);
  myList.print();
}


Please upvote, as i have given the exact answer as asked in question. Still in case of any issues in code, let me know in comments. Thanks!
Know the answer?
Add Answer of:
C++ program: Convert the classes to template classes #include <iostream> #include <string> using namespace std; class Node { private: int data; Node* next; public: Node(int...
Your Answer: Your Name: What's your source?
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
  • #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ...

    #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ListNode *head; class LinkedList { public: int insertNode(float num); void deleteNode(float num); void destroyList(); void displayList(); LinkedList(void) {head = NULL;} ~LinkedList(void) {destroyList();} }; int LinkedList::insertNode(float num) { struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL; newNode = new ListNode; if(newNode == NULL) { cout << "Error allocating memory for new list member!\n"; return 1; } newNode->value = num; newNode->next = NULL; if(head==NULL) { cout << "List...

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

  • C++ LinkedList I need the code for copy constructor and assignment operator #include <iostream> #include <string> using namespace std; typedef string ItemType; struct Node {    ItemType va...

    C++ LinkedList I need the code for copy constructor and assignment operator #include <iostream> #include <string> using namespace std; typedef string ItemType; struct Node {    ItemType value;    Node *next; }; class LinkedList { private:    Node *head;    // You may add whatever private data members or private member functions you want to this class.    void printReverseRecursiveHelper(Node *temp) const; public:    // default constructor    LinkedList() : head(nullptr) { }    // copy constructor    LinkedList(const LinkedList& rhs);    // Destroys all the dynamically allocated memory    //...

  • In Java You may add any classes or methods to the following as you see fit in order to complete t...

    In Java You may add any classes or methods to the following as you see fit in order to complete the given tasks. Modify the LinkedList (or DoubleLinkedList) class and add a method append. append should take another LinkedList (DoubleLinkedList) as input and append that list to the end of this list. The append method should work by doing a few "arrow" adjustments on the boxes and it should not loop through the input list to add elements one at...

  • Please Correct my code c++

    //////////////////////////////LinkList.cpp#include "LinkList.h"/* The LinkList class implementation*//* Constructor */LinkList::LinkList(){headNode = new Node();headNode->setNext(NULL);currentNode = NULL;lastCurrentNode = NULL;size = 0;}/* add() class method */void LinkList::add (int addObject){Node * newNode = new Node();newNode->set(addObject);if( currentNode != NULL ){newNode->setNext(currentNode->getNext());currentNode->setNext( newNode );lastCurrentNode = currentNode;currentNode = newNode;}else{newNode->setNext(NULL);headNode->setNext(newNode);lastCurrentNode = headNode;currentNode = newNode;}size ++;}/* get() class method */int LinkList::get(){if (currentNode != NULL)return currentNode->get();}/* next() class method */bool LinkList::next(){if (currentNode == NULL) return false;lastCurrentNode = currentNode;currentNode = currentNode->getNext();if (currentNode == NULL || size == 0)return false;elsereturn true;}/* Friend function to traverse...

  • C++ Program with double link list_Urgent_help needed

    x.øi5 face="Times New Roman" size="3">Question Details:Q) in this code we will add two function which is shown after this code. the code is /*Node.h file*/#include <iostream.h>#include <stdlib.h>class Node{public:int get();void set(int);Node*getNext();void setNext(Node*);Node*getPrev();void setPrev(Node*);private:int object;Node*nextNode;Node*prevNode;};/* Node.cpp file*///#include "Node.h"/*The Node class implemenatation*/int Node::get(){return object;}void Node::set(int object){this->object = object;}Node*Node::getNext(){return nextNode;}void Node::setNext(Node*NextNode){this->nextNode = nextNode;}Node*Node::getPrev(){return prevNode;}void Node::setPrev(Node *prevNode){this->prevNode = prevNode;}/*DoublyLinkList.h *///#include "Node.h"/* The LinList class implementation */class DoublyLinkList{public:DoublyLinkList();void add( int addObject);int get();bool next();bool prev();friend void traverseForward(DoublyLinkList list);friend void traverseReverse(DoublyLinkList list);friend DoublyLinkList addNodes();private:int size;Node * headNode;Node* currentNode;Node*...

  • Please Add two friend functions in DoublyLinkList class

    x.øi5 ///////////////////////////////////////// Node.h#include<iostream>#include<stdlib.h>using namespace std;class Node{private: int object; Node *nextNode; Node *prevNode;public: int get(); void set(int); Node * getNext(); void setNext(Node*); Node * getPrev(); void setPrev(Node*);};//////////////////////Node.cpp ///////////#include"Node.h"// the node class implemantionint Node::get(){ return object;}void Node::set(int object){ this->object=object;}Node *Node:: getNext(){ return nextNode;}void Node::setNext(Node* nextNode){ this->nextNode=nextNode;}Node *Node::getPrev(){ return prevNode;}void Node::setPrev(Node* prevNode){ this->prevNode = prevNode;}///////////////////////// DoublyLinkList.h#include"Node.h"// the Link list class declarationclass DoublyLinkList{ private: int size; Node *headNode; Node *currentNode; Node *lastCurrentNode; public: DoublyLinkList(); void add(int addObject); int get(); bool next(); bool prev(); friend...

  • DATA STRUCTURES

    x.øi5 face="Times New Roman" size="3">/*Node.h file*/#include <iostream.h>#include <stdlib.h>class Node{public:int get();void set(int);Node*getNext();void setNext(Node*);Node*getPrev();void setPrev(Node*);private:int object;Node*nextNode;Node*prevNode;};/* Node.cpp file*///#include "Node.h"/*The Node class implemenatation*/int Node::get(){return object;}void Node::set(int object){this->object = object;}Node*Node::getNext(){return nextNode;}void Node::setNext(Node*NextNode){this->nextNode = nextNode;}Node*Node::getPrev(){return prevNode;}void Node::setPrev(Node *prevNode){this->prevNode = prevNode;}/*DoublyLinkList.h *///#include "Node.h"/* The LinList class implementation */class DoublyLinkList{public:DoublyLinkList();void add( int addObject);int get();bool next();bool prev();friend void traverseForward(DoublyLinkList list);friend void traverseReverse(DoublyLinkList list);friend DoublyLinkList addNodes();private:int size;Node * headNode;Node* currentNode;Node* lastCurrentNode;};/* DoublyLinkList.cpp file *///#include "DoublyLinkList.h"/* The LinkList class implementation */// constructorDoublyLinkList::DoublyLinkList(){headNode = new Node();headNode->setNext(NULL);headNode->setPrev(NULL);currentNode = NULL;lastCurrentNode = NULL;size =...

  • Help wanted....Linked List

    Q: Write aroutine to make two lists L1 and L2 with 5 elements given by theuser at run time. Or add two lists with its elements in themainfunction.I have written the code but i got stuck while making two listL1 and L2 with 5 elements given by the user at run time. I don'tknow how to make two different linkedlist. Plz help me in makingthe two different linked list L1 and L2with 5 elements givenby the user at run time.I...

  • Data Structure

    x.øi5ss="FontBold">Question Details:/*Node.h file*/#include <iostream.h>#include <stdlib.h>class Node{public:int get();void set(int);Node*getNext();void setNext(Node*);Node*getPrev();void setPrev(Node*);private:int object;Node*nextNode;Node*prevNode;};/* Node.cpp file*///#include "Node.h"/*The Node class implemenatation*/int Node::get(){return object;}void Node::set(int object){this->object = object;}Node*Node::getNext(){return nextNode;}void Node::setNext(Node*NextNode){this->nextNode = nextNode;}Node*Node::getPrev(){return prevNode;}void Node::setPrev(Node *prevNode){this->prevNode = prevNode;}/*DoublyLinkList.h *///#include "Node.h"/* The LinList class implementation */class DoublyLinkList{public:DoublyLinkList();void add( int addObject);int get();bool next();bool prev();friend void traverseForward(DoublyLinkList list);friend void traverseReverse(DoublyLinkList list);friend DoublyLinkList addNodes();private:int size;Node * headNode;Node* currentNode;Node* lastCurrentNode;};/* DoublyLinkList.cpp file *///#include "DoublyLinkList.h"/* The LinkList class implementation */// constructorDoublyLinkList::DoublyLinkList(){headNode = new Node();headNode->setNext(NULL);headNode->setPrev(NULL);currentNode = NULL;lastCurrentNode = NULL;size = 0;};//add() class methodvoid...

Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
Share Your Knowledge

Post an Article
Post an Answer
Post a Question with Answer

Self-promotion: Authors have the chance of a link back to their own personal blogs or social media profile pages.