-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
38 lines (32 loc) · 700 Bytes
/
Stack.java
File metadata and controls
38 lines (32 loc) · 700 Bytes
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
class Stack<T>{
class Node<T>{
T data;
Node next;
public Node(T data){
this.data = data;
}
}
Node head;
public boolean isEmpty(){
return head == null;
}
public void push(T data){
Node node = new Node(data);
node.next = head;
head = node;
}
public T pop(){
Node temp;
if(isEmpty())
return null;
temp = head;
head = head.next;
return (T) temp.data;
}
public T top() throws Exception{
if(isEmpty()){
throw new java.lang.RuntimeException("Queue is Empty");
}
return (T) head.data;
}
}