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 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>
-head:item<T> *
-size:int
------------------------
+cLL()
+~cLL()
+isEmpty():bool
+getSize():int
+push(newItem: item<T>*):void
+pop():item<T>*
+getMaxItem():T
+getMinItem():T
+peek(): item<T> *
+printList():void
The class variables are as follows:
• head: The head pointer for the linked list.
• size: The current size of the circular linked list. It starts at 0 and grows as the list
does.
The class methods are as follows:
• cLL: The constructor. It will set the size to 0 and initialise head as null.
• ∼cLL: The class destructor. It will iterate through the circular linked list and
deallocate all of the memory assigned for the items.
• isEmpty: This function returns a bool. If the circular linked list is empty, then it
will return true. Otherwise, if it has items then it will return false.
• getSize: This returns the current size of the circular linked list. If the list is empty
the size should be 0.
• push: This receives a new item and adds it to the circular linked list. It is added to
the front of the list. The front being the head of the list.
• pop: This receives, and returns, the first element in the list. The first element is
removed from the list in the process. If the list is empty, return null. The first
element referring to the head pointer in this case.
• peek: This function returns the first element in the list but does not remove it, first
being the head item.
• getMaxItem: This will return the value of the item in the circular linked list which
is the maximum for the list.
• getMinItem: This will return the value of the item in the circular linked list which
is the minimum for the list.
3• printList: This will print out the entire list from head onwards. Importantly, each
item is listed on one line with commas delimiting each item. For example,
1.2,1.3,1,4,5,6
Remember to add a newline at the end.
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*
+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.
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>
#include<cstdlib>
using namespace std;
template <typename T>
class item
{
public:
   item (T t);
   item *next;
   T getData();
   ~item();
private:
   T data;
};
template <typename T>
item<T>::item(T t)
{
   data=t;
}
template <typename T>
T item<T>::getData()
{
   return data;
}
template <typename T>
item<T>::~item()
{
   cout<<"Item Deleted"<<endl;
}

//----------class cLL---------------------
template <typename T>
class cLL
{
public:
   cLL();
   //~cLL();
   bool isEmpty();
   int getSize();
   void push(item<T> *);
   item<T>* pop();
   T getMaxItem();
   T getMinItem();
   item<T>* peek();
   void printList();
private:
   item<T> *head;
   int size;
};

template <typename T>
cLL<T>::cLL()
{
   head=NULL;
   size=0;
}
template <typename T>
int cLL<T>::getSize()
{
   return size;
}
template <typename T>
bool cLL<T>::isEmpty()
{
   return size==0;
}
template <typename T>
void cLL<T>::push(item<T> *newItem)
{
   item<T> *temp = head;
   if(head==NULL)
   {
       newItem->next=newItem;
   }
   else
   {
       while (temp->next != head)
temp = temp->next;
temp->next = newItem;
       newItem->next=head;
   }
   size++;
   head=newItem;
}
template <typename T>
void cLL<T>::printList()
{
   item<T> *temp = head;
if (head != NULL)
{
do
{
cout << temp->getData() << ",";
temp = temp->next;
}
while (temp->next != head);
       cout<<temp->getData();
       cout<<endl;
}
   else
   {
       cout<<"List is empty!!!"<<endl;
   }
}
template <typename T>
T cLL<T>::getMaxItem()
{
   item<T> *temp = head;
   T maxValue=head->getData();
   do
{
if(maxValue<temp->getData())
           maxValue=temp->getData();
temp = temp->next;
}
while (temp != head);
       return maxValue;
}
template <typename T>
T cLL<T>::getMinItem()
{
   item<T> *temp = head;
   T minValue=head->getData();
   do
{
if(minValue>temp->getData())
           minValue=temp->getData();
temp = temp->next;
}
while (temp != head);
       return minValue;
}
template <typename T>
item<T>* cLL<T>:: peek()
{
   if(head==NULL)
       return NULL;
   return head;
}
template <typename T>
item<T>* cLL<T>::pop()
{
   if(head==NULL)
       return NULL;
   item<T> *temp = head;
   item<T> *popedElement=head;
   if(head->next==head)
   {

       head->next=NULL;
       //delete(head);
       return temp;
   }
     
   // Find the last node of the list
while(temp->next!=head)
temp=temp->next;
  
// Point last node to the next of head i.e.
// the second node of the list
temp->next=(head)->next;
(head)->next=NULL;
       //free(head);
head=temp->next;
       return popedElement;
}

int main()
{
   item<double> node1(1.2);
   item<double> node2(1.3);
   item<double> node3(1);
   item<double> node4(4);
   item<double> node5(5);
   item<double> node6(6);

   cLL<double> list;
   list.push(&node1);
   list.push(&node2);
   list.push(&node3);
   list.push(&node4);
   list.push(&node5);
   list.push(&node6);

   cout<<"List is : ";
   list.printList();

   cout<<"\nSize of the list is: "<<list.getSize()<<endl;
   cout<<"\nMax value is : "<<list.getMaxItem()<<endl;
   cout<<"\nMin value is : "<<list.getMinItem()<<endl;

   cout<<"\nPeek element is "<<list.peek()->getData()<<endl;

   cout<<"\nPoped element is : "<<list.pop()->getData()<<endl;

   cout<<"\nList is : ";
   list.printList();

   return 1;

}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


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