SlideShare a Scribd company logo
1. The design of a singly-linked list
below is a picture of the function that needs to be used
below is the code of the above picture:
#include <string>
#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
#define defaultSize 100
void Assert(bool val, string s)
{
if (!val)
{ // Assertion failed -- close the program
cout << "Assertion Failed: " << s << endl;
exit(-1);
}
}
template <typename E>
class Link {
public:
E element; // Value for this node
Link *next; // Pointer to next node in list
// Constructors
Link(const E& elemval, Link<E>* nextval = NULL)
{ element = elemval; next = nextval; }
Link(Link<E>* nextval =NULL) { next = nextval; }
};
template <typename E>
class LList: public Link<E> {
private:
Link<E>* head;// Intialization helper method
Link<E>* tail;// Pointer to last element
Link<E>* curr;// Access to current element
int cnt;// Size of list
void init(){// Intialization helper method
curr = tail = head = new Link<E>;
cnt = 0;
}
void removeall() {// Return link nodes to free store
while(head != NULL) {
curr = head;
head = head->next;
delete curr;
}
}
public:
LList(int size=defaultSize) { init(); }// Constructor
~LList() { removeall(); }// Destructor
void print() const;// Print list contents
void clear() { removeall(); init(); }// Clear list
// Insert "it" at current position
void insert(const E& it) {
curr->next = new Link<E>(it, curr->next);
if (tail == curr) tail = curr->next; // New tail
cnt++;
}
void append(const E& it) { // Append "it" to list
tail = tail->next = new Link<E>(it, NULL);
cnt++;
}
// Remove and return current element
E remove() {
Assert(curr->next != NULL, "No element");
E it = curr->next->element; // Remember value
Link<E>* ltemp = curr->next; // Remember link node
if (tail == curr->next) tail = curr; // Reset tail
curr->next = curr->next->next; // Remove from list
delete ltemp; // Reclaim space
cnt--; // Decrement the count
return it;
}
void moveToStart()// Place curr at list start
{ curr = head; }
void moveToEnd() // Place curr at list end
{ curr = tail; }
void prev(){
if (curr == head) return;
Link<E>* temp = head;
while (temp->next!=curr) temp=temp->next;
curr = temp;
}
void next(){ if (curr != tail) curr = curr->next; }
int length() const { return cnt; }
int currPos() const {
Link<E>* temp = head;
int i;
for (i=0; curr != temp; i++)
temp = temp->next;
return i;
}
void moveToPos(int pos){
Assert ((pos>=0)&&(pos<=cnt), "Position out of range");
curr = head;
for(int i=0; i<pos; i++) curr = curr->next;
}
const E& getValue() const {
Assert(curr->next != NULL, "No value");
return curr->next->element;
}
};
completed this code to fulfill the requirement below:
Write a function to insert an integer into a singly-linked list of elements arranged from largest to
smallest. Requires that elements remain ordered after insertion.
2. The design of array-based stack
below is a picture of the function that needs to be used
below is the code of the above picture:
#include <string.h>
#include <iostream>
using namespace std;
#define defaultSize 100
template <typename E>
class Stack {
private:
void operator =(const Stack&) {} // Protect assignment
Stack(const Stack&) {} // Protect copy constructor
public:
Stack() {} // Default constructor
virtual ~Stack() {} // Base destructor
// Reinitialize the stack. The user is responsible for
// reclaiming the storage used by the stack elements.
virtual void clear() = 0;
// Push an element onto the top of the stack.
// it: The element being pushed onto the stack.
virtual void push(const E& it) = 0;
// Remove the element at the top of the stack.
// Return: The element at the top of the stack.
virtual E pop() = 0;
// Return: A copy of the top element.
virtual const E& topValue() const = 0;
// Return: The number of elements in the stack.
virtual int length() const = 0;
};
template <typename E>
class AStack: public Stack<E> {
private:
int maxSize; // Maximum size of stack
int top; // Index for top element
E *listArray; // Array holding stack elements
public:
AStack(int size =defaultSize) // Constructor
{ maxSize = size; top = 0; listArray = new E[size]; }
~AStack() { delete [] listArray; } // Destructor
void clear() { top = 0; } // Reinitialize
void push(const E& it) { // Put "it" on stack
// Assert(top != maxSize, "Stack is full");
listArray[top++] = it;
}
E pop() { // Pop top element
// Assert(top != 0, "Stack is empty");
return listArray[--top];
}
const E& topValue() const { // Return top element
// Assert(top != 0, "Stack is empty");
return listArray[top-1];
}
int length() const { return top; } // Return length
};
completed this code to fulfill the requirement below:
Write a function that uses the stack to check whether the parentheses in the string are balanced.
For example, "(()(()))" is balanced, and "(()()" and "())" are unbalanced.
3. The design of array-based queue
below is a picture of the function that needs to be used
below is the code of the above picture:
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;
#define defaultSize 10
void Assert(bool val, string s)
{
if (!val)
{ // Assertion failed -- close the program
cout << "Assertion Failed: " << s << endl;
exit(-1);
}
}
// Abstract queue class
template <typename E> class Queue {
private:
void operator =(const Queue&) {} // Protect assignment
Queue(const Queue&) {} // Protect copy constructor
public:
Queue() {} // Default
virtual ~Queue() {} // Base destructor
// Reinitialize the queue. The user is responsible for
// reclaiming the storage used by the queue elements.
virtual void clear() = 0;
// Place an element at the rear of the queue.
// it: The element being enqueued.
virtual void enqueue(const E&) = 0;
// Remove and return element at the front of the queue.
// Return: The element at the front of the queue.
virtual E dequeue() = 0;
// Return: A copy of the front element.
virtual const E& frontValue() const = 0;
// Return: The number of elements in the queue.
virtual int length() const = 0;
};
// Array-based queue implementation
template <typename E> class AQueue: public Queue<E> {
private:
int maxSize; // Maximum size of queue
int front; // Index of front element
int rear; // Index of rear element
E *listArray; // Array holding queue elements
public:
AQueue(int size =defaultSize) { // Constructor
// Make list array one position larger for empty slot
maxSize = size+1;
rear = 0; front = 1;
listArray = new E[maxSize];
}
~AQueue() { delete [] listArray; } // Destructor
void clear() { rear = 0; front = 1; } // Reinitialize
void enqueue(const E& it) { // Put "it" in queue
Assert(((rear+2) % maxSize) != front, "Queue is full");
rear = (rear+1) % maxSize; // Circular increment
listArray[rear] = it;
}
E dequeue() { // Take element out
Assert(length() != 0, "Queue is empty");
E it = listArray[front];
front = (front+1) % maxSize; // Circular increment
return it;
}
const E& frontValue() const { // Get front value
Assert(length() != 0, "Queue is empty");
return listArray[front];
}
virtual int length() const // Return length
{ return ((rear+maxSize) - front + 1) % maxSize; }
};
completed this code to fulfill the requirement below:
Write a function that the user selects queue i (0i9), and then inserts any number of numbers into
queue i. Input "#" to indicate that the user does not select any queue. At the end of the program,
the non-empty queue among the 10 queues is output in the order of queue number from the
smallest to the largest.
For example
Input
0(queue number)
1 3 5 7 9 8
1(queue number)
2 4 6
9(queue number)
111 222 333
#
Output
1 3 5 7 9 8 2 4 6 111 222 333

More Related Content

PDF
ReversePoem.java ---------------------------------- public cl.pdf
ravikapoorindia
 
PDF
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
siennatimbok52331
 
PPT
Stack queue
Tony Nguyen
 
PPT
Stack queue
James Wong
 
PPT
Stack queue
Young Alista
 
PPT
Stack queue
Luis Goldster
 
PPT
Stack queue
Hoang Nguyen
 
PPT
Stack queue
Harry Potter
 
ReversePoem.java ---------------------------------- public cl.pdf
ravikapoorindia
 
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
siennatimbok52331
 
Stack queue
Tony Nguyen
 
Stack queue
James Wong
 
Stack queue
Young Alista
 
Stack queue
Luis Goldster
 
Stack queue
Hoang Nguyen
 
Stack queue
Harry Potter
 

Similar to 1- The design of a singly-linked list below is a picture of the functi (1).pdf (20)

PPT
Stack queue
Fraboni Ec
 
PPTX
The presention is about the queue data structure
gaurav77712
 
PDF
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
arorastores
 
PDF
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
forladies
 
PPTX
Templates
Farwa Ansari
 
PDF
Write a program that converts an infix expression into an equivalent.pdf
mohdjakirfb
 
DOCX
#include iostream#include d_node.h #include d_nodel.h.docx
ajoy21
 
PDF
File name a2.cppTaskFor this assignment, you are required to ei.pdf
infomalad
 
PDF
Were writing code for a project that dynamically allocates an arra.pdf
fsenterprises
 
PDF
For each task, submit your source java code file.(1) Objective Im.pdf
dhavalbl38
 
PPTX
Stack and Queue
Apurbo Datta
 
DOCX
@author Derek Harter @cwid 123 45 678 @class .docx
adkinspaige22
 
PDF
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
mallik3000
 
PDF
How do you stop infinite loop Because I believe that it is making a.pdf
feelinggift
 
PDF
VTU DSA Lab Manual
AkhilaaReddy
 
DOCX
Write a simple program in C (which comiles in gcc a unix environment ).docx
noreendchesterton753
 
PPTX
C++11 - STL Additions
GlobalLogic Ukraine
 
PDF
This is problem is same problem which i submitted on 22017, I just.pdf
fcaindore
 
PDF
C++ Nested loops, matrix and fuctions.pdf
yamew16788
 
PDF
Recursion to iteration automation.
Russell Childs
 
Stack queue
Fraboni Ec
 
The presention is about the queue data structure
gaurav77712
 
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
arorastores
 
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
forladies
 
Templates
Farwa Ansari
 
Write a program that converts an infix expression into an equivalent.pdf
mohdjakirfb
 
#include iostream#include d_node.h #include d_nodel.h.docx
ajoy21
 
File name a2.cppTaskFor this assignment, you are required to ei.pdf
infomalad
 
Were writing code for a project that dynamically allocates an arra.pdf
fsenterprises
 
For each task, submit your source java code file.(1) Objective Im.pdf
dhavalbl38
 
Stack and Queue
Apurbo Datta
 
@author Derek Harter @cwid 123 45 678 @class .docx
adkinspaige22
 
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
mallik3000
 
How do you stop infinite loop Because I believe that it is making a.pdf
feelinggift
 
VTU DSA Lab Manual
AkhilaaReddy
 
Write a simple program in C (which comiles in gcc a unix environment ).docx
noreendchesterton753
 
C++11 - STL Additions
GlobalLogic Ukraine
 
This is problem is same problem which i submitted on 22017, I just.pdf
fcaindore
 
C++ Nested loops, matrix and fuctions.pdf
yamew16788
 
Recursion to iteration automation.
Russell Childs
 
Ad

More from afgt2012 (20)

PDF
1- The book discusses cues that we should be aware of when someone beg.pdf
afgt2012
 
PDF
1- The principle basis for the classification of organisms into the Ki.pdf
afgt2012
 
PDF
1- The diagram below shows some features of the Earth's crust and uppe.pdf
afgt2012
 
PDF
1- tell me the enzyme mechanism-biosynthetic pathway 2- tell me what i.pdf
afgt2012
 
PDF
1- The image below shows a pure phospholipid bilayer- plus water on ei.pdf
afgt2012
 
PDF
1- Suppose that the economy is in a recession- Show graphically how Ke.pdf
afgt2012
 
PDF
1- The history of economic growth The following two graphs show GDP pe.pdf
afgt2012
 
PDF
1- The hormone is released from the hypothalamus that causes the rele.pdf
afgt2012
 
PDF
1- The Fayette County Health Department was notified that Amy Cress- a.pdf
afgt2012
 
PDF
1- The first exercise is to find the various partials of CE(S-t) in or.pdf
afgt2012
 
PDF
1- Which of the following changes are NOT evidence of migrating magma-.pdf
afgt2012
 
PDF
1- Which of the following describes what businesses should do at the t.pdf
afgt2012
 
PDF
1- Which item below is NOT an example of scientific evidence- Group of.pdf
afgt2012
 
PDF
1- Where does the energy come from to pump H+ ions across the inner mi.pdf
afgt2012
 
PDF
1- When you enter dates in the Criteria row- Access places _______ sym.pdf
afgt2012
 
PDF
1- Which factor do Heckscher-Ohlin identify as the basic determinants.pdf
afgt2012
 
PDF
1- When a researcher ensures that they are reducing the possiblily of.pdf
afgt2012
 
PDF
1- What were your intial reactions to the film- 2- Did you learn anyth.pdf
afgt2012
 
PDF
1- What types of utilities are in operation in the US-- a- Privately-o.pdf
afgt2012
 
PDF
1- When adding a title to the header of your form or report- Access wi.pdf
afgt2012
 
1- The book discusses cues that we should be aware of when someone beg.pdf
afgt2012
 
1- The principle basis for the classification of organisms into the Ki.pdf
afgt2012
 
1- The diagram below shows some features of the Earth's crust and uppe.pdf
afgt2012
 
1- tell me the enzyme mechanism-biosynthetic pathway 2- tell me what i.pdf
afgt2012
 
1- The image below shows a pure phospholipid bilayer- plus water on ei.pdf
afgt2012
 
1- Suppose that the economy is in a recession- Show graphically how Ke.pdf
afgt2012
 
1- The history of economic growth The following two graphs show GDP pe.pdf
afgt2012
 
1- The hormone is released from the hypothalamus that causes the rele.pdf
afgt2012
 
1- The Fayette County Health Department was notified that Amy Cress- a.pdf
afgt2012
 
1- The first exercise is to find the various partials of CE(S-t) in or.pdf
afgt2012
 
1- Which of the following changes are NOT evidence of migrating magma-.pdf
afgt2012
 
1- Which of the following describes what businesses should do at the t.pdf
afgt2012
 
1- Which item below is NOT an example of scientific evidence- Group of.pdf
afgt2012
 
1- Where does the energy come from to pump H+ ions across the inner mi.pdf
afgt2012
 
1- When you enter dates in the Criteria row- Access places _______ sym.pdf
afgt2012
 
1- Which factor do Heckscher-Ohlin identify as the basic determinants.pdf
afgt2012
 
1- When a researcher ensures that they are reducing the possiblily of.pdf
afgt2012
 
1- What were your intial reactions to the film- 2- Did you learn anyth.pdf
afgt2012
 
1- What types of utilities are in operation in the US-- a- Privately-o.pdf
afgt2012
 
1- When adding a title to the header of your form or report- Access wi.pdf
afgt2012
 
Ad

Recently uploaded (20)

DOCX
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
PPTX
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
PDF
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
PDF
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
PPTX
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PPTX
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PDF
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
PPTX
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
PPTX
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
PDF
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
PROTIEN ENERGY MALNUTRITION: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 

1- The design of a singly-linked list below is a picture of the functi (1).pdf

  • 1. 1. The design of a singly-linked list below is a picture of the function that needs to be used below is the code of the above picture: #include <string> #include <iostream> #include <cmath> #include <cstdio> #include <algorithm> using namespace std; #define defaultSize 100 void Assert(bool val, string s) { if (!val) { // Assertion failed -- close the program cout << "Assertion Failed: " << s << endl; exit(-1); } } template <typename E> class Link { public: E element; // Value for this node Link *next; // Pointer to next node in list
  • 2. // Constructors Link(const E& elemval, Link<E>* nextval = NULL) { element = elemval; next = nextval; } Link(Link<E>* nextval =NULL) { next = nextval; } }; template <typename E> class LList: public Link<E> { private: Link<E>* head;// Intialization helper method Link<E>* tail;// Pointer to last element Link<E>* curr;// Access to current element int cnt;// Size of list void init(){// Intialization helper method curr = tail = head = new Link<E>; cnt = 0; } void removeall() {// Return link nodes to free store while(head != NULL) { curr = head; head = head->next; delete curr; } }
  • 3. public: LList(int size=defaultSize) { init(); }// Constructor ~LList() { removeall(); }// Destructor void print() const;// Print list contents void clear() { removeall(); init(); }// Clear list // Insert "it" at current position void insert(const E& it) { curr->next = new Link<E>(it, curr->next); if (tail == curr) tail = curr->next; // New tail cnt++; } void append(const E& it) { // Append "it" to list tail = tail->next = new Link<E>(it, NULL); cnt++; } // Remove and return current element E remove() { Assert(curr->next != NULL, "No element"); E it = curr->next->element; // Remember value Link<E>* ltemp = curr->next; // Remember link node if (tail == curr->next) tail = curr; // Reset tail curr->next = curr->next->next; // Remove from list delete ltemp; // Reclaim space
  • 4. cnt--; // Decrement the count return it; } void moveToStart()// Place curr at list start { curr = head; } void moveToEnd() // Place curr at list end { curr = tail; } void prev(){ if (curr == head) return; Link<E>* temp = head; while (temp->next!=curr) temp=temp->next; curr = temp; } void next(){ if (curr != tail) curr = curr->next; } int length() const { return cnt; } int currPos() const { Link<E>* temp = head; int i; for (i=0; curr != temp; i++) temp = temp->next; return i; } void moveToPos(int pos){
  • 5. Assert ((pos>=0)&&(pos<=cnt), "Position out of range"); curr = head; for(int i=0; i<pos; i++) curr = curr->next; } const E& getValue() const { Assert(curr->next != NULL, "No value"); return curr->next->element; } }; completed this code to fulfill the requirement below: Write a function to insert an integer into a singly-linked list of elements arranged from largest to smallest. Requires that elements remain ordered after insertion. 2. The design of array-based stack below is a picture of the function that needs to be used below is the code of the above picture: #include <string.h> #include <iostream> using namespace std; #define defaultSize 100 template <typename E> class Stack { private: void operator =(const Stack&) {} // Protect assignment Stack(const Stack&) {} // Protect copy constructor
  • 6. public: Stack() {} // Default constructor virtual ~Stack() {} // Base destructor // Reinitialize the stack. The user is responsible for // reclaiming the storage used by the stack elements. virtual void clear() = 0; // Push an element onto the top of the stack. // it: The element being pushed onto the stack. virtual void push(const E& it) = 0; // Remove the element at the top of the stack. // Return: The element at the top of the stack. virtual E pop() = 0; // Return: A copy of the top element. virtual const E& topValue() const = 0; // Return: The number of elements in the stack. virtual int length() const = 0; }; template <typename E> class AStack: public Stack<E> { private: int maxSize; // Maximum size of stack int top; // Index for top element E *listArray; // Array holding stack elements
  • 7. public: AStack(int size =defaultSize) // Constructor { maxSize = size; top = 0; listArray = new E[size]; } ~AStack() { delete [] listArray; } // Destructor void clear() { top = 0; } // Reinitialize void push(const E& it) { // Put "it" on stack // Assert(top != maxSize, "Stack is full"); listArray[top++] = it; } E pop() { // Pop top element // Assert(top != 0, "Stack is empty"); return listArray[--top]; } const E& topValue() const { // Return top element // Assert(top != 0, "Stack is empty"); return listArray[top-1]; } int length() const { return top; } // Return length }; completed this code to fulfill the requirement below: Write a function that uses the stack to check whether the parentheses in the string are balanced. For example, "(()(()))" is balanced, and "(()()" and "())" are unbalanced. 3. The design of array-based queue below is a picture of the function that needs to be used
  • 8. below is the code of the above picture: #include <string> #include <iostream> #include <cstdlib> using namespace std; #define defaultSize 10 void Assert(bool val, string s) { if (!val) { // Assertion failed -- close the program cout << "Assertion Failed: " << s << endl; exit(-1); } } // Abstract queue class template <typename E> class Queue { private: void operator =(const Queue&) {} // Protect assignment Queue(const Queue&) {} // Protect copy constructor public: Queue() {} // Default virtual ~Queue() {} // Base destructor // Reinitialize the queue. The user is responsible for
  • 9. // reclaiming the storage used by the queue elements. virtual void clear() = 0; // Place an element at the rear of the queue. // it: The element being enqueued. virtual void enqueue(const E&) = 0; // Remove and return element at the front of the queue. // Return: The element at the front of the queue. virtual E dequeue() = 0; // Return: A copy of the front element. virtual const E& frontValue() const = 0; // Return: The number of elements in the queue. virtual int length() const = 0; }; // Array-based queue implementation template <typename E> class AQueue: public Queue<E> { private: int maxSize; // Maximum size of queue int front; // Index of front element int rear; // Index of rear element E *listArray; // Array holding queue elements public: AQueue(int size =defaultSize) { // Constructor // Make list array one position larger for empty slot
  • 10. maxSize = size+1; rear = 0; front = 1; listArray = new E[maxSize]; } ~AQueue() { delete [] listArray; } // Destructor void clear() { rear = 0; front = 1; } // Reinitialize void enqueue(const E& it) { // Put "it" in queue Assert(((rear+2) % maxSize) != front, "Queue is full"); rear = (rear+1) % maxSize; // Circular increment listArray[rear] = it; } E dequeue() { // Take element out Assert(length() != 0, "Queue is empty"); E it = listArray[front]; front = (front+1) % maxSize; // Circular increment return it; } const E& frontValue() const { // Get front value Assert(length() != 0, "Queue is empty"); return listArray[front]; } virtual int length() const // Return length { return ((rear+maxSize) - front + 1) % maxSize; }
  • 11. }; completed this code to fulfill the requirement below: Write a function that the user selects queue i (0i9), and then inserts any number of numbers into queue i. Input "#" to indicate that the user does not select any queue. At the end of the program, the non-empty queue among the 10 queues is output in the order of queue number from the smallest to the largest. For example Input 0(queue number) 1 3 5 7 9 8 1(queue number) 2 4 6 9(queue number) 111 222 333 # Output 1 3 5 7 9 8 2 4 6 111 222 333