0% found this document useful (0 votes)
3 views14 pages

Understanding Stack Data Structure

This document provides an overview of the Stack data structure, detailing its definition as a Last-in, First-out (LIFO) structure and various operations such as push, pop, checking if the stack is empty, retrieving the top element, determining the size, and searching for an element. It includes Java code examples for each operation. The document serves as a guide for understanding and implementing stack operations in programming.

Uploaded by

Full name
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)
3 views14 pages

Understanding Stack Data Structure

This document provides an overview of the Stack data structure, detailing its definition as a Last-in, First-out (LIFO) structure and various operations such as push, pop, checking if the stack is empty, retrieving the top element, determining the size, and searching for an element. It includes Java code examples for each operation. The document serves as a guide for understanding and implementing stack operations in programming.

Uploaded by

Full name
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

Jinan University

Faculty of Science
Dep. Of Computer Science

DATA STRUCTURE
Dr. Rami Safarjalani

2024-2025 Chapter 5: Stack


Out Line
2

 What is Stack data structure?


 Push operation
 Pop operation
 Retrieve the data of an element
 Clear the Stack
 Print all data of stack
 Search about data
 Stack Collection
What is the Stack?
3

 Stack is a dynamic linear data structure.


 Data in a stack are added and removed from
only one end of the list.
What is the Stack?
4

 We define a stack as a list of items that are


accessible only from the end of the list, which
is called the top of the stack.
 Elements are always removed from the top,
and inserted on the top also.
 A stack is known as a Last-in, First-out (LIFO)
data structure
Add Item to Stack
5

To add element to Stack, use the push() method:


import [Link];
public class Add_element {
public static void main(String[] args) {
Stack<String> cars = new Stack<>();
[Link]("Volvo");
[Link]("BMW");
[Link]("Kia");
[Link](cars);
}
}
Remove Item from Stack
6

To remove element from Stack, use the pop() method:


import [Link];
public class remove_element {
public static void main(String[] args) {
Stack<String> cars = new Stack<>();
[Link]("Volvo");
[Link]("BMW");
[Link]("Kia");
[Link]();
[Link](cars);
}
}
Check Stack is empty
7

To check if Stack is empty, use the empty() method:


import [Link];
public class check_empty {
public static void main(String[] args) {
Stack<String> cars = new Stack<>();
[Link]([Link]());
[Link]("Volvo");
[Link]("BMW");
[Link]([Link]());
}
}
Top element in stack
8

To looks at the Top element in stack, use peek() method

import [Link];
public class Top_elt {
public static void main(String[] args) {
Stack<String> cars = new Stack<>();
[Link]("Volvo");
[Link]("BMW");
[Link]("Honda");
[Link]([Link]());
}
}
Size of Stack
9

To know the size of a stack, use size() method

import [Link];
public class stack_size{
public static void main(String[] args) {
Stack<String> cars = new Stack<>();
[Link]("Volvo");
[Link]("Honda");
[Link]("BMW");
[Link]([Link]());
}
}
Search element in stack
10

To search for an element in stack, use search() method

import [Link];
public class search_elt {
public static void main(String[] args) {
Stack<String> cars = new Stack<>();
[Link]("Volvo");
[Link]("Honda");
[Link]("BMW");
[Link]([Link]("BMW"));
}
}
Push operation
11

Top Top 7
Top

1 1

6 6 6
Push operation
12
Pop operation
13

Top 7 Top 7

1 1

6 6
Pop operation
14

You might also like