In: Computer Science
(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.
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.