Question

In: Computer Science

2.2.1 cLL The class is described according to the simple UML diagram below: 2cLL<T> -head:item<T> *...

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>*
+removeAt(x:T):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 in this case refers to the head.
• 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.
• removeAt: This will remove an item from the linked list based on its value. If
the value is not found, nothing should be removed. Also note that in the event of
multiple values being found, the first one in the list, from head, should be removed.
Note that nothing is deleted. Instead the node must be removed through relinking
and then returned.
• printList: This will print out the entire list from head onwards. The output consists
of a single comma delimited line, with a newline at the end. For example:
1,2,3,4,5
32.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.

Solutions

Expert Solution

// C++ program to create a circular linked list
#include <iostream>
using namespace std;

// item class to represent a node in the dLL class
template <class T>
class item
{
private:
   T data;
public:
   item(T t);
   ~item();
   item<T> *next;
   T getData();
};

// constructor
template <class T>
item<T>::item(T t)
{
   data = t;
   next = NULL;
}

// destructor
template <class T>
item<T>::~item()
{
   cout<<"Item Deleted"<<endl;
}

// return the data
template <class T>
T item<T>::getData()
{
   return data;
}

//end of item

template <class T>
class cLL
{
private:
   item<T> *head;
   int size;

public:
   cLL();
   ~cLL();
   bool isEmpty();
   void push(item<T>* newItem);
   item<T>* pop();
   item<T>* removeAt(T x);
   int getSize();
   void printList();
};

// default constructor
template <class T>
cLL<T>::cLL()
{
   head = NULL;
   size = 0;
}

// destructor
template <class T>
cLL<T>::~cLL()
{
   if(head != NULL)
   {
       item<T> *curr = head->next;
       // loop over the linked list
       while(curr != head)
       {
           item<T> *temp = curr; // get the current head
           curr = curr->next; // set head to pointer next to head
           delete(temp); // delete the old head
       }

       delete(head);
       head = NULL;
       size = 0;
   }
}

// function to return true if the list is empty else false
template <class T>
bool cLL<T>::isEmpty()
{
   return(head == NULL);
}

// function to insert the element as the first element of the list
template <class T>
void cLL<T>::push(item<T>* newItem)
{
   if(head == NULL) // empty list
   {
       head = newItem;
       head->next = head;
   }else
   {
       item<T> *curr = head->next;
       // loop to get the last element of the list
       while(curr->next != head)
           curr = curr->next;
       curr->next = newItem; // update the next pointer of the last node
       // insert the new node as the head node
       newItem->next = head;
       head = newItem;
   }

   size++; // increment the size
}

// function to remove and return the first node of the list
template <class T>
item<T>* cLL<T>:: pop()
{
   // list is empty
   if(isEmpty())
       return NULL;
   else
   {

       item<T> *node = head;
       // if one element
       if(head->next == head)
           head = NULL;
       else
       {
           item<T> *curr = head->next;
           // loop to get the last element of the list
           while(curr->next != head)
               curr = curr->next;
           // update the next pointer of last node
           curr->next = head->next;
           // update the head
           head = head->next;

       }

       size--; // decrement the size

       return node;
   }
}

// function to remove the node with data x
template <class T>
item<T>* cLL<T>::removeAt(T x)
{
   if(isEmpty()) // empty list
   {
       return NULL;
   }else if(head->getData() == x) // if x is present in head, call pop
   {
       return pop();
   }else
   {
       item<T> *curr = head->next;
       item<T> *prev = head;
       // loop to get the node with data x
       while(curr != head)
       {
           // if curr's data is x
           if(curr->getData() == x)
           {
               prev->next = curr->next; // update the enxt pointer of prev node
               size--; // decrement the size
               return curr; // return the curr node
           }

           prev = curr; // set prev to curr
           curr = curr->next; // set curr to next of curr
       }

       return NULL;
   }
}

// function to return the size of the list
template <class T>
int cLL<T>::getSize()
{
   return size;
}

// function to print the list
template <class T>
void cLL<T>::printList()
{
   if(isEmpty()) // empty list
   {
       cout<<"Empty list"<<endl;
   }else if(head->next == head) // single node list
   {
       cout<<head->getData()<<endl;
   }else
   {
       item<T> *curr = head;
       // loop to print the list
       while(curr->next != head)
       {
           cout<<curr->getData()<<",";
           curr = curr->next;
       }

       cout<<curr->getData()<<endl;
   }

}
//end of cLL


Related Solutions

Given the following UML class diagram, implement the class as described by the model. Use your...
Given the following UML class diagram, implement the class as described by the model. Use your best judgment when implementing the code inside of each method. +---------------------------------------------------+ | Polygon | +---------------------------------------------------+ | - side_count : int = 3 | | - side_length : double = 1.0 | +---------------------------------------------------+ | + Polygon() | | + Polygon(side_count : int) | | + Polygon(side_count : int, side_length : double) | | + getSides() : int | | + setSides(side_count : int) | |...
1.) According to the UML Class Diagram, these are the mutator and accessor methods that you...
1.) According to the UML Class Diagram, these are the mutator and accessor methods that you need to define: 1a.) +setName(value:String):void 1b.) +setGPA(value:double):void 1c.) +setID(value: String):void 1d.) +getName(): String 1e.) +getLastName():String 2.) Working with constructors 2a.) +Student() : This is the default constuctor. For the Strings properties, initialize them to null. The order is String ID, String name, String lastName, and double GPA. 2b.) +Student(value:String) - This constructor receives just the ID. 2c.) +Student(value:String, var: String) - This constructor receives...
draw a uml diagram for a class
draw a uml diagram for a class
create the UML Diagram to model a Movie/TV viewing site. Draw a complete UML class diagram...
create the UML Diagram to model a Movie/TV viewing site. Draw a complete UML class diagram which shows: Classes (Only the ones listed in bold below) Attributes in classes (remember to indicate privacy level, and type) No need to write methods Relationships between classes (has is, is a, ...) Use a program like Lucid Cart and then upload your diagram. Each movie has: name, description, length, list of actors, list of prequals and sequals Each TV Show has: name, description,...
Draw a UML diagram for the classes. Code for UML: // Date.java public class Date {...
Draw a UML diagram for the classes. Code for UML: // Date.java public class Date {       public int month;    public int day;    public int year;    public Date(int month, int day, int year) {    this.month = month;    this.day = day;    this.year = year;    }       public Date() {    this.month = 0;    this.day = 0;    this.year = 0;    } } //end of Date.java // Name.java public class Name...
<<<<<<<<. I need the UML diagram for all classes.java below. >>>>> // Vehicle.java public class Vehicle...
<<<<<<<<. I need the UML diagram for all classes.java below. >>>>> // Vehicle.java public class Vehicle {    // data members declared as private    private String make;    private double weight;    private double height;    private double length;    private int maxSpeed;    private int noOfDoors;    private int numberSeats;    /**    * @param make    * @param weight    * @param height    * @param length    * @param maxSpeed    * @param noOfDoors    *...
A UML class diagram can be used to model UML by considering each graphical component of...
A UML class diagram can be used to model UML by considering each graphical component of the notation to be a class. For example, a class diagram contains a collection of classes. Your problem is to construct and draw one class diagram with the below UML elements. That is, there will be a class for each of the elements listed. You must also provide ALL of the relationships between each of these classes. The elements to model in the class...
Draw a UML diagram that describes a class that will be used to describe a product...
Draw a UML diagram that describes a class that will be used to describe a product for sale on Glamazon.com. The product has a name, a description, a price, ratings by many customers (1 to 5 stars), and a group of customer comments. New products have no ratings or comments by customers, but do have a name, description and price. The price can be changed and more customer ratings and comments can be added. A global average rating of all...
Given the following UML class diagram, implement the class as described by the model. Use your best judgement when implementing the code inside of each method.
In java Given the following UML class diagram, implement the class as described by the model. Use your best judgement when implementing the code inside of each method.+------------------------------+| Circle |+------------------------------+| - radius : double = 1.0 |+------------------------------+| + Circle(radius : double) || + getRadius() : double || + setRadius(radius : double) || + area() : double || + perimeter() : double |+------------------------------+The following information might also be useful:Formula for area of a circle with radius r:       A = πr^2Formula for...
Complete the following class UML design class diagram by filling in all the sections based on...
Complete the following class UML design class diagram by filling in all the sections based on the information given below.         The class name is Boat, and it is a concrete entity class. All three attributes are private strings with initial null values. The attribute boat identifier has the property of “key.” The other attributes are the manufacturer of the boat and the model of the boat. Provide at least two methods for this class. Class Name: Attribute Names: Method...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT