Question & Answer: A ring is a collection of items that has a reference to a current item. An operation-let's call it advance-moves the referenc…..

Please help me, write this code in C++, with classes and Dynamic Memory.

.A ring is a collection of items that has a reference to a current item. An operation-lets 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 rings meth- ods. Include javadoc-style comments in your code.

Don't use plagiarized sources. Get Your Custom Essay on
Question & Answer: A ring is a collection of items that has a reference to a current item. An operation-let's call it advance-moves the referenc…..
GET AN ESSAY WRITTEN FOR YOU FROM AS LOW AS $13/PAGE
Order Essay

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 operation 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 methods. Include javadoc-style comments in your code.

Expert Answer

 

#include<iostream>

using namespace std;

template <class T>
class ring {
private:
T* arr;
int maxSize;
int size;
int index;
void resize() {
maxSize = size*2;
T* newArr = new T[maxSize];
for (int i = 0; i < size; i++)
newArr[i] = arr[i];
arr = newArr;
}
public:

ring() {
maxSize = 5;
size = 0;
index = -1;
}

void add(T item) {
if (index == -1) {
arr[++index] = item;
size ++;
return;

}
if (size == maxSize)
resize();
for (int i = size; i > index; i–)
arr[i] = arr[i – 1];
arr[index] = item;
size ++;
}

T remove() {
T tmp = arr[index];
for (int i = index; i < size; i++)
arr[i] = arr[i + 1];
size –;
if (index == size)
index –;
return tmp;
}

T get() {
return arr[index];
}

void set(T item) {
arr[index] = item;
}

void next() {
if (size == 0)
return;
index++;
if (index == size)
index = 0;
}

};

int main() {
ring<int> r;
r.add(5);
cout << “add 5” << endl;
r.add(7);
cout << “add 7” << endl;
cout << “get –> ” << r.get() << endl;
r.next();
cout << “next ” << endl;
cout << “get –> ” << r.get() << endl;
r.add(-1);
cout << “add -1” << endl;
r.add(-3);
cout << “add -3” << endl;
cout << “get –> ” << r.get() << endl;
r.next();
cout << “next ” << endl;
cout << “get –> ” << r.get() << endl;
r.next();
cout << “next” << endl;
cout << “get –> ” << r.get() << endl;
cout << “get –> ” << r.get() << endl;
r.next();
cout << “next ” << endl;
cout << “get –> ” << r.get() << endl;
r.next();
cout << “next” << endl;
cout << “get –> ” << r.get() << endl;
}

Output:

Question & Answer: A ring is a collection of items that has a reference to a current item. An operation-let's call it advance-moves the referenc..... 1

Still stressed from student homework?
Get quality assistance from academic writers!