-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyLinkedList.java
164 lines (144 loc) · 3.16 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
class Node<T>
{
public T nodeval;
public Node<T> next;
public Node()
{
next = null;
nodeval = null;
}
public Node(T o, Node<T> n)
{
nodeval = o;
next = n;
}
public T getElement()
{
return nodeval;
}
public Node<T> getNext()
{
return next;
}
public void setNext(Node<T> n)
{
next = n;
}
}
public class MyLinkedList<T>
{
public Node<T> head = new Node();
public Node<T> tail = new Node();
public int size;
public MyLinkedList()
{
head = null;
tail = null;
size = 0;
}
public Boolean IsEmpty()
{
return (size == 0);
}
public int size()
{
return size;
}
public void addLast(T o)
{
Node<T> lastnode = new Node(o,null);
if (this.IsEmpty()){head=lastnode;}
else tail.setNext(lastnode);
tail = lastnode;
size++;
}
public void addFirst(T o)
{
head = new Node(o,head);
if (size==0){tail=head;}
size++;
}
public T removeFirst()
{
if (this.IsEmpty()) return null;
T firstelement = head.getElement();
head = head.getNext();
size--;
if (size == 0)
tail = null;
return firstelement;
}
public MyLinkedList<T> clone() // Makes a copy of the current linked list rather than copying its reference
{
Node<T> pointer = head;
MyLinkedList<T> newll = new MyLinkedList();
while(pointer!=null)
{
newll.addLast(pointer.getElement());
pointer = pointer.getNext();
}
return newll;
}
public Boolean contains(T o)
{
Node<T> pointer = head;
while(pointer!=null)
{
if (pointer.getElement().equals(o))
return true;
pointer=pointer.getNext();
}
return false;
}
public T remove(T o)
{
if (this.IsEmpty() || !(contains(o))) return null;
Node<T> pointer = head;
if (pointer.getElement().equals(o)) {this.removeFirst();}
else
{
while(pointer != null)
{
Node<T> k = pointer.next;
if (k == null)
break;
if (k.getElement().equals(o))
{
pointer.setNext(k.next);
}
pointer = pointer.getNext();
}
}
return o;
}
public MyLinkedList<T> unionlist(MyLinkedList<T> a)
{
MyLinkedList<T> templl = a.clone();
MyLinkedList<T> newll = this.clone();
Node<T> pointer = templl.head;
for(int i=0; i<templl.size(); i++)
{
newll.addLast(pointer.getElement());
pointer = pointer.next;
}
return newll;
}
public T get(int i) // Returns the ith element of the linked list
{
Node<T> pointer = head;
int x = 0;
while (pointer != null)
{
if (x == i)
return(pointer.getElement());
x++;
pointer = pointer.getNext();
}
return null;
}
public void printList() // Prints all the elements of the current linked list
{
Node<T> tmp = this.head;
while(tmp!=null){System.out.println(tmp.getElement());tmp = tmp.getNext();}
}
}