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

Java Stack Operations Example

The document contains Java code demonstrating the use of Stack data structures, including methods for pushing, popping, peeking, and searching elements. It showcases various examples of stack operations with both Integer and String types. The main functionality includes adding elements to the stack, removing them, and displaying the current state of the stack.

Uploaded by

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

Java Stack Operations Example

The document contains Java code demonstrating the use of Stack data structures, including methods for pushing, popping, peeking, and searching elements. It showcases various examples of stack operations with both Integer and String types. The main functionality includes adding elements to the stack, removing them, and displaying the current state of the stack.

Uploaded by

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

import [Link].

*;
import [Link].*;

class Test
{
// Pushing element on the top of the stack
static void stack_push(Stack<Integer> stack)
{
for(int i = 0; i < 5; i++)
{
[Link](i);
}
}

// Popping element from the top of the stack


static void stack_pop(Stack<Integer> stack)
{
[Link]("Pop Operation:");

for(int i = 0; i < 5; i++)


{
Integer y = (Integer) [Link]();
[Link](y);
}
}

// Displaying element on the top of the stack


static void stack_peek(Stack<Integer> stack)
{
Integer element = (Integer) [Link]();
[Link]("Element on stack top: " + element);
}

// Searching element in the stack


static void stack_search(Stack<Integer> stack, int element)
{
Integer pos = (Integer) [Link](element);

if(pos == -1)
[Link]("Element not found");
else
[Link]("Element is found at position: " + pos);
}

public static void main (String[] args)


{
Stack<Integer> stack = new Stack<Integer>();

stack_push(stack);
stack_pop(stack);
stack_push(stack);
stack_peek(stack);
stack_search(stack, 2);
stack_search(stack, 6);
}
}
import [Link].*;
import [Link].*;

class StackDemo {

// Main Method
public static void main(String[] args)
{

// Default initialization of Stack


Stack stack1 = new Stack();

// Initialization of Stack
// using Generics
Stack<String> stack2 = new Stack<String>();

// pushing the elements


[Link](4);
[Link]("All");
[Link]("Geeks");

[Link]("Geeks");
[Link]("For");
[Link]("Geeks");

// Printing the Stack Elements


[Link](stack1);
[Link](stack2);
}
}

import [Link].*;
import [Link].*;

public class StackDemo {


public static void main(String args[])
{
// Creating an empty Stack
Stack<Integer> stack = new Stack<Integer>();

// Use add() method to add elements


[Link](10);
[Link](15);
[Link](30);
[Link](20);
[Link](5);

// Displaying the Stack


[Link]("Initial Stack: " + stack);

// Removing elements using pop() method


[Link]("Popped element: "
+ [Link]());
[Link]("Popped element: "
+ [Link]());

// Displaying the Stack after pop operation


[Link]("Stack after pop operation "
+ stack);
}
}

You might also like