Hello, I need to create an implementation of a linked list in Java. I use NetBea
ID: 3787781 • Letter: H
Question
Hello, I need to create an implementation of a linked list in Java. I use NetBeans 8.1. The linked list implementation should be in its own class named LinkedList. The linkedlist should hold integers and must contain the following methods:
LinkedList() - Default constructor
addFront(valueToAdd) - Creates a new node and adds it to the front of the linked list.
removeFront() - Removes the front node from the linked list and returns the value it contained.
addBack(valueToAdd) - Creates a new node and adds it to the back of the linked list.
removeBack() - Removes the last node from the linked list and returns the value it contained.
getLength() - Returns the length of the linked list.
display() - Displays the linked list and its contents.
See output below for an example:
Linked List Adding elements to the front. 33-->8 58- 33 ->8 Length of linked list: 3 Adding elements to the back. 58- 33 >8- >1 58- 33 58- 33 >8- >1-- >34- >20 Length of linked list: 6 Removing elements from the front... Length of linked list: 4 Removing elements from the back. >1-- >34 8-- >1 Length of linked list: 2Explanation / Answer
Hi, Please find my implementation.
Please let me know in case of any issue.
############ LinkedList.java ############
class NodeList {
int data;
NodeList next;
public NodeList(int data) {
this.data = data;
next = null;
}
}
public class LinkedList {
private NodeList head;
private int size;
public LinkedList() {
head = null;
size = 0;
}
public void addFront(int data) {
NodeList newNode = new NodeList(data);
newNode.next = head;
head = newNode;
size = size+1;
}
public int removeFront(){
if(head == null){
System.out.println("List is empty");
return -999;
}
int value = head.data;
head = head.next;
size = size-1;
return value;
}
public void addBack(int value){
if(head == null){
head = new NodeList(value);
}else{
NodeList temp = head;
while(temp.next != null)
temp = temp.next;
temp.next = new NodeList(value);
}
size = size+1;
}
public int removeBack(){
// list is empty
if(head == null){
System.out.println("List is empty");
return -999;
}
// only one node
if(head.next == null){
int value = head.data;
head = null;
return value;
}
NodeList temp = head;
while(temp.next.next != null)
temp = temp.next;
int value = temp.next.data;
temp.next = null;
size = size-1;
return value;
}
public int getLength(){
return size;
}
public void display(){
NodeList temp = head;
while(temp != null){
if(temp.next!=null)
System.out.print(temp.data+"-->");
else
System.out.println(temp.data);
temp = temp.next;
}
System.out.println();
}
}
############# LinkedListTest.java ################
public class LinkedListTest {
public static void main(String[] args) {
LinkedList list = new LinkedList();
System.out.println("Adding elements to the front......");
list.addFront(8);
list.display();
list.addFront(33);
list.display();
list.addFront(58);
list.display();
System.out.println("Length of linked list: "+list.getLength());
System.out.println();
System.out.println("Adding elements to the back......");
list.addBack(1);
list.display();
list.addBack(34);
list.display();
list.addBack(20);
list.display();
System.out.println("Length of linked list: "+list.getLength());
System.out.println();
System.out.println("Removing elements from the front.....");
list.removeFront();
list.display();
list.removeFront();
list.display();
System.out.println("Length of linked list: "+list.getLength());
System.out.println();
System.out.println("Removing elements from the back.....");
list.removeBack();
list.display();
list.removeBack();
list.display();
System.out.println("Length of linked list: "+list.getLength());
System.out.println();
}
}
/*
Sample run:
Adding elements to the front......
8
33-->8
58-->33-->8
Length of linked list: 3
Adding elements to the back......
58-->33-->8-->1
58-->33-->8-->1-->34
58-->33-->8-->1-->34-->20
Length of linked list: 6
Removing elements from the front.....
33-->8-->1-->34-->20
8-->1-->34-->20
Length of linked list: 4
Removing elements from the back.....
8-->1-->34
8-->1
Length of linked list: 2
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.