Cañete Discussion 2
Cañete Discussion 2
GRADUATE PROGRAM
MCSCC 123-MSCS12G1 - Advanced Data Structures and Algorithms
Prelim Period
1. What is stack?
A stack is a linear data structure where elements are stored in the LIFO (Last In First Out)
principle where the last element inserted would be the first element to be deleted. A stack is an
Abstract Data Type (ADT), that is popularly used in most programming languages. It is named
stack because it has the similar operations as the real-world stacks, for example − a pack of
cards or a pile of plates, etc.
The last-in, first-out (LIFO) method assumes that the last unit to arrive in inventory is
sold first.
The first-in, first-out (FIFO) method assumes that the oldest unit of inventory is sold first.
LIFO FIFO
1. Stack of Plates Queue at a ticket counter
2. One way Parking Garage Supermarket Checkout Line
LIFO FIFO
1. //Stack of Plates – LIFO Example // Queue at a ticket counter – FIFO
Example
#include <iostream>
#include <stack> #include <iostream>
#include <queue>
using namespace std;
using namespace std;
class PlateStack {
private: class TicketCounter {
stack<string> plates; private:
queue<string> customerQueue;
public:
void addPlate(string plate) { public:
plates.push(plate); void joinQueue(string name) {
cout << "Plate \"" << plate << "\" customerQueue.push(name);
added to the stack.\n"; cout << "Customer \"" << name
} << "\" has joined the queue.\n";
}
void removePlate() {
if (plates.empty()) { void serveCustomer() {
cout << "No plates left in the if (customerQueue.empty()) {
stack!\n"; cout << "No customers in the
} else { queue!\n";
cout << "Removing top } else {
plate: \"" << plates.top() << "\"\n"; cout << "Serving customer: \""
plates.pop(); << customerQueue.front() << "\"\n";
} customerQueue.pop();
} }
}
void topPlate() {
if (plates.empty()) { void nextCustomer() {
cout << "No plates in the if (customerQueue.empty()) {
stack!\n"; cout << "No customers in the
} else { queue!\n";
cout << "Top plate: \"" << } else {
plates.top() << "\"\n"; cout << "Next customer in line:
} \"" << customerQueue.front() << "\"\n";
} }
}
bool isEmpty() {
return plates.empty(); bool isEmpty() {
} return customerQueue.empty();
}; }
};
int main() {
PlateStack stack; int main() {
int choice; TicketCounter counter;
string plateName; int choice;
string customerName;
do {
cout << "\nStack of Plates Menu:\ do {
n"; cout << "\nTicket Counter Menu:\
cout << "1. Add Plate\n2. n";
Remove Plate\n3. View Top Plate\n4. cout << "1. Join Queue\n2. Serve
Exit\n"; Customer\n3. View Next Customer\
cout << "Enter your choice: "; n4. Exit\n";
cin >> choice; cout << "Enter your choice: ";
cin.ignore(); cin >> choice;
cin.ignore();
switch (choice) {
case 1: switch (choice) {
cout << "Enter plate name: case 1:
"; cout << "Enter customer
getline(cin, plateName); name: ";
stack.addPlate(plateName); getline(cin, customerName);
break; counter.joinQueue(custome
case 2: rName);
stack.removePlate(); break;
break; case 2:
case 3: counter.serveCustomer();
stack.topPlate(); break;
break; case 3:
case 4: counter.nextCustomer();
cout << "Exiting program...\ break;
n"; case 4:
break; cout << "Exiting the ticket
default: counter system...\n";
cout << "Invalid choice, break;
please try again!\n"; default:
} cout << "Invalid choice,
} while (choice != 4); please try again!\n";
}
return 0; } while (choice != 4);
}
return 0;
//Output }
//Output
2. //One way Parking Garage – LIFO //Supermarket Checkout line – FIFO
Example
#include <iostream>
#include <stack> #include <iostream>
#include <queue>
using namespace std;
using namespace std;
class ParkingGarage {
private: class SupermarketCheckout {
stack<string> cars; private:
queue<string> checkoutQueue;
public:
void parkCar(string car) { public:
cars.push(car); void joinQueue(string customer) {
cout << "Car \"" << car << "\" checkoutQueue.push(customer);
parked in the garage.\n"; cout << "Customer \"" <<
} customer << "\" has joined the
checkout line.\n";
void removeCar() { }
if (cars.empty()) {
cout << "Garage is empty! No void serveCustomer() {
cars to remove.\n"; if (checkoutQueue.empty()) {
} else { cout << "No customers in the
cout << "Car \"" << cars.top() checkout line!\n";
<< "\" is leaving the garage.\n"; } else {
cars.pop(); cout << "Serving customer: \""
} << checkoutQueue.front() << "\"\n";
} checkoutQueue.pop();
}
void topCar() { }
if (cars.empty()) {
cout << "Garage is empty! No void nextCustomer() {
cars are parked.\n"; if (checkoutQueue.empty()) {
} else { cout << "No customers in the
cout << "Last parked car: \"" checkout line!\n";
<< cars.top() << "\"\n"; } else {
} cout << "Next customer in line:
} \"" << checkoutQueue.front() << "\"\n";
}
bool isEmpty() { }
return cars.empty();
} bool isEmpty() {
}; return checkoutQueue.empty();
}
int main() { };
ParkingGarage garage;
int choice; int main() {
string carName; SupermarketCheckout checkout;
int choice;
do { string customerName;
cout << "\nParking Garage
Menu:\n"; do {
cout << "1. Park Car\n2. Remove cout << "\nSupermarket
Car\n3. View Last Parked Car\n4. Checkout Menu:\n";
Exit\n"; cout << "1. Join Checkout Line\
cout << "Enter your choice: "; n2. Serve Customer\n3. View Next
cin >> choice; Customer\n4. Exit\n";
cin.ignore(); // Ignore newline cout << "Enter your choice: ";
character cin >> choice;
cin.ignore();
switch (choice) {
case 1: switch (choice) {
cout << "Enter car case 1:
name/number: "; cout << "Enter customer
getline(cin, carName); name: ";
garage.parkCar(carName); getline(cin, customerName);
break; checkout.joinQueue(custom
case 2: erName);
garage.removeCar(); break;
break; case 2:
case 3: checkout.serveCustomer();
garage.topCar(); break;
break; case 3:
case 4: checkout.nextCustomer();
cout << "Exiting the parking break;
garage system...\n"; case 4:
break; cout << "Exiting the
default: checkout system...\n";
cout << "Invalid choice, break;
please try again!\n"; default:
} cout << "Invalid choice,
} while (choice != 4); please try again!\n";
}
return 0; } while (choice != 4);
}
return 0;
}