Professional Documents
Culture Documents
This Study Resource Was
This Study Resource Was
*;
public LinkedList(){
this.head = new Node(null);
this.size = 0;
}
m
if(head.data == null){
er as
head = new Node(item); //add items in list
co
++size;
eH w
}
else{
o.
//Insert element at the end of list
rs e
Node node = new Node(item);
ou urc
Node prev = head; //Access the first element on the list
while(prev.next != null){
prev = prev.next; //reach the last element
o
}
aC s
v i y re
prev.next = node;
size++;
}
}
ed d
node.next = head;
Th
head = node;
++size;
}
else{
Node prev = head;
for(int i = 0; i < pos; i++){
prev = prev.next;
}
This study source was downloaded by 100000829368906 from CourseHero.com on 12-01-2021 09:18:06 GMT -06:00
https://www.coursehero.com/file/92962463/LinkedListjava/
//check if position exists
if(pos > size || pos < 0){
System.out.println("Position is not valid");
return null;
}
if(pos == 0){
Node node = head;
head = head.next;
--size;
return node.data;
}
else{
Node prev = head;
for(int i = 0; i < pos; i++)
prev = prev.next;
Node node = prev.next;
prev.next = node.next;
--size;
return node.data;
}
}
m
er as
public Object get(int pos){
co
Node current = head;
eH w
int count = 0;
if(pos < 0 || pos > size-1){
o.
System.out.println("Position is not valid");
rs e
return null;
ou urc
}
else{
while(current!=null){
if(count == pos){
o
return current.data;
aC s
}
v i y re
count++;
current = current.next;
}
}
return null;
ed d
}
ar stu
next = current.next;
current.next = prev;
prev = current;
current = next;
}
node = prev;
return node;
}
list.add("Beyonce");
This study source was downloaded by 100000829368906 from CourseHero.com on 12-01-2021 09:18:06 GMT -06:00
https://www.coursehero.com/file/92962463/LinkedListjava/
list.add("Rayven Justice");
list.add("Dustin Lynch");
System.out.println(list);
list.add("0, Jonas Brothers");
System.out.println(list);
list.remove(1);
list.get(1);
System.out.println(list);
}
}
m
er as
co
eH w
o.
rs e
ou urc
o
aC s
v i y re
ed d
ar stu
sh is
Th
This study source was downloaded by 100000829368906 from CourseHero.com on 12-01-2021 09:18:06 GMT -06:00
https://www.coursehero.com/file/92962463/LinkedListjava/
Powered by TCPDF (www.tcpdf.org)