Question

In: Computer Science

You are provided with a partial implementation of a templated singly-linked list in LinkedList.h, with some...

You are provided with a partial implementation of a templated singly-linked list in LinkedList.h, with some missing functionality. It contains a templated Node class and a templated LinkedList class. Do not modify the class definitions.

The linked list class contains the following methods:

• LinkedList – Constructor of a linked list with a head pointing to null (implemented).

• ~LinkedList – Destructor of a linked list.

• deleteFromHead – Removes and returns content of the first node of the list. (If the list is empty, does nothing.)

• deleteFromTail – Removes and returns content of last node of the list. (If the list is empty, does nothing.)

• deleteNode – Removes node with provided data from the list.

• InsertToHead – Inserts node with provided data at the head (implemented).

• InsertToTail – Inserts node with provided data at the tail. • getSize – Returns the number of nodes in the linked list.

• print - Prints the linked list (implemented).

Notice that while the declaration and implementation of classes are typically split between .cpp and .h files, some compilers cannot handle templates in separate files, which is why we include both the declaration and implementation in a single .h file. The main.cpp file consists of sample test code for each of the tasks below. Your code, which should be added to the provided LinkedList.h will be compiled and run with variations of this file. Submit your modified LinkedList.h file only

LinkedList.h:

#ifndef LinkedList_h
#define LinkedList_h

#include <iostream>

using namespace std;

template <class T = int>
class Node {
public:
    Node();                                         // default constructor
    Node(const T& data, Node<T>* next = nullptr);   // donstructor
    T data;                                         // node data
    Node<T>* next;                                  // node next pointer
};

template <class T = int>
class LinkedList {
public:
    LinkedList();                                   // constructor
    ~LinkedList();                                  // destructor
    T deleteFromHead();                             // removes and returns content of head
    T deleteFromTail()                              // removes and returns content of tail
    void deleteNode(T data)                         // removes node with specified data
    void InsertToHead(T data);                      // insert node with data at the head
    void InsertToTail(T data);                      // insert node with data at the tail
    int getSize();                                  // returns size of linked list
    void print();                                   // prints linked list
private:
    Node<T>* head;                                  // head of linked list
};


/******* From here down is the content of the LinkedList.cpp file: ***********************/

 /* Implementation of Node */

 // default constructor
 template<class T>
 Node<T>::Node()
 {
 }
 
 // constructor
 template<class T>
 Node<T>::Node(const T& data, Node<T>* next)
 {
     this->data = data;
     this->next = next;
 }
 
 /* Implementation of linked list */

 // constructor
 template <class T>
 LinkedList<T>::LinkedList()
 {
     head = nullptr;
 }

 // destructor
 template <class T>
 LinkedList<T>::~LinkedList()
 {
    /*** to be implemented ***/
 }

 template <class T>
 T LinkedList<T>::deleteFromHead()
 {
    /*** to be implemented ***/
 }


 template <class T>
 T LinkedList<T>::deleteFromTail()
 {
    /*** to be implemented ***/
 }
 

 template <class T>
 void LinkedList<T>::deleteNode(T data)
 {
    /*** to be implemented ***/
 }
 

 template <class T>
 void LinkedList<T>::InsertToHead(T data)
 {
     Node<T> * newNode = new Node<T>(data, nullptr);
     
     if (head == nullptr)
         head = newNode;
     else
     {
         newNode->next = head;
         head = newNode;
     }
 }


 template <class T>
 void LinkedList<T>::InsertToTail(T data)
 {
     /*** to be implemented ***/
 }


 template <class T>
 int LinkedList<T>::getSize()
 {
     /*** to be implemented ***/
 }


 template <class T>
 void LinkedList<T>::print()
 {
     if (head == nullptr)
     {
         cout << "Linked list is empty" << endl;;
         return;
     }
     
     cout << head->data << " ";
     
     if (head->next == nullptr)
     {
         cout << endl;
         return;
     }
 
     Node<T>* currNode = head->next;
     Node<T>* prevNode = head;
 
     
     while (currNode->next != nullptr)
     {
         cout << currNode->data << " ";
         prevNode = currNode;
         currNode = currNode->next;
     }
 
     cout << currNode->data << endl;
     return;
 }
     
     
#endif /* LinkedList_h */

Solutions

Expert Solution

Hello there,
Let's fill those gaps..

 // default constructor
 template<class T>
 Node<T>::Node()
 {
 }
 
 // constructor
 template<class T>
 Node<T>::Node(const T& data, Node<T>* next)
 {
     this->data = data;
     this->next = next;
 }
 
 /* Implementation of linked list */

 // constructor
 template <class T>
 LinkedList<T>::LinkedList()
 {
     head = nullptr;
 }

 // destructor
 template <class T>
 LinkedList<T>::~LinkedList()
 {
    /*** to be implemented ***/

if(head!=nullptr)
{     
     Node<T> *current = head;
     Node<T> *next;
     while(current ! = nullptr)
     {
      next = current->next;
      delete current;
      current = next;
     }
}

}

 template <class T>
 T LinkedList<T>::deleteFromHead()
 {
    /*** to be implemented ***/
   Node<T> *ptr; 
   if(head == nullptr)
     cout<<"List is Empty..";
   else
     {
      ptr=head;
      head = head->next;
      delete ptr;
     }     
 
 }
 template <class T>
 T LinkedList<T>::deleteFromTail()
 {
    /*** to be implemented ***/
   if(head==nullptr)
      cout<<"List is Empty..";
   else
   {

     Node<T> *fast;
     Node<T> *slow;
     
    fast = head;
    if(fast->next == nullptr)   //If a Linked List contain only a single node.. 
     {
        head = nullptr;
         delete head;
     }        
     else
      {
      while(fast->next != nullptr)
       {
             slow = fast;
             fast = fast->next;
       }
       slow->next = nullptr;
       delete fast;
      }

  }
   
 }
 
 template <class T>
 void LinkedList<T>::deleteNode(T data)
 {
    /*** to be implemented ***/
     int n;
     cin>>n;  //node to be deleted..    
     if(head==nullptr)
       cout<<"List is Empty..";
     else if(head->data==n)      //if node is to be deleted is first node from the front...
     {
        Node<T> *temp=head;
        head=head->next;
        delete temp;
     } 
     else 
     {
      Node<T> *fast;
      Node<T> *slow;
      fast = head;
          
      while(fast->next != nullptr)
      {
        slow = fast;
        if(fast->data==n)
          {
             break;
          }
        fast = fast->next;
      }

 Node<T> *t;
 t =  slow->next;  
 slow->next = fast->next;
 delete t;
 }
 
}
 

 template <class T>
 void LinkedList<T>::InsertToHead(T data)
 {
     Node<T> * newNode = new Node<T>(data, nullptr);
     
     if (head == nullptr)
         head = newNode;
     else
     {
         newNode->next = head;
         head = newNode;
     }
 }


 template <class T>
 void LinkedList<T>::InsertToTail(T data)
 {
     /*** to be implemented ***/
     Node<T> * newNode = new Node<T>(data, nullptr);
     if(head==nullptr)
       head = newNode;
     else
   {
     Node<T> *ptr=head;
     while(ptr->next!=nullptr)
     {
      ptr=ptr->next;
     }
   ptr->next = newNode;
}

}


 template <class T>
 int LinkedList<T>::getSize()
 {
     /*** to be implemented ***/
  if(head == nullptr)
    return 0;
  Node<T> ptr=head;
  int len=0;
  while(ptr!=nullptr)
   {
         ptr = ptr->next;
         len++;
  }     
  return len;
 }


 template <class T>
 void LinkedList<T>::print()
 {
     if (head == nullptr)
     {
         cout << "Linked list is empty" << endl;;
         return;
     }
     
     cout << head->data << " ";
     
     if (head->next == nullptr)
     {
         cout << endl;
         return;
     }
 
     Node<T>* currNode = head->next;
     Node<T>* prevNode = head;
 
     
     while (currNode->next != nullptr)
     {
         cout << currNode->data << " ";
         prevNode = currNode;
         currNode = currNode->next;
     }
 
     cout << currNode->data << endl;
     return;
 }
     
     
#endif /* LinkedList_h */

Thanks for reaching out to us..

Keep asking and keep learning.. :))))


Related Solutions

Q1) In the implementation of Singly linked list we had an integer variable called size that...
Q1) In the implementation of Singly linked list we had an integer variable called size that keeps track of how many elements in the list. Also, we have a reference “tail” that points to the last node in the list. You are asked to re-implement the concept of singly linked list without using variable size, and without using reference “tail”. a) What are the methods of the main operation of singly linked list that need to be changed? Rewrite them...
I've provided a Node class that implements a node of a simple singly-linked list (with .value...
I've provided a Node class that implements a node of a simple singly-linked list (with .value and .next fields), and an empty LinkedList class. Your task is to implement LinkedList.sort(l), where given the node l as the head of a singly-linked list, LinkedList.sort(l) sorts the nodes in the list into ascending order according to the values in the .value field of each node. Your implementation should do an in-place update of the list. It is ok to use a simple...
I was supposed to conver a singly linked list to a doubly linked list and everytime...
I was supposed to conver a singly linked list to a doubly linked list and everytime I run my program the output prints a bunch of random numbers constantly until I close the console. Here is the code. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> struct node { int data; struct node *next; struct node *prev; }; //this always points to first link struct node *head = NULL; //this always points to last link struct node *tail = NULL;...
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
Complete the provided partial C++ Linked List program. Main.cpp is given and Link list header file...
Complete the provided partial C++ Linked List program. Main.cpp is given and Link list header file is also given. The given testfile listmain.cpp is given for demonstration of unsorted list functionality. The functions header file is also given. Complete the functions of the header file linked_list.h below. ========================================================= // listmain.cpp #include "Linked_List.h" int main(int argc, char **argv) {      float           f;      Linked_List *theList;      cout << "Simple List Demonstration\n";      cout << "(List implemented as an Array - Do...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly Linked List 2. Display the list 3. Count the number of nodes 4. Insert a new node at the beginning of a Singly Linked List. 5. Insert a new node at the end of a Singly Linked List 6. Insert a new node after the value 5 of Singly Linked List 7. Delete the node with value 6. 8. Search an existing element in...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class in C++ that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value x if...
c. You are given the following Java files: SLL.java that implements generic Singly Linked List, with...
c. You are given the following Java files: SLL.java that implements generic Singly Linked List, with class SLLNode listed as inner class. TestIntegerSLL.java that tests the SLL class by using a linked list of Integer. In SLL class add the following method:                                                                    public void moveToEnd (int i) It will move the element at the i -th position to the end of the list. You can assume i to be within the list, and that the first element has the...
The file supplied.o contains code that can build, display, and destroy a linear linked list (singly-linked)....
The file supplied.o contains code that can build, display, and destroy a linear linked list (singly-linked). For this lab, you will need to write the following two functions in list.cpp, and add function prototypes for them to list.h. The provided main.cpp has calls to each of these functions commented out. As you write the functions, uncomment them from main.cpp. void reverse(node * head, node *& newHead) Recursively make a revserse copy of the source list with head where newhead is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT