0% found this document useful (0 votes)
6 views3 pages

Dsa Mod

A stack is a linear data structure that follows the LIFO (Last In First Out) principle. Elements can only be added or removed from one end, called the top. Common stack operations include push to add an element, pop to remove an element, peek to access the top element without removing it, and empty to check if the stack is empty. The algorithms for push and pop involve checking if the stack is full/empty, accessing the top element, and incrementing/decrementing the top pointer.

Uploaded by

layson christian
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)
6 views3 pages

Dsa Mod

A stack is a linear data structure that follows the LIFO (Last In First Out) principle. Elements can only be added or removed from one end, called the top. Common stack operations include push to add an element, pop to remove an element, peek to access the top element without removing it, and empty to check if the stack is empty. The algorithms for push and pop involve checking if the stack is full/empty, accessing the top element, and incrementing/decrementing the top pointer.

Uploaded by

layson christian
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/ 3

Stack in java

Introduction to Stacks
-A stack is also known as a Last-In-First-Out (LIFO) list.
-A stack is a linear structure in which items are added or removed only at one end the
top.
-The depth of stack is the number of elements it contains.
-An empty stack has depth zero.

STACK ADT. (Abstract Data Type).


-A list with the restriction that insertion and deletion can be performed only from one
end, called the top.

Operations
• Push (x) – Insert or push some item “x” onto the stack.
• Pop( ) – Removing the most recent item or element from the stack.
• Top( ) – Returns the item or element at the top of the stack.
• IsEmpty( ) – It will return true if the stack is empty, false otherwise.
• Peek( ) – Get the top data of the stack without removing it.
• isFull( ) – Check if the stack is full.

Algorithm of push( )
The process of putting a new data element onto stack is known as a Push Operation.
Push operation involves a series of steps −
Step 1 − Checks if the stack is full.
Step 2 − If the stack is full, produces an error and exit.
Step 3 − If the stack is not full, increments top to point next empty space.
Step 4 − Adds data element to the stack location, where top is pointing.
Step 5 − Returns success.

Algorithm of push( ) cont.

Algorithm of pop( )
Accessing the content while removing it from the stack, is known as a Pop Operation. In
an array implementation of pop() operation, the data element is not actually removed,
instead top is decremented to a lower position in the stack to point to the next value.
But in linked-list implementation, pop() actually removes data element and deallocates
memory space.
Step 1 − Checks if the stack is empty.
Step 2 − If the stack is empty, produces an error and exit.
Step 3 − If the stack is not empty, accesses the data element at which top is pointing.
Step 4 − Decreases the value of top by 1.
Step 5 − Returns success.

Algorithm of pop( )

Accessing the content while removing it from the stack, is known as a Pop Operation. In
an array implementation of pop() operation, the data element is not actually removed,
instead top is decremented to a lower position in the stack to point to the next value.
But in linked-list implementation, pop() actually removes data element and deallocates
memory space.
Step 1 − Checks if the stack is empty.
Step 2 − If the stack is empty, produces an error and exit.
Step 3 − If the stack is not empty, accesses the data element at which top is pointing.
Step 4 − Decreases the value of top by 1.
Step 5 − Returns success.

----------------------------------------------------------------------

import java.util.Stack;
public class StackEmptyMethodExample
{
public static void main(String[] args)
{
//creating an instance of Stack class
Stack<Integer> stk= new Stack<>();
// checking stack is empty or not
boolean result = stk.empty();
System.out.println("Is the stack empty? " + result);
// pushing elements into stack
stk.push(78);
stk.push(113);
stk.push(90);
stk.push(120);
//prints elements of the stack
System.out.println("Elements in Stack: " + stk);
result = stk.empty();
System.out.println("Is the stack empty? " + result);
}
}

Ouput:

Is the stack empty? true


Elements in Stack: [78, 113, 90, 120]
Is the stack empty? False
import java.util.Stack;
----------------------------------------------------------------------

public class StackPeekMethodExample


{
public static void main(String[] args)
{
Stack<String> stk= new Stack<>();
// pushing elements into Stack
stk.push("Apple");
stk.push("Grapes");
stk.push("Mango");
stk.push("Orange");
System.out.println("Stack: " + stk);
// Access element from the top of the stack
String fruits = stk.peek();
//prints stack
System.out.println("Element at top: " + fruits);
}
}

Output:

Stack: [Apple, Grapes, Mango, Orange]


Element at the top of the stack: Orange
----------------------------------------------------------------------
import java.util.Stack;
public class Stk {

public static void main(String[] args) {


Stack<Integer> stk = new Stack<>();

// Use add() method to add elements


stk.push(15);
stk.push(25);
stk.push(35);
stk.push(45);
stk.push(55);

// Displaying the Stack


System.out.println("Initial Stack: " + stk);

// Removing elements using pop() method


System.out.println("Popped element: " + stk.pop());
System.out.println("Popped element: " + stk.pop());

// Displaying the Stack after pop operation


System.out.println("Stack after pop operation " + stk);
}
}
Output:

Initial Stack: [15, 25, 35, 45, 55]


Popped element: 55
Popped element: 45
Stack after pop operation [15, 25, 35]

You might also like