You are on page 1of 3

Republic of the Philippines

CAMARINES SUR POLYTECHNIC COLLEGES


Nabua, Camarines Sur

CCIT 104:
MIDTERM
Filename:Group 2_BSIT2D
PROJECT
1.[Topic 2: Stack]
 Key concept
 Stack is a linear data structure that follows a particular order in which the operations are
performed.
 Stack follows the principle of Last In First Our (LIFO) or First In Last Out (FILO)
 In Stack you can do putting a new element on top and remove the top element
 LIFO is also called push and removing an item is called pop
 The five Basic Operations of stack are:
 Push
 Pop
 IsEmpty
 IsFull
 Peek
 TOP used to keep track of the top element in the stack
 Three Applications of Stack Data Structure:
 To reverse a word
 In compilers
 In browsers

Learning Tasks:
Takeaways:
This C++ code demonstrates how to work with a stack using the C++ Standard Library's std::stack container.
It pushes elements onto the stack, displays the stack's current state, pops an item from the stack, and then
displays the stack's state after the pop operation
#include Directives: The code includes the necessary C++ Standard Library headers for input and output
(<iostream>) and the stack container (<stack>).Stack Initialization: It initializes a stack named myStack to
store integers. This stack will be used to push, pop, and display elements.
Pushing Elements: The code pushes three integers (1, 2, and 3) onto the stack using the push method. The
stack now contains these elements, with 3 as the top element.Displaying the Current Stack: To visualize the
current state of the stack, a temporary stack named tempStack is created. A while loop iterates through
tempStack, printing each element's value while preserving the original stack. The output shows the initial
stack contents as "Current Stack."Checking If the Stack Is Empty: The code checks if the myStack is not
empty (i.e., there are elements to pop).Popping an Item: If the stack is not empty, it proceeds to pop an item.
It retrieves the top element using top(), stores it in poppedItem, and then pops the top element from the stack
using pop(). The popped item is printed as "Popped Item."
Displaying the Stack After Popping: After popping an item, a while loop iterates through the myStack and
prints each element's value. The output shows the state of the stack after the pop operation.Handling an
Empty Stack: If the stack is initially empty, it prints a message indicating that the stack is empty and there's
nothing to pop.
Program Completion: The program returns 0, indicating successful execution.

You might also like