-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
60 lines (54 loc) · 1.59 KB
/
Stack.java
File metadata and controls
60 lines (54 loc) · 1.59 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
//java doesn't allow generic array
//avoid casting, it shows weakness
//generic implementation
//handling multiple type of data
// maintain pointer to first node and remove/add from front.
// Stack<Integer> stack = new Stack<Integer>();
public class Stack<Item> {
private Node first=null;//inner class variables as stack ?? check
// for queues use another variable too , last
// public LinkedStackOfStrings(){
// Node n=new Node();
// System.out.println(n.item+"--");
// }
//inner class
//should not have static members
//inner class has extra overhead of 8 bytes
private class Node{
//access modifier doesn't matter
Item item;//instance variables have a default value
Node next; //object , a reference to another node
// public String toString(){
// return item ;
// }
}
public boolean isEmpty(){
return first==null;
}
public void push(Item item){
Node oldfirst=first;
// System.out.println(first);
first=new Node();//object 16 bytes overhead
first.item=item;// 8 bytes reference to string
first.next=oldfirst;// 8 bytes reference to node (so 40 bytes per stack node)
System.out.println(first.item);
System.out.println(first.next);
// System.out.println(first + "===========");
}
public Item pop(){
Item item=first.item;
first=first.next;// first node is deleted is reclaimed by the garbage collector
return item;
}
public static void main(String[] args ){
Stack l=new Stack();
// System.out.println(l.first);
// System.out.println("====");
String s="hello";
l.push(s);
String sa="hello2";
l.push(sa);
String sab="hello3";
l.push(sab);
}
}