-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyLinkedList.java
executable file
·103 lines (83 loc) · 1.36 KB
/
MyLinkedList.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
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
public class MyLinkedList<T> {
Node<T> head;
private int size;
public MyLinkedList() {
// TODO Auto-generated constructor stub
head=new Node<T>();
size=0;
}
public MyLinkedList(Node<T> head)
{
this.head=head;
size=1;
}
public int getSize()
{
return size;
}
public void add(T data)
{
Node<T> node=new Node<T>(data);
node.setNext(head);
head=node;
size++;
}
public void remove(T node)
{
if(node.equals(this.head.getData()))
{
head=head.next;
size--;
return;
}
Node<T> curr=this.head;
//TODO handle the last deletion
while(!curr.next.getData().equals(node))
{
curr=curr.next;
}
curr.next=curr.next.next;
size--;
}
public boolean isEmpty()
{
if(size==0)
{
return true;
}
return false;
}
public boolean contains(Node<T> node)
{
int i=0;
Node<T> curr=head;
while(i<size && !curr.equals(node))
{
curr=curr.next;
i++;
}
if(i==size)
{
return false;
}
else
{
return true;
}
}
public Node<T> get(int i) throws LinkedListOutofBoundsException
{
Node<T> curr=this.head;
if(i>=size)
{
throw new LinkedListOutofBoundsException();
}
int count=size-i-1;
while(count>0)
{
curr=curr.next;
count--;
}
return curr;
}
}