100% de satisfacción garantizada Inmediatamente disponible después del pago Tanto en línea como en PDF No estas atado a nada 4.2 TrustPilot
logo-home
Otro

Lecture 6 Extra Credit main.cpp - Class Exercise 2

Puntuación
-
Vendido
-
Páginas
5
Subido en
06-11-2025
Escrito en
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.

Mostrar más Leer menos
Institución
Grado









Ups! No podemos cargar tu documento ahora. Inténtalo de nuevo o contacta con soporte.

Escuela, estudio y materia

Institución
Grado

Información del documento

Subido en
6 de noviembre de 2025
Número de páginas
5
Escrito en
2025/2026
Tipo
Otro
Personaje
Desconocido

Temas

Vista previa del contenido

// 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
Accede al documento completo:

100% de satisfacción garantizada
Inmediatamente disponible después del pago
Tanto en línea como en PDF
No estas atado a nada

Conoce al vendedor
Seller avatar
tinahuynh

Conoce al vendedor

Seller avatar
tinahuynh California State University, Long Beach
Seguir Necesitas iniciar sesión para seguir a otros usuarios o asignaturas
Vendido
Nuevo en Stuvia
Miembro desde
1 mes
Número de seguidores
0
Documentos
68
Última venta
-

0.0

0 reseñas

5
0
4
0
3
0
2
0
1
0

Recientemente visto por ti

Por qué los estudiantes eligen Stuvia

Creado por compañeros estudiantes, verificado por reseñas

Calidad en la que puedes confiar: escrito por estudiantes que aprobaron y evaluado por otros que han usado estos resúmenes.

¿No estás satisfecho? Elige otro documento

¡No te preocupes! Puedes elegir directamente otro documento que se ajuste mejor a lo que buscas.

Paga como quieras, empieza a estudiar al instante

Sin suscripción, sin compromisos. Paga como estés acostumbrado con tarjeta de crédito y descarga tu documento PDF inmediatamente.

Student with book image

“Comprado, descargado y aprobado. Así de fácil puede ser.”

Alisha Student

Preguntas frecuentes