This is for c++.
Ok, I have most of this done but can't quite figure out how to get the queue to appear on screen. I also think I may have messed up my loops as well. What it is supposed to do is ask what the user wants to do and then it will carry that step out and then ask the user what to do again etc... until the user decides to quit. I was hoping you guys could help me with fixing my loops and getting the queue to appear on screen like it should.

//.H FILE
#include <iostream>
#define SIZE 30

using namespace std;

class queue
{
	int a[SIZE]; 
	int front;
	int rear;
	
	public:
		queue();
		~queue();
		void insert(int i);
		int remove();
		int isEmpty();
		int isFull();
		int print();
};

queue::queue()
{
	front = 0;
	rear = 0;
}

queue::~queue()
{
	delete []a;
}

void queue::insert(int i)
{
	if(isFull())
	{
		cout << "Queue is full.";
		return;
	}

	a[rear] = i;
	rear++;
}

int queue::remove()
{
	if(isEmpty())
	{
		cout << "Queue is empty.";
		return (-9999);
	}

	return(a[front++]);
}

int queue::isEmpty()
{
	if(front == rear)
		return 1;
	
	else
		return 0;
}

int queue::isFull()
{
	if(rear == SIZE)
		return 1;
	
	else
		return 0;
}

void print()
{
	//code for appearing on screen goes here I think

}
//C++ FILE
#include <iostream>
#include <iomanip>
#include "Lab 8.h"

using namespace std;

int main()
{
	queue list;
	int choice;
	int value;

	
	cout << "1. Insert and dispaly" << endl;
	cout << "2. Delete and display" << endl;
	cout << "3. Exit" << endl;
	cout << "Enter your choice: ";
	cin >> choice;

	do
	{
		if ( choice == 1 )
		{

			cout << "Enter your value to be inserted: ";
			cin >> value;

			cout << //Queue with added item will go here
			
		}

		else if(choice == 2)
		{
			cout << //Queue with deleted item will go here
			
			
		}
	}
	while ( choice != 3);
	{
		
	}

	if ( choice == 3)
	{
		cout << "Done." << endl;
		system ("PAUSE");
		return 0;
	}

system ("PAUSE");
return 0;
}

Hopefully I explained it enough so you guys can understand it. If more info is needed please just ask and I will provide it. Thank you for any help.

Recommended Answers

All 2 Replies

>can't quite figure out how to get the queue to appear on screen.
Complete the queue::print() function.
First of all, make print() a member function of the class. In the print () function, you should simply print your main data array which is. int a; from front to rear with help of a loop.

for(int i=front;i<rear;i++)
     cout<<a[i];

>I also think I may have messed up my loops as well
Yes indeed, you have messed up the Menu by not using switch-case.
Use this template

int main()
{
//define choice,value and list
do
{
    //Display the menu
    cout << "1. Insert and dispaly" << endl;
    cout << "2. Delete and display" << endl;
    cout << "3. Exit" << endl;
    cout << "Enter your choice: ";
    cin >> choice;//take choice
    switch (choice)
    {
    case 1://when choice ==1
        cout << "Enter your value to be inserted: ";
        cin >> value;
        //cout << //Queue with added item will go here
        //
        //
        break;
    case 2://when choice ==2
        ;//cout << //Queue with deleted item will go here

    }
}while(choice != 3);
return 0;
}//end of main

I guess there are two schools of thought regarding how to view the content of a queue. One school says you can only look at the top of the queue so you have to pop everything off the queue one element at a time to find out what's in there. The other school says you can view the contents by looking at the underlying container (see post by siddhant3s).

Queues use the first in first out protocol. If the underlying container is an array then one option to add at the end of the array (the element with the largest used index) and removal at the zero index---sliding all elements in the queue to the left by one each time an element is removed. That way you only have to keep track of the "rear" because the "front" is always at index number zero. In your scheme "rear" would be the index of the next element to be added.

one version of print could be:

while queue not empty
----display top of queue (that is, a[0])
----pop top from queue

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.