Java Intermediate

0% completed

Previous
Next
Basic Overview of Stack Class

The Stack class in Java represents a last-in, first-out (LIFO) data structure. It is part of the Java Collections Framework and extends the Vector class, which means it inherits all the behaviors of a dynamic array while adding stack-specific methods.

Image

A stack operates on the principle that the most recently added element is the first one to be removed. This behavior is particularly useful in scenarios such as undo mechanisms, parsing expressions, and backtracking algorithms.

Key Characteristics of the Stack Class

  • LIFO Principle:
    Elements are pushed onto the top of the stack and popped from the top. The last element added is the first one removed.

  • Inherits from Vector:
    Since Stack extends Vector, it supports all methods of the Vector class. However, it also provides additional methods specific to stack operations.

  • Common Stack Operations:

    • push(E item): Adds an item onto the top of the stack.
    • pop(): Removes and returns the top item of the stack.
    • peek(): Returns the top item without removing it.
    • empty(): Checks if the stack is empty.
    • search(Object o): Returns the 1-based position from the top of the stack where the object is located (or -1 if not found).

Syntax for Using the Stack Class

import java.util.Stack; Stack<Type> stack = new Stack<Type>();
  • Explanation:
    This statement creates a new Stack that holds objects of the specified Type. Since Stack implements the List interface (through Vector), it supports both generic and stack-specific methods.

Examples

Example 1: Basic Stack Operations (push, pop, peek, empty)

In this example, we create a Stack of strings and demonstrate basic operations: pushing elements onto the stack, peeking at the top element, popping elements off the stack, and checking if the stack is empty.

Java
Java

. . . .

Example Explanation:

  • Push Operations:
    • Elements "First", "Second", and "Third" are pushed onto the stack.
  • Peek Operation:
    • peek() retrieves the top element ("Third") without removing it.
  • Pop Operations:
    • pop() removes and returns the top element. The first two pops remove "Third" and "Second".
  • Empty Check:
    • The empty() method is used to check if the stack has any elements remaining.
  • Outcome:
    • The final pop removes the last element ("First"), and the stack becomes empty.

Example 2: Using the search() Method in Stack

This example demonstrates the use of the search() method. The search() method returns the 1-based position of an element from the top of the stack, or -1 if the element is not found.

Java
Java

. . . .

Example Explanation:

  • Push Operations:
    • The integers 10, 20, 30, and 40 are pushed onto the stack.
  • Search Operation:
    • The search(20) method returns the 3rd position from the top (1-based), indicating that the element 20 is found.
  • Element Not Found:
    • search(50) returns -1 because 50 is not in the stack.
  • Outcome:
    • The example demonstrates how the search() method can be used to locate elements within a stack.

Understanding the Stack class is fundamental for implementing LIFO-based algorithms and managing tasks that require reverse-order processing.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next