Please help me write this in C++ with Classes, Dynamic Arrays. Please write the
ID: 3868646 • Letter: P
Question
Please help me write this in C++ with Classes, Dynamic Arrays. Please write the code in computer, with comments and organization. (Not so advanced because it is for a class of data structures).
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 code for ring implementation using dynamic arrays. A driver program is given to test the implementation of ring. 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;
class Ring
{
int *data;
int maxCapacity; //the maximum capacity of the ring
int count; //no. of items in ring
int currentIndex;
public:
Ring(int capacity = 10);
int size(); //the number of items stored in the ring
int capacity(); //the maximum capacity of the ring
bool empty(); //true if ring is empty , false otherwise
void add(int n); //add an item after the current
void remove(); //removes current item
void print(); //prints the list starting from beginning
void advance(); //movecurrent to next item
int current();//returns the current item
~Ring();//destructor
};
#endif /* Ring_h */
Ring.cpp
#include "Ring.h"
Ring::Ring(int capacity) //default constructor
{
data = new int[capacity];
maxCapacity = capacity;
currentIndex = -1;
count = 0;
}
//method to add an item into the ring after the current pointer.
//current points to new item
void Ring::add(int v)
{
if(size() < maxCapacity)
{
//move the index to next item
currentIndex++;
//make room for the new item to be added , by shifing all elements from current index to last
// to the right
for(int i = size() - 1; i >= currentIndex; i--)
data[i+1] = data[i];
//overwrite the previous item since its already shifted right
data[currentIndex] = v;
count++;
}
else
cout << "ring is full" << endl;
}
//returns the current item
void Ring::remove()
{
if(!empty())
{
//shift all elemnts to left
for(int i = currentIndex + 1; i < size(); i++)
data[i-1] = data[i];
count--;
if(currentIndex == size()) //if the last item was deleted, move the currrent index to 0
{
if(size() == 0)
currentIndex = -1;
else
currentIndex = 0;
}
}
}
//returns true if ring is empty , false otherwise
bool Ring::empty()
{
return count == 0;
}
//the number of items in the ring
int Ring::size()
{
return count;
}
//to move the current pointer to next item
void Ring::advance()
{
if(size() > 1)
{
currentIndex++;
if(currentIndex == size()) //if reached last item, go back to 1st item
currentIndex = 0;
}
}
//returns the current item
int Ring::current()
{
return data[currentIndex];
}
//prints from the first item
void Ring::print()
{
for(int i = 0; i < size(); i++)
{
if(i == currentIndex) //highlight the current item
cout << "->" ;
cout << data[i] << " " ;
}
cout << endl << "---------------------------------------------------------------" << endl;
}
Ring::~Ring()
{
delete []data;
}
RingDriver.cpp
#include "Ring.h"
int main()
{
Ring numRing;
int choice = 0;
int num;
while(choice != 5)
{
cout << "1. Add item after current" << endl;
cout << "2. Remove current" << endl;
cout << "3. Advance current" << endl;
cout << "4. Print" << endl;
cout << "5. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch(choice)
{
case 1:
cout << "Enter a number: ";
cin >> num;
numRing.add(num);
cout << "Added " << num << endl;
numRing.print();
break;
case 2:
numRing.remove();
cout << "After removing current" << endl;
numRing.print();
break;
case 3:
numRing.advance();
cout << "After advance," << endl;
numRing.print();
break;
case 4:
numRing.print();
break;
case 5:
break;
default:
cout << "Invalid menu choice" << endl;
}
}
return 0;
}
output
1. Add item after current
2. Remove current
3. Advance current
4. Print
5. Exit
Enter your choice: 1
Enter a number: 2
Added 2
->2
---------------------------------------------------------------
1. Add item after current
2. Remove current
3. Advance current
4. Print
5. Exit
Enter your choice: 1
Enter a number: 4
Added 4
2 ->4
---------------------------------------------------------------
1. Add item after current
2. Remove current
3. Advance current
4. Print
5. Exit
Enter your choice: 1
Enter a number: 6
Added 6
2 4 ->6
---------------------------------------------------------------
1. Add item after current
2. Remove current
3. Advance current
4. Print
5. Exit
Enter your choice: 1
Enter a number: 8
Added 8
2 4 6 ->8
---------------------------------------------------------------
1. Add item after current
2. Remove current
3. Advance current
4. Print
5. Exit
Enter your choice: 1
Enter a number: 10
Added 10
2 4 6 8 ->10
---------------------------------------------------------------
1. Add item after current
2. Remove current
3. Advance current
4. Print
5. Exit
Enter your choice: 3
After advance,
->2 4 6 8 10
---------------------------------------------------------------
1. Add item after current
2. Remove current
3. Advance current
4. Print
5. Exit
Enter your choice: 3
After advance,
2 ->4 6 8 10
---------------------------------------------------------------
1. Add item after current
2. Remove current
3. Advance current
4. Print
5. Exit
Enter your choice: 3
After advance,
2 4 ->6 8 10
---------------------------------------------------------------
1. Add item after current
2. Remove current
3. Advance current
4. Print
5. Exit
Enter your choice: 3
After advance,
2 4 6 ->8 10
---------------------------------------------------------------
1. Add item after current
2. Remove current
3. Advance current
4. Print
5. Exit
Enter your choice: 3
After advance,
2 4 6 8 ->10
---------------------------------------------------------------
1. Add item after current
2. Remove current
3. Advance current
4. Print
5. Exit
Enter your choice: 3
After advance,
->2 4 6 8 10
---------------------------------------------------------------
1. Add item after current
2. Remove current
3. Advance current
4. Print
5. Exit
Enter your choice: 3
After advance,
2 ->4 6 8 10
---------------------------------------------------------------
1. Add item after current
2. Remove current
3. Advance current
4. Print
5. Exit
Enter your choice: 2
After removing current
2 ->6 8 10
---------------------------------------------------------------
1. Add item after current
2. Remove current
3. Advance current
4. Print
5. Exit
Enter your choice: 2
After removing current
2 ->8 10
---------------------------------------------------------------
1. Add item after current
2. Remove current
3. Advance current
4. Print
5. Exit
Enter your choice: 2
After removing current
2 ->10
---------------------------------------------------------------
1. Add item after current
2. Remove current
3. Advance current
4. Print
5. Exit
Enter your choice: 2
After removing current
->2
---------------------------------------------------------------
1. Add item after current
2. Remove current
3. Advance current
4. Print
5. Exit
Enter your choice: 2
After removing current
---------------------------------------------------------------
1. Add item after current
2. Remove current
3. Advance current
4. Print
5. Exit
Enter your choice: 2
After removing current
---------------------------------------------------------------
1. Add item after current
2. Remove current
3. Advance current
4. Print
5. Exit
Enter your choice: 1
Enter a number: 12
Added 12
->12
---------------------------------------------------------------
1. Add item after current
2. Remove current
3. Advance current
4. Print
5. Exit
Enter your choice: 1
Enter a number: 14
Added 14
12 ->14
---------------------------------------------------------------
1. Add item after current
2. Remove current
3. Advance current
4. Print
5. Exit
Enter your choice: 5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.