Question

In: Computer Science

C++ (With main to test) 2.2 Task 1 You are going to implement a variant of...

C++ (With main to test)

2.2
Task 1
You are going to implement a variant of the standard linked list, the doubly linked list.
Doubly linked lists are because they enable a backwards and forwards traversal of the
list through the addition of more pointers. By increasing the memory cost of the list, it
enables a better access since the list does not need to be traversed in only one direction.
This will consist of implementing two classes: dLL and item.


2.2.1
dLL
The class is described according to the simple UML diagram below:
2dLL<T>
-head: item<T>*
-tail: item<T>*
-size: int
----------------------------
+dLL()
+~dLL()
+getHead(): item<T>*
+getTail(): item<T>*
+push(newItem:item<T>*):void
+pop():item<T>*
+peek():item<T>*
+getItem(i:):item<T> *
+maxNode():T
+getSize():int
+printList():void
The class variables are as follows:
• head: The head pointer of the doubly linked list.
• tail: The tail pointer of the doubly linked list.
• size: The current size of the doubly linked list. This starts at 0 and increases as the
list grows in size.
The class methods are as follows:
• dLL: The class constructor. It starts by setting the variables to null and 0 respec-
tively.
• ∼dLL: The class destructor. It will deallocate all of the memory in the class.
• getHead: This returns the head pointer of the doubly linked list.
• getTail: This returns the tail pointer of the doubly linked list.
• push: This adds a new item to the doubly linked list, by adding it to the front of
the list. The push should add to the front, which refers to the head pointer.
• pop: This returns the top item of the linked list. The item is returned and removed
from the list.
• peek: This returns the top item of the linked list but without removing it from the
list.
• getItem: This returns the item of the linked list at the index specified by the
argument but without removing it from the list. If the index is out of bounds,
return null. Use 0-indexing for the get item. That is, indices start at 0.
• maxNode: This returns the value of the item that has the highest value in the linked
list.
3• getSize: This returns the current size of the linked list.
• printList: This prints out the entire list in order, from head to tail. Each item’s
data value is separate by a comma. For example: 3.1,5,26.6,17.3. Afterwards, print
out a newline.
2.2.2
item
The class is described according to the simple UML diagram below:
item <T>
-data:T
-------------------
+item(t:T)
+~item()
+next: item*
+prev: item*
+getData():T
The class has the following variables:
• data: A template variable that stores some piece of information.
• next: A pointer of item type to the next item in the linked list.
• prev: A pointer of item type to the previous item in the linked list.
The class has the following methods:
• item: This constructor receives an argument and instantiates the data variable with
it.
• ∼item: This is the destructor for the item class. It prints out ”Item Deleted” with
no quotation marks and a new line at the end.
• getData: This returns the data element stored in the item.


You will be allowed to use the following libraries: cstdlib,string,iostream.

Solutions

Expert Solution

Code:

#include<iostream>
#include<string>
using namespace std;

// Template for class item
template <class T>
class item{
private:
   T data; // data holds the variable in the item
public:
   item* next; // pointer to the next item
   item* prev; // pointer to the previous item

   // Constructor
   item(T t){
       data = t;   // assigning data = t
       next = NULL;   // assigning next to null
       prev = NULL;   // assigning prev to null
   }

   // Destructor
   ~item(){
       cout<<"Item Deleted"<<endl;
   }
  
   // Function to return the data in the item
   T getData(){
       return this->data;
   }
};


template <class T>
class dLL{
private:
   item<T>* head;   // Pointer to the head item
   item<T>* tail; // Pointer to the tail item
   int size; // total number of items in the doubly linked list
public:
   dLL(){
       head = NULL; // initialize head to null
       tail = NULL; // initialize tail to null
       size = 0; // initialize size = 0
   }

   ~dLL(){
       // Should destroy all the items in linked list
       while(head != NULL) // the item to be deleted should not be null
       {
           item<T>* temp = head;
           head = head->next;
           delete temp;   // Deletes the item temp.
                   // Calls the destructor of item
           temp = NULL;   // After deleting, should be put to null
       }
   }

   // Function returns the pointer the head item
   item<T>* getHead(){
       return head;
   }

   // Function returns the pointer the tail item
   item<T>* getTail(){
       return tail;
   }

   // Function to add a new item to the front of the list.
   void push(item<T>* newItem){
       if(head == NULL) // if head == NULL, this is the first item to be inserted
       {
           head = newItem; // assigning head to newItem
           tail = head;   // point tail to head
           size++;       // increase size by one
           return;      
       }
      
       newItem->next = head;   // newItem
       head->prev = newItem;
       head = newItem;
       size++;
   }
  
   item<T>* pop(){
       item<T>* temp = head;
       head = head->next;
       head->prev = NULL;
       if(head == NULL)
           tail = NULL;
       size--;
       return temp;
   }

   item<T>* peek(){
       return head;
   }

   item<T>* getItem(int i){
       item<T>* temp = head;
       while(i > 0)
       {
           temp = temp->next;
           i--;
       }
      
       return temp;
   }

   // Returns the maximum data
   T maxNode(){
       item<T>* temp = head;
       T max;
       if(typeid(string)!=typeid(T))
           max = 0;
       else
       {
           cout<<"The data is of type string no max";
       }

       // Traversing the list and checking for the max item
       while(temp != NULL)
       {
           if(typeid(string)!=typeid(T) && temp->getData() > max)
               max = temp->getData();
           temp = temp->next;
       }
       return max;
   }

   // return the number of elements in the list
   int getSize(){
       return size;
   }

   // Print the Doubly linked list
   void printList(){
       item<T>* temp = head;

       cout << temp->getData();
       temp = temp->next;

       // traversing the linked list and printing the datas
       while(temp != NULL)
       {
           cout <<"," << temp->getData();
           temp = temp->next;
       }
       cout<<endl;
   }

};

int main()
{
   item<int>* i = new item<int>(1);
   dLL<int> DLL;

   DLL.push(i);
   DLL.push(new item<int>(2));

   cout<<"The List after inserting two elements:"<<endl;
   DLL.printList();

   DLL.push(new item<int>(5));
   DLL.push(new item<int>(4));
   DLL.push(new item<int>(3));

   cout<<"The List after inserting the rest of the elements:"<<endl;  
   DLL.printList();

   item<int>* temp = DLL.pop();

   cout<<"The deleted node data: "<<temp->getData()<<endl;  

   cout<<"The List after popping the top node:"<<endl;
   DLL.printList();
  
   if(temp != NULL)
   {
       delete temp;
       temp = NULL;
   }

   temp = DLL.getItem(2);

   cout<<"The data at position 2: "<<temp->getData()<<endl;

   cout<<"Peek at the top item: "<<DLL.peek()->getData()<<endl;

   cout<<"Max Node: "<<DLL.maxNode()<<endl;

   cout<<"Size of DLL: "<<DLL.getSize()<<endl;

   return 1;
}

Output:

The List after inserting two elements:
2,1
The List after inserting the rest of the elements:
3,4,5,2,1
The deleted node data: 3
The List after popping the top node:
4,5,2,1
Item Deleted
The data at position 2: 2
Peek at the top item: 4
Max Node: 5
Size of DLL: 4
Item Deleted
Item Deleted
Item Deleted
Item Deleted


Related Solutions

(C++ with main to test) 2.2 Task 1 You are going to implement a variant of...
(C++ with main to test) 2.2 Task 1 You are going to implement a variant of the standard linked list, the circular list. The circular linked list is a variant of the standard linked list that wraps around so that traversals through the list will wrap around to the beginning instead of simply reaching the end. This will consist of implementing two classes: cLL and item. 2.2.1 cLL The class is described according to the simple UML diagram below: 2cLL<T>...
(C++ with main to test) Task 1 Imagine that you are an engineer on a distant...
(C++ with main to test) Task 1 Imagine that you are an engineer on a distant planet. You were tasked with mining the resources of the planet and have done so through largely primitive means. However you have managed to set up an automated train system to gather and collect the resources from your distant operations. Your train system collects boxes full of resources. A box might contain ores or wood or even liquid fuel. These boxes are placed into...
Project #1 Designing and implementing the Game of Life The main task is to implement (in...
Project #1 Designing and implementing the Game of Life The main task is to implement (in java) a 2-D (or 3-D) Graphic version of the Game of Life
in C++ For this program, you are going to implement a stack using an array and...
in C++ For this program, you are going to implement a stack using an array and dynamic memory allocation. A stack is a special type of data structure that takes in values (in our case integers) one at a time and processes them in a special order. Specifically, a stack is what's called a first-in-last-out (FILO) data structure. That is to say, the first integer inserted into the stack is the last value to be processed. The last value in...
The question is about C++ The main should test all functionalities of the playlist implement a...
The question is about C++ The main should test all functionalities of the playlist implement a linked list to store songs of a playlist. A song (which is your node) is going to include the following data: - int Song ID - string Song Title - string Author Title - double Length Don't forget to include the next pointer that will lead to the next song. Your playlist (which is your linkedlist) is going to include the following functionalities: -...
Code in C Instructions For this programming assignment you are going to implement a simulation of...
Code in C Instructions For this programming assignment you are going to implement a simulation of Dijkstra’s solution to the Dining Philosophers problem using threads, locks, and condition variables. Dijkstra’s Solution Edsgar Dijkstra’s original solution to the Dining Philosophers problem used semaphores, but it can be adapted to use similar mechanisms: • Each philosopher is in one of three states: THINKING, HUNGRY, or EATING. • Every philosopher starts out in the THINKING state. • When a philosopher is ready to...
In this task you will implement a C++ function with arguments: a sorted integer array, size...
In this task you will implement a C++ function with arguments: a sorted integer array, size of the array, and a target integer value. Find all combinations of two elements in the sorted array which sum up to the target value. When found, add the combinations into an array, and print it. Where there is greater than one combination, you may use the number 200 as a separator for the combinations in the output array. Do not use more than...
In this task you will implement a C++ function with arguments: a sorted integer array, size...
In this task you will implement a C++ function with arguments: a sorted integer array, size of the array, and a target integer value. Find all combinations of two elements in the sorted array which sum up to the target value. When found, add the combinations into an array, and print it. Where there is greater than one combination, you may use the number 200 as a separator for the combinations in the output array. Do not use more than...
n the instructions for this Task C, we're going to assume that you have completed your...
n the instructions for this Task C, we're going to assume that you have completed your script for Task B above; i.e. that you have a working dictionary-based version of the functions (originally named addToRecord(), etc. in Task A) now named addToHistogram(), etc. Let's assume that your dictionary-based function (from Task B above) that creates a histogram is named makeHistogram(). You are going to use your makeHistogram() in the following sub-tasks. In fact, you're welcome to include any function you...
Write and test a C program to implement Bubble Sort. . In your C program, you...
Write and test a C program to implement Bubble Sort. . In your C program, you should do: Implement the array use an integer pointer, get the size of the array from standard input and use the malloc function to allocate the required memory for it. Read the array elements from standard input. Print out the sorted array, and don’t forget to free the memory. Debug your program using Eclipse C/C++ CDT.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT