File tree Expand file tree Collapse file tree 1 file changed +13
-2
lines changed Expand file tree Collapse file tree 1 file changed +13
-2
lines changed Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ public static void main(String[] args) {
15
15
stack .push (2 );
16
16
stack .push (3 );
17
17
stack .push (4 );
18
+ stack .push (5 );
18
19
19
20
stack .printStack ();
20
21
@@ -23,6 +24,8 @@ public static void main(String[] args) {
23
24
stack .pop ();
24
25
stack .pop ();
25
26
27
+ System .out .println ("Top element of stack currently is: " + stack .peek ());
28
+
26
29
}
27
30
28
31
}
@@ -75,12 +78,20 @@ public void pop() {
75
78
System .out .println ("Popped element is: " + temp .data );
76
79
}
77
80
81
+ public int peek () {
82
+ if (getSize () == 0 ) {
83
+ return -1 ;
84
+ }
85
+
86
+ return head .data ;
87
+ }
88
+
78
89
public void printStack () {
79
90
80
91
Node temp = head ;
81
92
System .out .println ("Stack is printed as below: " );
82
93
while (temp != null ) {
83
- System .out .print (temp .data + " " );
94
+ System .out .println (temp .data + " " );
84
95
temp = temp .next ;
85
96
}
86
97
System .out .println ();
@@ -94,5 +105,5 @@ public boolean isEmpty() {
94
105
public int getSize () {
95
106
return size ;
96
107
}
97
-
108
+
98
109
}
You can’t perform that action at this time.
0 commit comments