Question

Implement a class of char Queue using the concept of array with the following member functions:...

Implement a class of char Queue using the concept of array with the following member functions: push, pop (void), peek, empty, and full as explained in the lecture.

In C++

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>

using namespace std;

class Queue {

public:

char* A;

int front = -1;

int rear = -1;

int aSize;

Queue() {}

Queue(int n) {A = new char[n];aSize = n;}

void Push(char x) { //O(1)

if (isFull()) { cout << "No space to store value" << endl; return; }

if (Empty()) {front++;}

//rear++;

rear = (rear+1) % aSize;

A[rear] = x;

}

bool Empty() { //O(1)

if (front == -1 && rear == -1) return true;

else return false;

}

bool isFull() { //O(1)

//if (rear >= aSize - 1) return true;

if (front == (rear + 1) % aSize) return true;

else return false;

}

void Print() { //O(n)

if (Empty()) { cout << "Nothing to print" << endl; return; }

if (front > rear) { //For circular queue

for(int i=front; i<aSize;i++)

cout << A[i] << " ";

for(int i=0;i<=rear; i++)

cout << A[i] << " ";

}

else { //For normal queue

for (int i = front; i <= rear; i++) {

cout << A[i] << " ";

}

}

cout << endl;

}

void Pop() { //O(1)

if (front == rear && front != -1){ //Last element in the queue

front = -1;

rear = -1;

}

else {

front = (front+1)%aSize;

}

}

char DequeBack(){

char value = A[front];

if (front == rear && front != -1){

value = A[front];

front = -1;

rear = -1;

}

else {

front = (front+1)%aSize;

}

return value;

}

char Peek(){

if (Empty()) { cout << "Queue is Empty" << endl; return '%'; }

char value = A[front];

return value;

}

};

int main() {

Queue* queue = new Queue(6);

queue->Push('A'); queue->Print();//A

queue->Push('B'); queue->Print();//A B

queue->Push('C'); queue->Print();//A B C

queue->Push('D'); queue->Print();//A B C D

cout << "Peek: "<<queue->Peek() << endl;

queue->Print();

queue->Pop();

cout << "After Popping an element, the queue is: ";

queue->Print();

}

======================================================
SEE OUTPUT

Files > clang++-7 -pthread -std=c++17 -o main main.cpp > ./main main.cpp A AB ABC ABCD Peek: A ABCD After Popping an element,

========================================
Thanks, let me now if there is any concern.

Add a comment
Know the answer?
Add Answer to:
Implement a class of char Queue using the concept of array with the following member functions:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. (40’) In myStack.cpp, implement the member functions of the class myStack, which is the class...

    1. (40’) In myStack.cpp, implement the member functions of the class myStack, which is the class for integer stacks. 2. (20’) In stackTest.cpp, complete the implementation of function postfixTest(), which use an integer stack to evaluate post-fix expressions. For simplicity, you can assume the post-fix expression is input character by character (i.e., not an entire string), and each operand is a non-negative, single-digit integer (i.e., 0,1,…,9). However, you are supposed to detect invalid/ illegal post-fix expression input, e.g., “4 5...

  • In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and...

    In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and dequeue functions. You could use either the array or linked list implementation for stacks and queues. Source for stack array: --------------------------------------------------- #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the...

  • Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds value...

    Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds values of double data type. 1.) Create a program that produces the following output OUTPUT: Q Quit Enter your choice: e Enter an item: 1.1 E Enqueue D Dequeue s-show queue ← showMenuO function called in main) OQuit // screen clears-.. continue enqueuing.screen clearing with each iteration Enter your choice: e Queue is full. E Enqueue D Dequeue s Show queue 0...

  • C++ ((USE STL verison please)) Implement the queue class as shown above. Use your queue to...

    C++ ((USE STL verison please)) Implement the queue class as shown above. Use your queue to store the names of 5 students waiting outside Student Services to meet with advisors. You may want to add more data to each node including student ID, Major, etc. Add a member function displayQueue which displays the contents of the queue. Write the main function to test the queue. CLASS: #ifdef queue_h #define queue_h namespace queues { struct queueNode{ char data; queueNode *link; };...

  • QUESTION 1: Queue Class: Write a Queue class using doubly-linked structure and implement the following functionalities....

    QUESTION 1: Queue Class: Write a Queue class using doubly-linked structure and implement the following functionalities. enqueue (inserts element to the end) dequeue (removes the front element and provides content) isEmpty (checks whether the Queue is empty or not) makeEmpty () peek (provides the element sitting at the top/front, but does not remove) print (prints all the elements from front to the end) reversePrint(prints all the elements from end to the front with the help of back pointers inside your...

  • (Please do in C++) Implement a Queue using a vector or the STD ::queue:: class Note...

    (Please do in C++) Implement a Queue using a vector or the STD ::queue:: class Note the difference in what it takes to implement using a char[] array vs a vector or the STD ::queue:: class The user will input a string and the program will return the string with each letter in the string duplicated. Displaying correct Queue implementation. Utilize the conventional methods as needed. Please input String: happy hhaappyy

  • The class pictured below is designed to implement an integer queue using two stacks. Assume the...

    The class pictured below is designed to implement an integer queue using two stacks. Assume the stack methods all work as desired (though their implementations are not shown). .(a) Trace what happens in the following situation, showing intermediate steps (values of variables, what is in the stacks, and what is in the queue at various points in the methods, not just the results of the methods). • Start with an empty queue. Enqueue 5, then 16, then 7. Dequeue twice....

  • Use Java to implement a basic stack using an array of integers. For the stack, you...

    Use Java to implement a basic stack using an array of integers. For the stack, you will create an array of integers that holds 5 numbers. To make it easier, you can declare the array at the class level. That way you will be able to use the array in any method in your class without using parameters. Your input/output interface should look something like the following: What operation do you want to do? push What number do you want...

  • Suppose I want to implement the public member functions of a Stack (push, pop, top, size,...

    Suppose I want to implement the public member functions of a Stack (push, pop, top, size, isEmpty). However, instead of the private member data we had in class, I have only a single Queue. You may assume that the Queue has unbounded capacity, but is otherwise as per the one shown in class. Explain at a high level (you do not need to provide C++ code, but someone should be able to understand how you would code it from what...

  • Question1 ) The STL provides similiar classes called stack and queue, both of which gave functions...

    Question1 ) The STL provides similiar classes called stack and queue, both of which gave functions called push and pop. Answer the following questions: 1.1) Does the push perform a different action in the stack class then in the queue class? Explain the answer. 1.2) Does the pop perform a different action in the stack class then in the queue class? Explain the answer. Question 2) 2.1) Suppose you are tasked with implementing a reverse queue in which elements are...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT