Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

import java.util.

*;

public class LinkedList<T>{


Node head;
int size;

private class Node<T>{


Object data;
Node next;

public Node(Object item){


this.data = item;
this.next = null;
}
}

public LinkedList(){
this.head = new Node(null);
this.size = 0;
}

public void add(Object item){

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

public void add(int pos, Object item){


ar stu

//check if position(index) exists


if(pos +1 > size || pos < 0)
System.out.println("Not a valid position");
if(pos == 0){
Node node = new Node(item);
sh is

node.next = head;
Th

head = node;
++size;
}
else{
Node prev = head;
for(int i = 0; i < pos; i++){
prev = prev.next;
}

Node node = new Node(item);


node.next = prev.next;
prev.next = node;
++size;
}
}

public Object remove(int pos){

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

public Object reverse(Node node){


Node prev = null;
Node current = head;
Node next = null;
sh is

while (current != null){


Th

next = current.next;
current.next = prev;
prev = current;
current = next;
}
node = prev;
return node;
}

public int size(){


return this.size;
}

public static void main(String args[]){


LinkedList<String> list = new LinkedList<String>();

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)

You might also like