-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
91 lines (91 loc) · 2.69 KB
/
Stack.java
File metadata and controls
91 lines (91 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Java Code to illustrate push() Method
import java.util.*;
public class StackDemo {
public static void main(String args[])
{
Stack& lt;
String& gt;
STACK = new Stack& lt;
String& gt;
();
STACK.push(" Welcome & quot;);
STACK.push(" To & quot;);
STACK.push(" Geeks & quot;);
STACK.push(" For & quot;);
STACK.push(" Geeks & quot;);
System.out.println(" Initial Stack: "+ STACK);
STACK.push(" Hello & quot;);
STACK.push(" World & quot;);
System.out.println(" Final Stack: "+ STACK);
}
}
import java.util.*;
public class StackDemo {
public static void main(String args[])
{
Stack<String> STACK = new Stack<String>();
STACK.push("Welcome");
STACK.push("To");
STACK.push("Geeks");
STACK.push("For");
STACK.push("Geeks");
System.out.println("Initial Stack: " + STACK);
System.out.println("Popped element: " + STACK.pop());
System.out.println("Popped element: " +STACK.pop());
System.out.println("Stack after pop operation "+ STACK);
}
}
// Java code to illustrate peek() function
import java.util.*;
public class StackDemo {
public static void main(String args[])
{
Stack<String> STACK = new Stack<String>();
STACK.push("Welcome");
STACK.push("To");
STACK.push("Geeks");
STACK.push("For");
STACK.push("Geeks");
System.out.println("Initial Stack: " + STACK);
System.out.println("The element at the top of the stack is: " + STACK.peek());
System.out.println("Final Stack: " + STACK);
}
}
// Java code to demonstrate empty() method
import java.util.*;
public class Stack_Demo {
public static void main(String[] args)
{
Stack<String> STACK = new Stack<String>();
STACK.push("Geeks");
STACK.push("4");
STACK.push("Geeks");
STACK.push("Welcomes");
STACK.push("You");
System.out.println("The stack is: " + STACK);
System.out.println("Is the stack empty? " +STACK.empty());
STACK.pop();
STACK.pop();
STACK.pop();
STACK.pop();
STACK.pop();
System.out.println("Is the stack empty? " +STACK.empty());
}
}
// Java code to demonstrate search() method
import java.util.*;
public class Stack_Demo {
public static void main(String[] args)
{
Stack<String> STACK = new Stack<String>();
STACK.push("Geeks");
STACK.push("4");
STACK.push("Geeks");
STACK.push("Welcomes");
STACK.push("You");
System.out.println("The stack is: " + STACK);
System.out.println("Does the stack contains '4'? "+ STACK.search("4"));
System.out.println("Does the stack contains 'Hello'? "+ STACK.search("Hello"));
System.out.println("Does the stack contains 'Geeks'? "+ STACK.search("Geeks"));
}
}