• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 2 out of 5 pages
Other

Lecture 6 Extra Credit main.cpp - Class Exercise 2

Document preview thumbnail
Preview 2 out of 5 pages

The provided code outlines a simple queue management system using a menu-driven interface. The main components are: 1. **Initialization**: The program starts by creating an instance of `NodeQueue` and calling the `makeEmpty` method to reset the queue. 2. **Menu Display**: The `showMenu` function presents the user with options to enqueue, dequeue, display the queue, or quit the program. 3. **User Input Handling**: The program enters a loop where it continually prompts for user input until the user chooses to quit (by entering 'Q'). The input can be one of the following: - **E (Enqueue)**: Users can add a new node to the queue by providing an ID and a color. The program checks if the queue is full before adding a new node. - **D (Dequeue)**: Users can remove a node from the front of the queue, provided it isn’t empty. - **S (Show Queue)**: Displays all nodes in the queue, listing their IDs and colors. - **Q (Quit)**: Exits the loop and ends the program. 4. **Queue Management**: The queue operations rely on the `enqueue`, `dequeue`, and `showQueue` methods of the `NodeQueue` class, which manage the internal storage of nodes. The program handles invalid inputs gracefully by notifying the user of invalid choices, ensuring a user-friendly experience.

Content preview

// Attatched: Lecture 6 Extra Credit
//
// 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;
}

Document information

Uploaded on
November 6, 2025
Number of pages
5
Written in
2025/2026
Type
Other
Person
Unknown
$15.99

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Sold
0
Followers
0
Items
68
Last sold
-



Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions