100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Other

Lecture 6 Extra Credit main.cpp - Class Exercise 2

Rating
-
Sold
-
Pages
5
Uploaded on
06-11-2025
Written in
2025/2026

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.

Show more Read less









Whoops! We can’t load your doc right now. Try again or contact support.

Document information

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

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;
}
$15.49
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
tinahuynh

Get to know the seller

Seller avatar
tinahuynh California State University, Long Beach
View profile
Follow You need to be logged in order to follow users or courses
Sold
New on Stuvia
Member since
1 month
Number of followers
0
Documents
68
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

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

Frequently asked questions