could you please help me with this assignment in C++ ------- UML----------------
ID: 3696935 • 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
class Dynamic
{
private:
struct Node
{
int value;
Node* next;
};
Node* head;
Node* tail;
int Items;
public:
Dynamic()
{
items = 0;
tail = 0;
head = tail;
}
~Dynamic()
{
clear();
}
int push (const int i)
{
Node *newNode = new Node;
if(isFull())
{
cout << "Stack is full! " << endl;
return 0;
}
else{
newNode-> value = i;
newNode-> next = head;
head = newNode;
items++; ///the items counter is increased to keep track of the queue size
}
return 1;
}
int enqueue (const int& i)
{
if (isFull()) ///////if queue is full (10) then stop operation
return 0;
Node * add = new Node; ////////new Node* structure is created, uninitialized
if (add == NULL) ////if the new allocation failed, stop operation
return 0;
add->i = i; ///the passed reference is copied to add's item data member
add->next = NULL; ///add's next pointer is initialized to NULL
items++; ///the items counter is increased to keep track of the queue size
if (head == NULL) //front would initially be set to NULL upon object creation
head = add; //the first use of "enqueue" on an empty queue object
//will always copy add to front
else
tail->next = add;
tail = add; ///add is currently copied to both front and rear...
///wtf, the next pointer is always NULL? why have it?
return 1;
}
int pop ()
{
if(isEmpty())
{
cout << "Stack is empty! " << endl;
return 0;
}
else{
Node *temp = head;
head = temp-> next;
items--; //the items counter is decreased to keep track of the queue size
}
return 1;
}
int dequeue (const int& i)
{
Node *temp = new Node;
if (isEmpty()) ///////if queue is Empty then stop operation
return 0;
else
if (head == NULL) ////if the head is null, stop operation
return 0;
else{
i = head -> i;
temp = head;
head =head -> next;
delete temp;
items--; //the items counter is decreased to keep track of the queue size
}
if(isEmpty())
head = tail = NULL;
items--; //the items counter is decreased to keep track of the queue size
return 1;
}
void clear()
{
Node *temp =new Node;
while(head != NULL)
{
temp = head;
head = head -> next;
delete temp;
}
head = tail = NULL;
}
int peek ()
{
if(IsEmpty())
{
cout << "Stack is empty! " << endl;
return 0;
}
return 1;
}
bool isFull ()
{
return 0;
}
bool isEmpty ()
{
if(head == NULL)
return 0;
}
int length()
{
return items;
}
void print ()
{
Node *p = new Node;
p = head;
if(head==NULL)
cout<<" Nothing to Display";
else{
while(p!= NULL){
cout<<endl<<p->value;
p=p->next;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.