100% tevredenheidsgarantie Direct beschikbaar na je betaling Lees online óf als PDF Geen vaste maandelijkse kosten 4.2 TrustPilot
logo-home
Overig

Lecture 6 Extra Credit main.cpp - Class Exercise 2

Beoordeling
-
Verkocht
-
Pagina's
5
Geüpload op
06-11-2025
Geschreven 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.

Meer zien Lees minder
Instelling
Vak









Oeps! We kunnen je document nu niet laden. Probeer het nog eens of neem contact op met support.

Geschreven voor

Instelling
Vak

Documentinformatie

Geüpload op
6 november 2025
Aantal pagina's
5
Geschreven in
2025/2026
Type
Overig
Persoon
Onbekend

Onderwerpen

Voorbeeld van de inhoud

// 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;
}
€13,62
Krijg toegang tot het volledige document:

100% tevredenheidsgarantie
Direct beschikbaar na je betaling
Lees online óf als PDF
Geen vaste maandelijkse kosten

Maak kennis met de verkoper
Seller avatar
tinahuynh

Maak kennis met de verkoper

Seller avatar
tinahuynh California State University, Long Beach
Volgen Je moet ingelogd zijn om studenten of vakken te kunnen volgen
Verkocht
Nieuw op Stuvia
Lid sinds
1 maand
Aantal volgers
0
Documenten
68
Laatst verkocht
-

0,0

0 beoordelingen

5
0
4
0
3
0
2
0
1
0

Recent door jou bekeken

Waarom studenten kiezen voor Stuvia

Gemaakt door medestudenten, geverifieerd door reviews

Kwaliteit die je kunt vertrouwen: geschreven door studenten die slaagden en beoordeeld door anderen die dit document gebruikten.

Niet tevreden? Kies een ander document

Geen zorgen! Je kunt voor hetzelfde geld direct een ander document kiezen dat beter past bij wat je zoekt.

Betaal zoals je wilt, start meteen met leren

Geen abonnement, geen verplichtingen. Betaal zoals je gewend bent via iDeal of creditcard en download je PDF-document meteen.

Student with book image

“Gekocht, gedownload en geslaagd. Zo makkelijk kan het dus zijn.”

Alisha Student

Veelgestelde vragen