Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 68603f8

Browse files
basic operations & implementations with example of inbuilt stack from java.utils.
1 parent 5fe0a94 commit 68603f8

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.udemy.dsapart1.stacks;
2+
import java.util.Scanner;
3+
import java.util.Stack;
4+
5+
public class SimpleStackOperations {
6+
7+
public static Stack<Double> firstStack;
8+
9+
public static void main(String[] args) {
10+
Scanner scInpObj = new Scanner(System.in);
11+
System.out.print("\nEnter the size of Stack : ");
12+
int sizeOfStack = scInpObj.nextInt();
13+
firstStack = new Stack<>();
14+
//firstStack.setSize(sizeOfStack);
15+
System.out.println("\nSize of stack has been set.");
16+
System.out.println("\nEnter the stack elements : ");
17+
pushElementsIntoStack(sizeOfStack);
18+
System.out.println("\nInput Stack : " + firstStack);
19+
System.out.println("\nInitial size of stack - "+firstStack.size());
20+
System.out.println("\nInitial capacity of stack - "+firstStack.capacity());
21+
22+
System.out.println("\nIs the stack is Empty : "+firstStack.isEmpty());
23+
System.out.println("***************** Perform Some Pop() & peek() operation LIFO (principle) **********************");
24+
System.out.println("\npeek the item : "+firstStack.peek());
25+
System.out.println("\nAfter peek Current Stack status : "+firstStack);
26+
System.out.println("\npop the item : "+firstStack.pop());
27+
System.out.println("\npop the item : "+firstStack.pop());
28+
System.out.println("\nAfter pop's Current Stack status : "+firstStack);
29+
System.out.println("\npop the item : "+firstStack.pop());
30+
System.out.println("\nCurrent Stack status : "+firstStack);
31+
System.out.println("\npeek the item : "+firstStack.peek());
32+
System.out.println("\nModifyed size of stack - "+firstStack.size());
33+
System.out.println("\nModifyed capacity of stack - "+firstStack.capacity());
34+
35+
System.out.println("***************** Perform insert operation **********************");
36+
firstStack.insertElementAt(112.0, 4);
37+
System.out.println("\nInserting 112.0 at 4th position.");
38+
firstStack.insertElementAt(150.0, 3);
39+
System.out.println("\nInserting 150.0 at 3rd position.");
40+
System.out.println("\nCurrent new Stack status : "+firstStack);
41+
firstStack.insertElementAt(166.0, 5);
42+
System.out.println("\nInserting 166.0 at 5th position.");
43+
System.out.println("\nCurrent new Stack status : "+firstStack);
44+
45+
System.out.println("***************** Perform remove operation **********************");
46+
System.out.println("\nRemove by index operation status : "+firstStack.remove(3));
47+
System.out.println("\nCurrent new Stack status : "+firstStack);
48+
System.out.println("\nRemove by value operation status : "+firstStack.remove(150.0));
49+
System.out.println("\n2nd Remove by value operation status : "+firstStack.remove(6.0));
50+
System.out.println("\nCurrent new final Stack status : "+firstStack);
51+
System.out.println("\nFinal size of stack - "+firstStack.size());
52+
System.out.println("\nFinal capacity of stack - "+firstStack.capacity());
53+
}
54+
55+
private static void pushElementsIntoStack(int sizeOfStack) {
56+
Scanner scInp1Obj = new Scanner(System.in);
57+
for (int i = 0; i < sizeOfStack; i++) {
58+
System.out.print("\npush the element No " + (i + 1) + " : ");
59+
firstStack.push(scInp1Obj.nextDouble());
60+
}
61+
}
62+
63+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /