-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyLinkedList.java
77 lines (71 loc) · 1.04 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
public class MyLinkedList<X>
{
Node head, tail;
int size;
class Node
{
X obj;
Node next, prev;
Node(X o)
{
obj = o;
next = prev = null;
}
}
public MyLinkedList()
{
head = tail = null;
size = 0;
}
public boolean IsEmpty()
{
return (head==null);
}
public boolean IsMember(X o)
{
Node temp = head;
while(temp != null)
{
if(temp.obj.equals(o))
return true;
temp = temp.next;
}
return false;
}
public void Insert(X o)
{
Node tmp = new Node(o);
size++;
if(head==null)
{
head = tail = tmp;
return;
}
tail.next = tmp;
tmp.prev = tail;
tail = tmp;
}
public void Delete(X o)
{
Node temp = head;
while(temp != null)
{
if(temp.obj.equals(o))
{
Node t = temp.next;
if(temp.prev != null) temp.prev.next = t;
else head = t;
if(t != null) t.prev = temp.prev;
else tail = temp.prev;
size--;
return;
}
temp = temp.next;
}
throw new RuntimeException("Error - Given object is not present in list");
}
public int size()
{
return size;
}
}