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

Stack

The document contains Java code examples demonstrating the use of a Stack and a Deque. It shows how to push, pop, and peek elements using both built-in Stack and ArrayDeque classes, as well as a custom Stack class implementation. The custom Stack class includes methods for pushing, popping, and displaying elements, along with overflow and underflow checks.
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 views3 pages

Stack

The document contains Java code examples demonstrating the use of a Stack and a Deque. It shows how to push, pop, and peek elements using both built-in Stack and ArrayDeque classes, as well as a custom Stack class implementation. The custom Stack class includes methods for pushing, popping, and displaying elements, along with overflow and underflow checks.
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

​import [Link].

Stack;​

​public class Main {​


​public static void main(String[] args) {​
​Stack<Integer> stack = new Stack<>();​

/​/ Push​
​[Link](10);​
​[Link](20);​
​[Link](30);​

/​/ Pop​
​[Link]();​

/​/ Peek​
​[Link]([Link]());​

/​/ Print stack​


​[Link](stack);​
​}​
​}​

​ SING ARRAYDEQUEUE​
U
​import [Link];​
​import [Link];​

​public class Main {​


​public static void main(String[] args) {​
​Deque<Integer> stack = new ArrayDeque<>();​

​[Link](10);​
​ [Link](20);​
s
​[Link](30);​

​[Link]([Link]()); // 30 (LIFO)​
​}​
​}​

​USING CLASS​
​class Stack {​
​int top = -1;​
​int size = 5;​
​int arr[] = new int[size];​

​void push(int val) {​


​if (top == size - 1) {​
​[Link]("Overflow");​
​return;​
​}​
​arr[++top] = val;​
​}​

​void pop() {​
​if (top == -1) {​
​[Link]("Underflow");​
​return;​
​}​
​top--;​
​}​

​void display() {​
​for (int i = top; i >= 0; i--) {​
​[Link](arr[i] + " ");​
​}​
​}​
​}​

You might also like