Question
c++ visual studio 2019 Do not use #include (I need a different method than the one below to implement a circular queue) (was told by
c++ visual studio 2019
Do not use #include
(I need a different method than the one below to implement a circular queue) (was told by my professor that other students had the same method)
- 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. |
| Prints all values in the queue, from the front to the back. |
List2.cpp
// Include Header files for string , assert and List2.h #include
using namespace std;
// Node Constructor with string param Node::Node(string s) { // Set the data to string and previous and next to NULL data = s; previous = NULL; next = NULL; }
// Default List constructor to set first & last to NULL List::List() { first = NULL; last = NULL; }
// method to check if list is empty bool List::empty() const { // compare first == NULL , which indicates empty list return first == NULL ; }
// Print method to print the contents of the list void List::print() { cout << " Contents : " ; // Iterate the list using iterator starting from begining Iterator itr = begin(); for(; !itr.equals( this->end()) ; itr.next() ) { cout << itr.get() << " , " ; } // print the last element cout << itr.get() << " "; } // attach the element at the last void List::push_back(string data) { // create a new node with given data Node* new_node = new Node(data); // compare if the last == null in which case empty list if (last == NULL ) { // make first and last point to the new node first = new_node; last = new_node; } else { // list is not empty // link last node and new node point to each other new_node->previous = last ; last->next = new_node; //make 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::insert(Iterator iter, string s) { // check if the position is null if (iter.position == NULL ) { // position is null indicates last node // 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); // create iterator to store the temp pointers Node* after = iter.position ; Node* before = after->previous; // reset the previous and next pointers apprpriately new_node->previous = before ; new_node->next = after ; // check if before pointer is last , which means we are inserting at the begining if( before == last) { // reset first to the new node first = new_node ; // and firs point to the last node first->previous = last ; last->next = first ; } else { // case where in between node before->next = new_node; } }
Iterator List::erase(Iterator iter) { // if iter is null assert assert(iter.position != NULL ); // store previous and next pointers Node* remove = iter.position ; Node* before = remove->previous; Node* after = remove->next ; // check if node to be removed is first if ( remove == first ) { // if both first and last are equal then make the list empty if ( first == last ) { first = NULL ; last = NULL ; } else { // reset first to after and last pointer on first node. first = after ; first->previous = last; last->next = first ; } } else before->next = after ; // 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 != last ) { last = before ; first->previous = last; last->next = first ; } } else { after->previous = before; } // delete the node and return the iterator delete remove ; Iterator r ; r.position = after ; r.container = this; return r; }
// 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 List::end() { Iterator iter ; iter.position = last ; iter.container = this; return iter; }
// this iterator method returns pointer Iterator::Iterator() { position = NULL ; container = NULL; }
// This method returns item at iter current position string Iterator::get() const { assert(position != NULL ); return position->data ; }
// this iterator returns next pointer 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 ; } cqueue.h
#ifndef CQUEUE_H #define CQUEUE_H
#include
using namespace std;
class cqueue : private List { public : cqueue(); string front(); string back() ; bool empty() const ; void pop() ; void push(string dat); int size() const ; void print() ; private : int sz ;
};
#endif cqueue.cpp
#include
using namespace std; // default constructor cqueue::cqueue() : List() { sz = 0 ; }
// THis method returns the element at the begining string cqueue::front() { Iterator itr = List::begin() ; return itr.get(); }
// this method returns element at the last string cqueue::back() { Iterator itr = List::end() ; return itr.get(); }
// CHeck if list is empty bool cqueue::empty() const { return List::empty(); }
// pop internally calls the erase if size is >0 void cqueue::pop() { if ( sz > 0 ) { Iterator itr = List::begin(); List::erase(itr); sz -= 1; } }
// push internally calls the push_back void cqueue::push(string dat ) { sz += 1; List::push_back(dat); }
int cqueue::size() const { return sz; }
void cqueue::print() { List::print(); cout << " Size : " << size() << " "; } testDriver.cpp
#include
using namespace std;
int main() { // Tests for Circulat List List cl ; cout << " Empty - " << cl.empty() << " "; for ( int i = 0 ; i < 6 ; i++ ) { cl.push_back(to_string( i )) ; } cl.print(); Iterator itr = cl.begin() ; itr.next() ; itr.next() ; //cl.insert(itr,"xyz"); //itr.next() ; //itr.next() ;
cl.erase(itr); cl.print(); Iterator itr1 = cl.begin() ; cl.erase(itr1); cl.print();
Iterator itr0 = cl.end() ; cl.erase(itr0); cl.print(); Iterator itr2 = cl.begin() ; itr2.next() ; itr2.next() ; cl.insert(itr2,"xyz"); cl.print(); cout << " Empty - " << cl.empty() << " "; // Test for Circular Queue cqueue cq ; cout << " CQueue Empty ? - " << cq.empty() << " "; for ( int i = 0 ; i < 6 ; i++ ) { cq.push(to_string( i )) ; } cq.print(); cout << " Front : " << cq.front() ; cout << " Back : " << cq.back() ; cout << "Poping .. "; cq.pop(); cq.print(); cout << "Poping .. "; cq.pop(); cq.print(); cout << "Poping .. "; cq.pop(); cq.print(); cout << "Poping .. "; cq.pop(); cq.print(); cout << "Poping .. "; cq.pop(); cq.print(); cout << "Poping .. "; cq.pop(); cq.print(); //for( ; !cq.empty() ; cq.pop()); cq.print(); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started