could you please help me with this assignment in C++ -------UML-----------------
ID: 3858188 • Letter: C
Question
could you please help me with this assignment in C++
-------UML------------------------------
class Dynamic
{
private: struct Node
{ int value;
Node* next;
};
Node* head;
Node* tail;
public: Dynamic()
~Dynamic()
int push (int i)
int enqueue (int i)
int pop (int& i)
int dequeue (int& i)
void clear
int peek (int& i) const
bool isFull () const
bool isEmpty ()const
int length() const
void print () const
---------------------------------------------------
Use the UML above to write the Class Dynamic that require:
This class implements all the methods required for a Linked List version ( dynamic ) of a stack and a queue that stores integers. For this class, if you would like to implement the enqueue method by using a "tail" pointer, feel free to add a second pointer. Overall: The constructor should initialize the pointer(s) to NULL.
The destructor should destroy the list.
clear() - this method should destroy the list.
isFull() - returns true if the structure is full, false otherwise.
isEmpty() - returns true if the structure is empty, false otherwise.
print() - displays the contents of the structure.
The value of each node is displayed on a single line, separated by a single space
. length() - returns the number of items in the list.
When the class functions as a stack:
push() - puts it's argument on top of the stack. Returns 0 if successful, -1 if it fails.
pop() - removes a node from the top of the stack. The value should be assigned to the reference parameter. Returns 0 if successful, -1 if it fails.
peek() - assigns the value from the top of the stack to the reference parameter, WITHOUT removing the node. Returns 0 if successful, -1 otherwise.
When the class functions as a queue:
enqueue() - adds it's argument to the end of the queue. Returns 0 if successful, -1 otherwise.
dequeue() - removes the node at the front of the queue, assigning it's value to the reference parameter. Returns 0 if successful, -1 otherwise.
peek() - assigns the value from the front of the queue to the reference parameter, WITHOUT removing the node. Returns 0 if successful, -1 otherwise.
Explanation / Answer
/**
* Dynamic Class Implementation
**/
class Dynamic
{
// Node Structure
private: struct Node {
int value;
Node* next;
};
//Variable Declaration
// Points to first node in the list
Node* head;
// Points to last node in the list
Node* tail;
// Constructor point head and tail to null
public: Dynamic() {
*head = null;
*tail = null;
}
// Destructor
~Dynamic() {
if(null != head && null != tail) {
Node* temp = head;
Node* prev;
while(temp->next){
prev = temp;
temp= temp->next;
prev->next = null;
}
head = null;
tail = null;
}
}
int push (int i) {
Node* temp = new Node;
if(temp != null) {
temp->value = i;
temp->next = null;
// If head or tail is null, no node is present, so add temp as head and tail, else add temp to top
if(null == head || null == tail) {
head = temp;
tail = temp;
} else {
tail = temp;
}
return 0;
} else {
return -1;
}
}
int enqueue (int i) {
Node* temp = new Node;
if(temp != null) {
temp->value = i;
temp->next = null;
// If head or tail is null, no node is present, so add temp as head and tail, else add temp to top
if(null == head || null == tail) {
head = temp;
tail = temp;
} else {
tail = temp;
}
return 0;
} else {
return -1;
}
}
int pop (int& i) {
// If tail or head is null, the list is empty
if(null == tail || null == head) {
return -1;
} else {
i = tail;
Node* temp = head;
Node* prev;
while(temp->next) {
prev = temp;
temp = temp->next;
}
// If prev is null, only one node is present in the list
if(null == prev) {
head = null;
tail = null;
} else {
prev->next = null;
tail = prev;
}
return 0;
}
}
int dequeue (int& i) {
// If tail or head is null, the list is empty
if(null == tail || null == head) {
return -1;
} else {
i = head;
Node* temp = head;
if(null == head->next`) {
head = null;
tail = null;
} else {
temp->next = null;
head = head->next;
}
return 0;
}
}
void clear {
// If head and tail are not null, list is not empty
if(null != head && null != tail) {
Node* temp = head;
Node* prev;
while(temp->next){
prev = temp;
temp= temp->next;
prev->next = null;
}
head = null;
tail = null;
}
}
int peek (int& i) const {
// If head or tail is null, list is empty
if(null == head || null == tail`) {
return -1;
} else {
i = head;
return 0;
}
}
bool isFull () const {
Node* temp = new Node;
if(null == temp) {
return true;
} else {
temp = null;
return false;
}
}
bool isEmpty ()const {
if(null == head || null == tail) {
return true;
} else {
else false;
}
}
int length() const {
int listLength = 0;
if(null != head && null != tail) {
Node* temp = head;
listLength++;
while(temp->next){
listLength++;
temp = temp->next;
}
}
return listLength;
}
void print () const {
if(null != head && null != tail) {
Node* temp = head;
while(temp->next){
cout<<temp.value;
cout<<" ";
temp = temp->next;
}
cout<<temp.value;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.