1
1
/**
2
- *
3
2
* @author Varun Upadhyay (https://github.com/varunu28)
4
- *
5
3
*/
6
4
7
5
// An implementation of a Stack using a Linked List
@@ -42,7 +40,7 @@ public Node(int data) {
42
40
43
41
/**
44
42
* A class which implements a stack using a linked list
45
- *
43
+ * <p>
46
44
* Contains all the stack methods : push, pop, printStack, isEmpty
47
45
**/
48
46
@@ -54,8 +52,7 @@ public void push(int x) {
54
52
Node n =new Node (x );
55
53
if (head ==null ) {
56
54
head =n ;
57
- }
58
- else {
55
+ }else {
59
56
Node temp =head ;
60
57
n .next =temp ;
61
58
head =n ;
@@ -73,20 +70,19 @@ public void pop() {
73
70
}
74
71
75
72
public int peek () {
76
- if (head ==null ) {
77
- return -1 ;
78
- }
79
- return head .data ;
73
+ if (head ==null ) {
74
+ return -1 ;
75
+ }
76
+ return head .data ;
80
77
}
81
78
82
79
public void printStack () {
83
80
Node temp =head ;
84
81
System .out .println ("Stack is printed as below: " );
85
82
while (temp !=null ) {
86
- if (temp .next ==null ) {
83
+ if (temp .next ==null ) {
87
84
System .out .print (temp .data );
88
- }
89
- else {
85
+ }else {
90
86
System .out .print (temp .data +" -> " );
91
87
}
92
88
temp =temp .next ;
@@ -99,12 +95,12 @@ public boolean isEmpty() {
99
95
}
100
96
101
97
public int getSize () {
102
- if (head ==null )
98
+ if (head ==null )
103
99
return 0 ;
104
100
else {
105
101
int size =1 ;
106
102
Node temp =head ;
107
- while (temp .next !=null ) {
103
+ while (temp .next !=null ) {
108
104
temp =temp .next ;
109
105
size ++;
110
106
}