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

3.0 Stack

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

3.0 Stack

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

What is a Stack?

A Stack is a linear data structure that follows the LIFO (Last-In-First-Out) principle. Stack has one end, whereas
the Queue has two ends (front and rear). It contains only one pointer top pointer pointing to the topmost
element of the stack. Whenever an element is added in the stack, it is added on the top of the stack, and the
element can be deleted only from the stack. In other words, a stack can be defined as a container in which
insertion and deletion can be done from the one end known as the top of the stack.
Working of Stack
Stack works on the LIFO pattern. As we can observe in the below figure there are five memory blocks in the
stack; therefore, the size of the stack is 5.
Suppose we want to store the elements in a stack and let's assume that stack is empty. We have taken the
stack of size 5 as shown below in which we are pushing the elements one by one until the stack becomes full.

When we perform the delete operation on the stack, there is only one way for entry and exit as the other end
is closed. It follows the LIFO pattern, which means that the value entered first will be removed last. In the
above case, the value 5 is entered first, so it will be removed only after the deletion of al l the other elements.
Standard Stack Operations
The following are some common operations implemented on the stack:
o push(): When we insert an element in a stack then the operation is known as a push. If the stack is full
then the overflow condition occurs.
o pop(): When we delete an element from the stack, the operation is known as a pop. If the stack is
empty means that no element exists in the stack, this state is known as an underflow state.
o isEmpty(): It determines whether the stack is empty or not.
o isFull(): It determines whether the stack is full or not.'
o peek(): It returns the element at the given position.
PUSH operation
The steps involved in the PUSH operation is given below: (Algorithm)
o Before inserting an element in a stack, we check whether the stack is full.
o If we try to insert the element in a stack, and the stack is full, then the overflow condition occurs.
o When we initialize a stack, we set the value of top as -1 to check that the stack is empty.
o When the new element is pushed in a stack, first, the value of the top gets incremented,
i.e., top=top+1, and the element will be placed at the new position of the top.
o The elements will be inserted until we reach the max size of the stack.

#include <iostream>
#include <stack>

int main() {
// Create an empty stack of integers
std::stack<int> myStack;

// Push elements onto the stack


myStack.push(10);
myStack.push(20);
myStack.push(30);
myStack.push(40);

// Display the elements in the stack


std::cout << "Stack elements: ";
while (!myStack.empty()) {
std::cout << myStack.top() << " "; // Access the top element
myStack.pop(); // Remove the top element
}

std::cout << std::endl;

return 0;
}
POP operation
The steps involved in the POP operation is given below:
o Before deleting the element from the stack, we check whether the stack is empty.
o If we try to delete the element from the empty stack, then the underflow condition occurs.
o If the stack is not empty, we first access the element which is pointed by the top
o Once the pop operation is performed, the top is decremented by 1, i.e., top=top-1.

Algorithm :
1. begin
2. if top = 0 then stack empty;
3. item := stack(top);
4. top = top - 1;
5. end;
#include <iostream>
#include <stack>

int main() {
// Create an empty stack of integers
std::stack<int> myStack;

// Push elements onto the stack


myStack.push(10);
myStack.push(20);
myStack.push(30);
myStack.push(40);

// Display and pop the elements from the stack


std::cout << "Stack elements: ";
while (!myStack.empty()) {
std::cout << myStack.top() << " "; // Access the top element
myStack.pop(); // Remove the top element
}

std::cout << std::endl;

return 0;
}
peek()
The peek() is an operation retrieves the topmost element within the stack, without deleting it. This operation
is used to check the status of the stack with the help of the top pointer.
Algorithm of pee()
1. START
2. return the element at the top of the stack
3. END

#include <iostream>
#include <stack>

int main() {
// Create a stack of integers
std::stack<int> myStack;

// Push elements onto the stack


myStack.push(10);
myStack.push(20);
myStack.push(30);
myStack.push(40);

// Peek at the top element without removing it


int topElement = myStack.top(); // Access the top element

// Display the top element (peeked value)


std::cout << "Top element of the stack: " << topElement << std::endl;
return 0;
}
isFull()
isFull() operation checks whether the stack is full. This operation is used to check the status of the stack with
the help of top pointer.
Algorithm
1. START
2. If the size of the stack is equal to the top position of the stack, the stack is full. Return 1.
3. Otherwise, return 0.
4. END
#include <iostream>

const int MAX_SIZE = 5; // Maximum stack size

int stack[MAX_SIZE]; // Stack implemented as a dynamic array


int top = -1; // Index of the top element

bool isFull() {
return top == MAX_SIZE - 1; // Check if the stack is full
}

void push(int value) {


if (!isFull()) {
stack[++top] = value; // Increment 'top' and add the value
} else {
std::cout << "Stack is full. Cannot push " << value << std::endl;
}
}

int pop() {
if (top >= 0) {
return stack[top--]; // Return the top element and decrement 'top'
} else {
std::cout << "Stack is empty. Cannot pop." << std::endl;
return -1; // Return a sentinel value for an empty stack
}
}

int main() {
push(10);
push(20);
push(30);
push(40);
push(50);
push(60); // This will trigger the "Stack is full" message

std::cout << "Popped element: " << pop() << std::endl;


std::cout << "Popped element: " << pop() << std::endl;

return 0;
}

isEmpty()
The isEmpty() operation verifies whether the stack is empty. This operation is
used to check the status of the stack with the help of top pointer.
Algorithm
1. START
2. If the top value is -1, the stack is empty. Return 1.
3. Otherwise, return 0.
4. END

#include <iostream>
#include <stack>

bool isEmpty(const std::stack<int>& myStack) {


return myStack.empty();
}

int main() {
std::stack<int> myStack;

std::cout << "Is the stack empty? " << (isEmpty(myStack) ? "Yes" : "No") << std::endl;

myStack.push(10);
myStack.push(20);
myStack.push(30);

std::cout << "Is the stack empty? " << (isEmpty(myStack) ? "Yes" : "No") << std::endl;

return 0;
}

You might also like