I'm trying to implement a queue using an array within C++. I cannot for the life of me figure out why some of my methods are not working properly. My exists method does not find a value that's in the queue and my duplicate method is duplicating the rear of my queue instead of the front. The duplicate method should take whatever is at the front of the queue (given it's not empty or full), copy that value, and put it at the front of the queue. Mine will take the rear value, copy it, and put it one position from the rear. In addition, the Enqueue method is not properly inserting values.
Here's my code, I'm not 100% sure what really works well or what doesnt.
#include <iostream>
#include "queue.h"
using namespace std;
static int nums[10];
int front = 0, rear = 0, sizeOfArray = 0;
int initialCapacity = 10;
int Enqueue(int num) {
if (sizeOfArray == initialCapacity) {
return -58;
}
for (int i = 0; i <
initialCapacity; i++) {
if (nums[i] ==
num) {
return -62;
}
else {
nums[rear] = num;
rear = (rear + 1);
sizeOfArray++;
return 0;
}
}
}
int Dequeue(int& num) {
if (sizeOfArray == 0) {
return -64;
}
front = (front + 1);
sizeOfArray--;
return 0;
}
int isEmpty() {
if (sizeOfArray == 0) {
return 1;
}
else {
return 0;
}
}
int Exists(int num) {
for (int i = 0; i < sizeOfArray; i++) {
int index = (front + i);
if (nums[index] == num) {
return 1;
}
else {
return 0;
}
}
return 0;
}
void Clear(void) {
front = rear = sizeOfArray = 0;
}
void Print(void) {
for (int i = 0; i < sizeOfArray; i++) {
cout << nums[i] << "
";
}
cout << endl;
}
int Duplicate(void) {
if (sizeOfArray == 0) {
return -78;
}
if (sizeOfArray == initialCapacity) {
return -81;
}
int dupeNum;
dupeNum = nums[front];
nums[front + 1] = dupeNum;
sizeOfArray++;
return 0;
}
Whenever I do the following methods in a test driver: Enqueue 4 Enqueue 5 Exists 5 Duplicate Print
I get this output:
Test 1 0 - 0 indicates success
Test 2 0 - 0 indicates success
Test 3 0 - 0 indicates a failure
Test 4 0 - indicates a success
4 4 0
In case of any queries, please revert back. I have fixed the enqueue and Duplicate Methods.
#include <iostream>
// #include "queue.h"
using namespace std;
/* Made Some Changes Here..... */
static int nums[10];
int front = 0, sizeOfArray = 0;
int initialCapacity = 10;
//Keep rear as start capacity - 1 for proper function of
enqueue
int rear=initialCapacity-1;
int Enqueue(int num) {
if (sizeOfArray == initialCapacity) {
return -58;
}
/* As we kept rear as start capacity - 1, we dont need
loop */
/* Increment rear.If it goes above capacity but queue
is not full, we do modulus of intitialCapacity */
rear = (rear +
1)%initialCapacity;
nums[rear] = num;
sizeOfArray++;
return 0;
}
int Dequeue(int num) {
if (sizeOfArray == 0) {
return -64;
}
front = (front + 1)%initialCapacity;
sizeOfArray--;
return 0;
}
int isEmpty() {
if (sizeOfArray == 0) {
return 1;
}
else {
return 0;
}
}
int Exists(int num) {
for (int i = 0; i < sizeOfArray; i++) {
int index = (front + i);
if (nums[index] == num) {
return 1;
}
else {
return 0;
}
}
return 0;
}
void Clear(void) {
front = rear = sizeOfArray = 0;
}
void Print(void) {
for (int i = 0; i < 10; i++) {
cout << nums[i] << " ";
}
cout << endl;
}
//The Logic was Wrong.
int Duplicate(void) {
if (sizeOfArray == 0) {
return -78;
}
if (sizeOfArray == initialCapacity) {
return -81;
}
int dupeNum;
dupeNum = nums[front];
//first increase size
sizeOfArray++;
//put start of queue ie. front as temp
int temp=nums[front],x,i=front;
//now loop from front till size of queueu
while(i<sizeOfArray){
//Shift All Numbers to the right.
x=nums[i];
nums[i]=temp;
temp=x;
i++;
}
//You will see that it generates duplicate
return 0;
}
int main(){
int a = Enqueue(47);
int b = Enqueue(97);
int c = Enqueue(60);
Print();
Duplicate();
Print();
return 0;
}

I'm trying to implement a queue using an array within C++. I cannot for the life...
Help me solve this in C++ Rewrite the code to use array instead of linked lists. Please do not change anything besides the data structure. Instead of linked list use an array. The functionality should remain exactly the same. You can prepare a class if you wish but it is not required. /** * Queue implementation using linked list C style implementation ( no OOP). */ #include <cstdio> #include <cstdlib> #include <climits> #include <iostream> #define CAPACITY 100 // Queue max...
How do I pass values to this function? class DynIntQueue { struct QueueNode { int value; QueueNode *next; QueueNode(int value1, QueueNode *next1 = nullptr) { value = value1; next = next1; } }; // These track the front and rear of the queue QueueNode *front; QueueNode *rear; public: // Constructor and Destructor DynIntQueue(); ~DynIntQueue(); // Member functions void enqueue(int); void dequeue(int &); bool isEmpty() const; void clear(); }; main #include <iostream> #include "DynIntQueue.h" using namespace std; int main() {DynIntQueue list;...
HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD
COMMENTS:
stackARRAY:
#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 top element from Stack
int topElement(); // get the top element
void display(); // display Stack elements from top to bottom
};
void Stack...
PROBLEM- void CBQueue::printF2B( ) If the queue is empty, the method should print “Queue is empty”, otherwise, this method should print the items in the queue starting at the front of the queue and proceeding to the rear of the queue. The items should be printed one per line. Now that this method is written, you can do a more thorough job of testing enqueue( ). You will want to call printF2B( ) and printB2F( ) after implementing each method...
PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same *************************************************************************************************************** /** * Stack implementation using array in C/procedural language. */ #include <iostream> #include <cstdio> #include <cstdlib> //#include <climits> // For INT_MIN #define SIZE 100 using namespace std; /// Create a stack with capacity of 100 elements int stack[SIZE]; /// Initially stack is...
Write a function that takes a string parameter and determines whether the string contains matching grouping symbols. Grouping symbols are parenthesis ( ) , brackets [] and curly braces { }. For example, the string {a(b+ac)d[xy]g} and kab*cd contain matching grouping symbols. However, the strings ac)cd(e(k, xy{za(dx)k, and {a(b+ac}d) do not contain matching grouping symbols. (Note: open and closed grouping symbols have to match both in number and in the order they occur in the string). Your function must use...
I need help fixing my code. My output should be the following. Hello, world! : false A dog, a panic in a pagoda : true A dog, a plan, a canal, pagoda : true Aman, a plan, a canal--Panama! : true civic : true If I had a hi-fi : true Do geese see God? : true Madam, I’m Adam. : true Madam, in Eden, I’m Adam. : true Neil, a trap! Sid is part alien! : true Never odd...
Create a program named NonZeroNumQueue.java then copy the contents from NonZeroNumQueue.txt. Then implement enqueue method. To get credit, DO NOT add or alter any data members/methods except the enqueue(int num). The NonZeroNumQueue will NOT accept zero, which mean you can enqueue any whole numbers EXCEPT 0(zero). You have already noticed that the queue is NEVER full, so the isFull() always returns false. given NonZeroNumQueue.txt public class NonZeroNumQueue{ private int[] data; private int total; private int front; private int tail; public...
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...
Hello, I have some errors in my C++ code when I try to debug it.
I tried to follow the requirements stated below:
Code:
// Linked.h
#ifndef INTLINKEDQUEUE
#define INTLINKEDQUEUE
#include <iostream>
usingnamespace std;
class IntLinkedQueue
{
private: struct Node {
int data;
Node *next;
};
Node *front; // -> first item
Node *rear; // -> last item
Node *p; // traversal position
Node *pp ; // previous position
int size; // number of elements in the queue
public:
IntLinkedQueue();...