0% found this document useful (0 votes)
2 views6 pages

Cañete Discussion 2

The document provides an overview of stacks as a linear data structure that follows the LIFO (Last In First Out) principle, along with a comparison to FIFO (First In First Out). It includes examples of real-world applications and code snippets demonstrating LIFO and FIFO implementations. The document is part of a graduate program activity focused on advanced data structures and algorithms.

Uploaded by

jcanete
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Cañete Discussion 2

The document provides an overview of stacks as a linear data structure that follows the LIFO (Last In First Out) principle, along with a comparison to FIFO (First In First Out). It includes examples of real-world applications and code snippets demonstrating LIFO and FIFO implementations. The document is part of a graduate program activity focused on advanced data structures and algorithms.

Uploaded by

jcanete
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Technological Institute of the Philippines

938 Aurora Blvd. Cubao, Quezon City

GRADUATE PROGRAM
MCSCC 123-MSCS12G1 - Advanced Data Structures and Algorithms

Prelim Period

Name: Jess Keith B. Cañete Date: March 5, 2025


Program/Section: MSCS/MSCS12G1 Instructor: Asst. Prof. Karren V. de Lara
Activity Title: Discussion 2 || Stack Task

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.

2. What is the difference between LIFO and FIFO transactions?

 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.

3. State sample of LIFO and FIFO.


 A real-time problem that solves using LIFO and FIFO (2 samples each).

LIFO FIFO
1. Stack of Plates Queue at a ticket counter
2. One way Parking Garage Supermarket Checkout Line

 Codes that show the LIFO and FIFO. (2 samples each)

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;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy