-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathStack.java
68 lines (56 loc) · 1005 Bytes
/
Stack.java
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
class Node{
int val;
Node next;
Node(int data){
this.val = data;
this.next = null;
}
}
class LinkedList{
Node head;
LinkedList(int data){
this.head = new Node(data);
}
}
// Implementation of stack using LinkedList
class Stack{
LinkedList stack;
Stack(int data){
stack = new LinkedList(data);
}
void push(int value){
Node newNode = new Node(value);
newNode.next = stack.head;
stack.head = newNode;
}
int top(){
return stack.head.val;
}
void pop(){
Node delNode = stack.head;
stack.head = delNode.next;
}
}
class Solution
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Stack stack = new Stack(5);
System.out.println(stack.top());
stack.push(6);
stack.push(8);
stack.push(9);
System.out.println(stack.top());
stack.pop();
System.out.println(stack.top());
stack.push(10);
stack.push(11);
stack.push(12);
System.out.println(stack.top());
stack.pop();
stack.pop();
stack.pop();
System.out.println(stack.top());
}
}