Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

c++ visual studio 2019 Do not use #include Use your circular list implementation to implement a circular queue in the following three ways: Using composition

c++ visual studio 2019

Do not use #include

Use your circular list implementation to implement a circular queue in the following three ways:

  • Using composition
  • Overriding selected public methods with private methods
  • Using private inheritance.

The circular queue should expose only the following methods:

back

Returns the value of the last (most recently added) element at the back of the queue.

empty

Tests if the queue is empty.

front

Returns the value of the element at the front of the queue.

pop

Removes an element from the front of the queue

push

Adds an element to the back of the queue.

size

Returns the number of elements in the queue.

print

Prints all values in the queue, from the front to the back.

list2.h------------------------------------------------------------------------------------\

#include #include #include

using namespace std;

class List; class Iterator;

class Node { public: /** * Constructs a node with a given data value. * @param s the data to store in this node */ Node(string s); private: string data; Node* previous; Node* next; friend class List; friend class Iterator; };

class List { public: /** * Constructs an empty list; */ List(); /** * Appends an element to the list. * @param data the value to append */ void push_back(string data); /** * Inserts an element into the list. * @param iter the position before which to insert * @param s the value to append */ void insert(Iterator iter, string s); /** * Removes an element from the list. * @param iter the position to remove * @return an iterator pointing to the element after the * erased element */ Iterator erase(Iterator iter); /** * Gets the beginning position of the list. * @return an iterator pointing to the beginning of the list */ Iterator begin(); /** * Gets the past-the-end position of the list. * @return an iterator pointing past the end of the list */ Iterator end(); private: Node* first; Node* last; friend class Iterator; };

class Iterator { public: /** * Constructs an iterator that does not point into any list. */ Iterator(); /** * Looks up the value at a position. * @return the value of the node to which the iterator points */ string get() const; /** * Advances the iterator to the next node. */ void next(); /** * Moves the iterator to the previous node. */ void previous(); /** * Compares two iterators * @param b the iterator to compare with this iterator * @return true if this iterator and b are equal */ bool equals(Iterator b) const; private: Node* position; List* container; friend class List; };

list.cpp----------------------------------------------------------------------------------------------

#include #include #include #include "list2.h"

using namespace std;

Node::Node(string s) { data = s; previous = NULL; next = NULL; }

List::List() { first = NULL; last = NULL; }

void List::push_back(string data) { Node* new_node = new Node(data); if (last == NULL) { first = new_node; last = new_node; } else { new_node->previous = last; last->next = new_node; last = new_node;

} last->next = first; first->previous = last; }

void List::insert(Iterator iter, string s) { if (iter.position == NULL) { push_back(s); return; } Node* new_node = new Node(s); Node* after = iter.position; Node* before = after->previous;

new_node->previous = before; new_node->next = after; if (before == last) { first = new_node; first->previous = last; last->next = first;

// the previous node of last // should be updated from // pointing to itself to // pointing to the new node last->previous = first; } else { before->next = new_node;

// the previous node of after // should be updated from // pointing to before to // pointing to the new node after->previous = new_node; }

}

Iterator List::erase(Iterator iter) { assert(iter.position != NULL); Node* remove = iter.position; Node* before = remove->previous; Node* after = remove->next;

if (remove == first) { if (first == last) { first = NULL; last = NULL; } else { first = after; first->previous = last; last->next = first; } } else { before->next = after;

// the previous node of after // should be updated from // pointing to the remove to // pointing to before after->previous = before; }

if (remove == last) { // No separate check is necessary // as this has been covered already /* if (first != last) { */ last = before; first->previous = last; last->next = first; // } } else { after->previous = before;

// the next node of before // should be updated from // pointing to the remove to // pointing to after before->next = after; }

delete remove;

Iterator r; r.position = after; r.container = this; return r; }

Iterator List::begin() { Iterator iter; iter.position = first; iter.container = this; return iter; }

Iterator List::end() { Iterator iter; iter.position = last; iter.container = this; return iter; }

Iterator::Iterator() { position = NULL; container = NULL; }

string Iterator::get() const { assert(position != NULL); return position->data; }

void Iterator::next() { assert(position != NULL); position = position->next; }

void Iterator::previous() { assert(position != container->first); if (position == NULL) position = container->last; else position = position->previous; }

bool Iterator::equals(Iterator b) const { return position == b.position; }

/** * Checks if the passed circular list is empty. * @param list the circular list to check * @return true if the circular list is empty and false otherwise */ bool empty(List& list) { return list.begin().equals(Iterator{}); }

/** * Prints the values in the nodes of the list * from the first to the last. * @param list the circular list to print */ void print(List& list) { for (Iterator it{ list.begin() }; !it.equals(list.end()); it.next()) { cout << it.get() << " "; } }

int main() { List staff;

staff.push_back("Tom"); staff.push_back("Dick"); staff.push_back("Harry"); staff.push_back("Juliet");

Iterator pos; pos = staff.begin(); pos.next(); pos.next(); pos.next();

staff.insert(pos, "Romeo");

pos = staff.begin(); pos.next();

staff.erase(pos);

// Replaced this with the print function usage. /* for (pos = staff.begin(); !pos.equals(staff.end()); pos.next()) cout << pos.get() << " "; */

// tests print { print(staff); }

// This is commented because it tests functions // that are not made available in the class in // list2.h. /* List mylist{ "Tom", "Dick", "Harry", "Juliet", "Romeo" }; if (mylist.empty()) cout << "true"; else cout << "false"; */

// tests empty { List emptyList; assert(empty(staff) == false); assert(empty(emptyList) == true); }

return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing Fundamentals Design

Authors: Marion Donnie Dutton Don F. Seaman

14th Edition Globel Edition

1292107634, 978-1292107639

More Books

Students also viewed these Databases questions

Question

What are the stages of project management? Write it in items.

Answered: 1 week ago

Question

why do consumers often fail to seek out higher yields on deposits ?

Answered: 1 week ago