-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlinkedlist.java
More file actions
104 lines (93 loc) · 1.87 KB
/
linkedlist.java
File metadata and controls
104 lines (93 loc) · 1.87 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
public class linkedlist {
public Node head;
public Node tail;
public int size;
public linkedlist()
{
head = new Node();
tail = head;
size = 0;
}
public void addToTail(Object val)
{
}
public void addToHead(Object val)
{
if( head.data == null ){
System.out.println("Adding first item");
head.data = val;
head.next = tail;
head.prev = null;
tail = head;
} else if(tail == head){
System.out.println("Adding second item");
Node newNode = new Node(val);
tail.prev = newNode;
newNode.next = tail;
head = newNode;
} else {
System.out.println("Adding third item");
Node newNode = new Node(val);
head.prev = newNode;
newNode.next = head;
head = newNode;
}
size++;
}
public Object remove(){
Node removed = head;
head = head.next;
Object data = removed.data;
removed = null;
size--;
return data;
}
// Get size of current list
public int size(){
return this.size;
}
private static class Node {
// Object variables
Node next;
Node prev;
Object data;
// NODE CONSTRUCTORS
public Node(){
next = null;
prev = null;
data = null;
}
public Node(Object newData)
{
next = null;
prev = null;
data = newData;
}
public Node(Object newData, Node nextNode)
{
next = nextNode;
prev = null;
data = newData;
}
public Node(Object newData, Node nextNode, Node prevNode)
{
next = nextNode;
prev = prevNode;
data = newData;
}
}
public static void main(String[] args){
linkedlist mylist = new linkedlist();
mylist.addToHead(5);
mylist.addToHead("HELLO");
mylist.addToHead(15);
mylist.addToHead(20);
System.out.println(mylist.size());
System.out.println(mylist.remove());
System.out.println(mylist.size());
System.out.println(mylist.remove());
System.out.println(mylist.size());
System.out.println(mylist.remove());
System.out.println(mylist.size());
}
}