Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Stack

stack data structure notes
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)
20 views6 pages

Stack

stack data structure notes
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

Stack

• Stack is a LIFO (Last In First Out) data structure i.e., the element that was
inserted last will be removed first.

• A stack is a linear data structure that provides two O(1) time


operations: adding an element to the top, and removing an element from
the top. It is only possible to access the top element of a stack.

Stack-Data-Structure

Applications of Stack in Programming

• We use text/image editor for editing the text/image where we have


options to redo/undo the editing done. When we click on the redo /undo
icon, the most recent editing is redone/undone. In this scenario, the
system uses a stack to keep track of changes made.

• While browsing the web, we move from one web page to another by
accessing links between them. In order to go back to the last visited web
page, we may use the back button on the browser. We may go to a
previously visited web page by using the BACK button of the browser. In
this case, the history of browsed pages is maintained as stack.

• Recursion

• Expression Evaluation

Basic Operations of Stack

• Push: Adds an element to the top of the stack.


• Pop: Removes the top element from the stack.
• Peek: Returns the top element without removing it.
• IsEmpty: Checks if the stack is empty.
• IsFull: Checks if the stack is full (in case of fixed-size arrays).
Implementation of Stack using array

Declaration
data_type name[size];

push() function

void push(int val) {


if(top>=n-1)
cout<<"Stack Overflow"<<endl;
else {
top++;
stack[top]=val;
}
}

pop() function

void pop() {
if(top<=-1)
cout<<"Stack Underflow"<<endl;
else {
cout<<"The popped element is "<< stack[top] <<endl;
top--;
}
}

peek() function

// peek at the top element of the stack


int peek() { () {
if(top == -1){
cout<<"Stack is empty"<<endl;
return -1
}
return arr[top]
}
[top];
}

C++ STL Stack


Declaring a Stack

#include<iostream>
#include <stack>
using namespace std;
int main(){
stack<dataType> myStack;
}

Adding Elements in the Stack

#include <iostream>
#include <stack>
using namespace std;

int main() {

// create a stack of strings


stack<string> colors;

// push elements into the stack


colors.push("Red");
colors.push("Orange");

return 0;
}
Removing Elements From the Stack

// removes last element from the stack


colors.pop();

Accessing Elements From the Stack

// get top element


string top = colors.top();

Getting Size of the Stack

// get the size of the stack


int size = colors.size();

Check if the Stack Is Empty

We use the empty() method to check if the stack is empty. This method
returns:
❖ 1 (true) - if the stack is empty
❖ 0 (false) - if the stack is not empty
// check if the stack is empty
if (colors.empty()) {
cout << "Yes" << endl;

}
else {
cout << "No" << endl;
}
Practice Problems based on Stack

Sr. No. Problems Difficulty


1 Reverse individual words Easy
2 Crawler log folder Easy
3 Make the string great Easy
4 Simplify path Medium
5 Longest absolute file path Medium
6 Asteroid Collision Medium
7 Car fleet Medium
8 Using a robot to print the lexicographically smallest string Medium
9 Robot collisions Hard
10 Trapping rain water Hard

You might also like