Please Help me write this in C++ with Classes and Dynamic Memory. Please organiz
ID: 3868105 • Letter: P
Question
Please Help me write this in C++ with Classes and Dynamic Memory. Please organization, comments and Send it written on computer not by hand
A ring is a collection of items that has a reference to a current item. An operation -let's call it advance-moves the reference to the next item in the collection. When the reference reaches the last item, the next advance opera- tion moves the reference back to the first item. A ring also has operations to get the current item, add an item, and remove an item. The details of where an item is added and which item is removed are up to you Design an ADT to represent a ring of objects. Specify each operation by stating its purpose, by describing its parameters, and by writing a pseudocode version of its header. Then write a C++interface for a ring's meth- ods. Include javadoc-style comments in your codeExplanation / Answer
Given below is the ring class and also a main program to see how it works. Output shown at end. Please don't forget to rate the answer if it helped. Thank you.
ring.h
#ifndef ring_h
#define ring_h
#include <iostream>
using namespace std;
struct item
{
int data;
item * next;
};
//a class to represent ring of items.
class ring
{
item * first; //refers to first item in the ring
item * current; //refers to current item being iterated using advance()
item * previous; //referes to item previous to current item
int sz; //size , i.e number of items in the ring
public:
ring(); //default constructor
void addItem(int n); //method to add an item into the ring after the current pointer
int removeItem(); //removes an item pointed by current pointer, should be called only if ring is not empty
bool isEmpty(); //returns true if ring is empty , false otherwise
int size(); //the number of items in the ring
void advance(); //to move the current pointer to next item
item * getCurrent();//returns the current item
void print(); //prints the time starting from 1st
};
#endif /* ring_h */
ring.cpp
#include "ring.h"
ring::ring() //default constructor
{
first = nullptr;
current = nullptr;
previous = nullptr;
}
//method to add an item into the ring after the current pointer. current is advanced to the new item added
void ring::addItem(int d)
{
//allocate memory for ring item
item * n = new item;
n->data = d;
n->next = nullptr;
if(current == nullptr) //empty ring
first = current = n;
else
{
//put the new node after current
n->next = current->next;
current->next = n;
//move the current to new node
previous = current;
current = n;
}
sz++; //increment size of ring
}
int ring::removeItem() //removes an item pointed by current pointer, should be called only if ring is not empty
{
int val;
item *next;
if(current != nullptr)
{
next = current->next;//save link to the next node before deleting current
val = current->data;
delete current; //delete current item
current = next; //now make next item as current
if(previous == nullptr) //deleteing 1st time in the ring
{
first = current;
}
else
{
previous->next = current;
}
//if we deleted the last node in the ring
if(current == nullptr)
{
current = first;
previous = nullptr;
}
sz--;
}
return val;
}
bool ring::isEmpty() //returns true if ring is empty , false otherwise
{
return sz == 0;
}
int ring::size() //the number of items in the ring
{
return sz;
}
void ring::advance()//to move the current pointer to next item
{
if(!isEmpty())
{
previous = current;
current = current->next;
if(current == nullptr) //reache end , go back to 1st item
{
previous = nullptr;
current = first;
}
}
}
item * ring::getCurrent()//returns the current item
{
return current;
}
//print from starting item. current item is highlighted in square bracket
void ring::print()
{
item *p = first;
cout << " ";
while(p != nullptr)
{
if(p == current)
cout << "[" << p->data << "] " ;
else
cout << p->data << " " ;
p = p->next;
}
cout << endl << "---------------------------------------------------------------" << endl;
}
ringmain.cpp
#include "ring.h"
int main()
{
ring numbers;
cout << "add 1" << endl;
numbers.addItem(1);
numbers.print();
cout << "add 2" << endl;
numbers.addItem(2);
numbers.print();
cout << "add 3, 4, 5" << endl;
numbers.addItem(3);
numbers.addItem(4);
numbers.addItem(5);
numbers.print();
cout << "advance" << endl;
numbers.advance();
numbers.print();
cout << "add 6" << endl;
numbers.addItem(6);
numbers.print();
cout << "advance, advance" << endl;
numbers.advance();
numbers.advance();
numbers.print();
cout << "removed current " << numbers.removeItem() << endl;
numbers.print();
cout << "add 7" << endl;
numbers.addItem(7);
numbers.print();
cout << "advance" << endl;
numbers.advance();
numbers.print();
cout << "remove, remove " << endl;
numbers.removeItem();
numbers.removeItem();
numbers.print();
return 0;
}
output
add 1
[1]
---------------------------------------------------------------
add 2
1 [2]
---------------------------------------------------------------
add 3, 4, 5
1 2 3 4 [5]
---------------------------------------------------------------
advance
[1] 2 3 4 5
---------------------------------------------------------------
add 6
1 [6] 2 3 4 5
---------------------------------------------------------------
advance, advance
1 6 2 [3] 4 5
---------------------------------------------------------------
removed current 3
1 6 2 [4] 5
---------------------------------------------------------------
add 7
1 6 2 4 [7] 5
---------------------------------------------------------------
advance
1 6 2 4 7 [5]
---------------------------------------------------------------
remove, remove
[6] 2 4 7
---------------------------------------------------------------