In c++ Write a .h and .cpp file to implement the following UMLS as follows: 2.2.
ID: 3749552 • Letter: I
Question
In c++
Write a .h and .cpp file to implement the following UMLS as follows:
2.2.1 dLL
The class is described according to the simple UML diagram below:
dLL<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>*
+getItem(i:int):item<T>*
+minNode():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.
pop: This returns the top item of the linked list. The item is returned and removed
from the list.
getItem: This returns the item of the linked list at the index specied by the
argument but without removing it from the list. If the index is out of bounds,
return null.
minNode: This returns the value of the item that has the lowest value in the linked
list.
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
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
Your submission must contain item.h,
item.cpp, dLL.cpp, dLL.h, main.cpp
Explanation / Answer
Below is the C++ code I hope that i have provided sufficient comments for your better understanding Note that I have done proper indentation but this code is automatically left alligned on this interface
Item -
template <class T>
class Item
{
private:
T data;
Item *next,*prev;
public:
Item(T value) //constructor
{
data=value;
next=NULL;
prev=NULL;
}
~Item() //Destructor
{
cout<<"Item Deleted"<<endl;
}
T getData()
{
return data;
}
};
DLL -
template <class T>
class DLL
{
private:
Item <T>*tail,*head;
int size;
public:
DLL() //constructor
{
tail=NULL;
head=NULL;
size=0;
}
~DLL()
{
Item <T>*temp=head,*n;
while(temp!=NULL) //Move till end
{
n=temp;
temp=temp->next;
delete n; //release memory
}
}
Item <T>*getHead()
{
return head;
}
Item <T>*getTail()
{
return tail;
}
void push(Item <T>*newItem)
{
if(head==NULL) //doubly linked list is currently empty
{
head=newItem;
tail=newItem;
}
else
{
newItem->next=head; //Connect it to the head
head->prev=newItem; //Connect it to the head
head=newItem; //Make it the first entry
}
size++;
}
Item<T>* pop()
{
if(tail==NULL) //doubly list is empty
return NULL;
Item <T>*n=tail;
if(head==tail) //Only 1 entry present in doubly link list
{
head=NULL;
tail=NULL;
}
else
{
tail=tail->prev;
tail->next=NULL;
}
size--;
delete n;
}
Item<T>* getItem(int i)
{
if(i>size) //Index out of bound
return NULL;
Item <T>*p=head;
for(int loop=1;loop<i;loop++)
{
p=p->next;
}
return p;
}
T minNode()
{
Item <T>*p=head;
T res=p->data;
while(p!=NULL) //Move till end
{
if(res>p->data) //compare current minimum with the current data
res=p->data;
p=p->next;
}
return res;
}
int getSize()
{
return size;
}
void printList()
{
Item <T>*p=head;
while(p!=NULL)
{
cout<<p->data<<",";
p=p->next;
}
cout<<endl;
}
};
Hope i have answered your question satisfactorily.Leave doubts in comment section if any
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.