Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

c++ visual studio 2019 Do not use #include (I need a different method than the one below to implement a circular queue using my circular

c++ visual studio 2019

Do not use #include

(I need a different method than the one below to implement a circular queue using my circular list) (was told by my professor that other students had the same method) also use the circular list method that I have made in order to implement the circular queue which is provided below the pictures of the method that other people have also submitted)

  • 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.

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

(my) list.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();

/** * Checks if the passed circular list is empty. * @return true if the circular list is empty and false otherwise */ bool empty();

/** * Prints the values in the nodes of the list * from the first to the last. */ void print();

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; };

(my) 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; } }

bool List::empty() { return first == NULL; }

void List::print() { Iterator it = begin();

if (empty()) { cout

do { cout

} while (!it.equals(end())); }

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 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->next; 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 != NULL); position = position->previous; }

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

int main() { List staff;

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

cout

Iterator pos; pos = staff.begin();

cout

cout

// test all other methods cout

cout

cout

while (!staff.empty()) { pos = staff.erase(pos); cout

cout

cout

return 0; }

List2.cpp Indude Header files for string, assert and List2h #include #include #include #include #include Default List constructor to set first & last to NULL List:Listo first = NULL last = NULL; > il method to check if list is empty bool List:empty const compare first == NULL, which indicates empty list return first == NULL; 3 Print method to print the contents of the list void List:printo coutend(); it next() coutprevious = last; last->next = new_node; Ilmake last node as new node last - new_node ) now to make the link circular again, point last node to first and first to the new node last->next = first; first->previous = last: ) void List:insertiterator iter, strings) { / check if the position is null if (iter.position == NULL) // position is null indicates last node I hence insert at the end and exit push_back(s); return; ) # INsert in between case # create new node with data Node new_node = new Node(s): I create iterator to store the temp pointers Node after = iter.position Node before after->previous; Il reset the previous and next pointers apprpriately new_node->previous = before; new_node->next = after Il check if before pointer is last, which means we are inserting at the begining if( before == last) Il reset first to the new node first-new_node: # and firs point to the last node first->previous = last last->next first; 3 else 1 case where in between node before->next = new_node, } Iterator List erase/Iterator iter) Wifiter is null assert assert(iter position != NULL); 1/ store previous and next pointers Node remove = iter position : Node before = remove->previous Node after = remove->next; Il check if node to be removed is first if (remove == first) W if both first and last are equal then make the list empty if (first == last) { first = NULL; last = NULL; > else i/reset first to after and last pointer on first node. first = after; first->previous = last last->next = first; 3 ) else before->next = after il check if node to be removed is last if (remove == last) // if both first and last are not equal reset the last pointer to first if (first l= last) last before; first->previous = last, last->next = first } 3 else after->previous = before; Helete the node and return the arator Il delete the node and return the iterator delete remove Iterator: r.position after r.container = this; return; ) # this method returns the pointer to first position Iterator List::begin Iterator iter iter position = first; iter.container = this: return iter .) this iterator method returns pointer to the last position Iterator Listendo Iterator iter iter.position = last; iter.container=this: return iter 1 this iterator method retums pointer Iterator:Iterator position = NULL; container = NULL: } This method returns item at iter current position string Iterator:get() const { assertiposition != NULL); return position->data; ) // this iterator returns next pointer void Iterator:nexto assert(position != NULL); position position->next; > void Iterator-previous assert(position container->first); if(position == NULL) position = container-Slast; else position - position->previous, 1 bool Iterator.equals(Iterator b) const return position - b position ; cqueue.h #ifndef CQUEUE_H #define CQUEUE_H #include #include #include #include #include #include #include include Sinclude #include 'cqueue.h" using namespace std; I default constructor cqueue:cqueue - Listo s2 = 0 ) // This method returns the element at the begining string cqueue:fronto { Iterator itt = List begin(); return itr.get(); 3 // this method returns element at the last string cqueue back Iterator itr Listendo: return itr.get: # CHeck if list is empty bool cqueue empty const { return List.empty0; } Il pop internally calls the erase if size is > void cqueue-pop! if(sz>0){ Iterator itr = List begin(); List.erase(itr): SZ=1 1 } 1) push internally calls the push_back void cqueue push(string dat) { SZ += 1; List:push_back/dat): } int cqueue::Sizeconst return sz } void cqueue:printi List:printo: cout #include #include #include siostream> #include :/mnt/a/PR/jp$ g++ List2.h cqueue.h List2.cpp cqueue.cpp testList2.cpp qausr@\>:/mnt/d/PRJ/jcp$ ./a.out Empty - 1 Contents: 0 1 2 3 4, 5 Contents: 0 1 3 4 5 Contents : 1 3 4 5 Contents : 1 3 4 Contents : 1 3 xyz Empty - CQueue Empty ? - 1 Contents: 0 1 2 3,4,5 Size : 6 Front : 0 Back : 5Poping Contents : 1, 2, 3, 4, 5 Size : 5 Poping Contents : 2 , 3, 4, 5 Size : 4 Poping Contents : 3 4 5 Size : 3 Poping Contents : 4 Size : 2 Poping Contents : 5 Size : 1 Poping a.out: List2.cpp:152: std::string Iterator::get() const: Assertion `position != NULL' failed. Aborted qausr@\>:/mnt/d/PRJ/jcp$ .. 5 3

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 Programming Languages 12th International Symposium Dbpl 2009 Lyon France August 2009 Proceedings Lncs 5708

Authors: Philippa Gardner ,Floris Geerts

2009th Edition

3642037925, 978-3642037924

More Books

Students also viewed these Databases questions

Question

2. What would you do first if you were Jennifer?

Answered: 1 week ago

Question

Define procedural justice. How does that relate to unions?

Answered: 1 week ago