//
// Queue.h - Class Exercise 2
//
// Created by Tina Huynh on 2/8/18.
// Copyright © 2018 Tina Huynh. All rights reserved.
//
// CMPR 131 - Data Structures
#include <iostream>
#include <string>
using namespace std;
const int SIZE = 5;
int id;
string color;
struct Node
{
int id;
string color;
};
class NodeQueue
{
private:
Node items[SIZE];
int front;
int back;
public:
NodeQueue() { front = back = SIZE - 1; }
~NodeQueue() {}
void makeEmpty() { front = back = SIZE - 1; }
void enqueue(int id, string color)
{
if(front == SIZE - 1)
front = 0;
back = ((back + 1) % SIZE);
items[back].id = id;
items[back].color = color;
}
void dequeue(int id, string color)
{
front = ((front + 1) % SIZE);
id = items[front].id;
color = items[front].color;
}
bool isEmpty() { return (front == back); }
bool isFull() { return ((back + 1) % SIZE == front); }
void showQueue()
{
int i = front;
do {
cout << items[i].id << " - " << items[i].color << endl;
i = ((i + 1) % SIZE);
} while (i != back);
cout << items[back].id << " - " << items[back].color << endl;
cout << "NULL" << endl;
, }
};
-----------------------------------------------------------------------------
// Attatched: Lecture 6 Extra Credit
//
// main.cpp - Class Exercise 2
//
// Created by Tina Huynh on 2/8/18.
// Copyright © 2018 Tina Huynh. All rights reserved.
//
// CMPR 131 - Data Structures
#include "Queue.h"
void showMenu();
int main()
{
NodeQueue queue;
char choice;
queue.makeEmpty();
while(toupper(choice) != 'Q')
{
showMenu();
cout << "Enter your choice: ";
cin >> choice;
switch (toupper(choice))
{
case 'E' :
{
if (queue.isFull())
{
cout << "Queue is full" << endl;
break;
}
cout << "Enter a new node: " << endl;
cout << "ID: ";
cin >> id;
cin.ignore();
cout << "Color: ";
getline(cin, color);
queue.enqueue(id, color);
break;
}
case 'D' :
{
if (queue.isEmpty())
{
cout << "Queue is empty" << endl;
break;
}
queue.dequeue(id, color);
break;
}