-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
102 lines (88 loc) · 1.66 KB
/
LinkedList.java
File metadata and controls
102 lines (88 loc) · 1.66 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
/**
Creates a Circularly Double Linked List. Intended for use in a Fibonacci Heap. Uses the node in ListNode.java (@see ListNode.java).
@author Josh Sharpe
**/
public class LinkedList
{
private ListNode head;
private ListNode tail;
private int size;
public
LinkedList()
{
head = null;
tail = null;
size = 0;
}
public int
getSize()
{
return this.size;
}
public ListNode
getHead()
{
return this.head;
}
public ListNode
getTail()
{
return this.tail;
}
public void
addToFront(ListNode newNode)
{
if(this.isEmpty())
{
this.head = newNode;
this.tail = newNode;
this.head.setPrev(this.tail);
this.tail.setNext(this.head);
}
else
{
newNode.setNext(this.head);
newNode.setPrev(this.tail);
this.head.setPrev(newNode);
this.tail.setNext(newNode);
this.head = newNode;
}
this.size++;
}
public ListNode
removeFromFront()
{
ListNode oldHead = this.head;
ListNode newHead = this.head.getNext();
this.tail.setNext(newHead);
newHead.setPrev(this.tail);
this.size--;
this.head = newHead;
return oldHead;
}
public void
printList()
{
ListNode tmp = this.head;
System.out.printf("Forward list: ");
for(int i=0; i<this.size; i++)
{
System.out.printf(" %d ", tmp.getValue());
tmp = tmp.getNext();
}
System.out.printf("\n");
tmp = this.tail;
System.out.printf("Backward list: ");
for(int i=0; i<this.size; i++)
{
System.out.printf(" %d ", tmp.getValue());
tmp = tmp.getPrev();
}
System.out.printf("\n");
}
public boolean
isEmpty()
{
return this.size == 0;
}
}