Question: import java.util.Scanner; public class LinkedList > { / / - - - - - - - - - - - - - - - -

import java.util.Scanner;
public class LinkedList>{
//---------------- nested Node class ----------------
private static class Node {
private E data; // reference to the data stored at this node
private Node next; // reference to the subsequent node in the list
public Node(E e, Node n){
data = e;
next = n;
}
public E getData(){
return data; }
public Node getNext(){
return next; }
public void setNext(Node n){
next = n; }
}
// instance variables of the LinkedList
private Node head = null; // head node of the list (or null if empty)
private Node tail = null; // last node of the list (or null if empty)
private int size =0; // number of nodes in the list
public LinkedList(){}// constructs an initially empty list
// access methods
public int size(){
return size; }
public boolean isEmpty(){
return size ==0; }
public E first(){// returns (but does not remove) the first data
if (isEmpty())
return null;
return head.getData();
}
public E last(){// returns (but does not remove) the last data
if (isEmpty())
return null;
return tail.getData();
}
// update methods
public void addFirst(E e){// adds data e to the front of the list
head = new Node>(e, head); // create and link a new node
if (size ==0)
tail = head; // special case: new node becomes tail also
size++;
}
public void addLast(E e){// adds data e to the end of the list
Node newest = new Node>(e, null); // node will eventually be the tail
if (isEmpty())
head = newest; // special case: previously empty list
else
tail.setNext(newest); // new node after existing tail
tail = newest; // new node becomes the tail
size++;
}
public E removeFirst(){// removes and returns the first data
if (isEmpty()) return null; // nothing to remove
E answer = head.getData();
head = head.getNext(); // will become null if list had only one node
size--;
if (size ==0)
tail = null; // special case as list is now empty
return answer;
}
public E removeLast(){// removes and returns the last data
if (isEmpty()) return null; // nothing to remove
E answer = tail.getData();
if (head == tail){// check for only one item on the list
head = null;
tail = null;
size =0;
return answer;
}
Node p = head; // find the next to last item
Node prev;
do {
prev = p;
p = p.getNext();
} while (p != tail);
tail = prev; // make the next to last item the last item
prev.next = null;
size--;
return answer;
}
public int indexOf(E e){// return the position of a value in the list
if (isEmpty()) return -1;
Node p = head;
for (int i =0; i=0;
}
@Override
public boolean equals(Object o){
if (o == null) return false;
if (!(o instanceof LinkedList)) return false;
LinkedList list2=(LinkedList) o;
if (size != list2.size) return false;
Node p = head;
Node q = list2.head;
while (p != null && q != null){
if (! p.getData().equals(q.getData())) return false;
p = p.getNext();
q = q.getNext();
}
return true;
}
@Override
public String toString(){
String result ="";
Node p = head;
while (p != null){
result = result +","+ p.getData();
p = p.getNext();
}
result = result.substring(1);
return result;
}
public static void main(String[] args){
Scanner s = new Scanner(System.in);
LinkedList list1= new LinkedList();
int n = s.nextInt();
for (int i=0; i list2= new LinkedList();
n = s.nextInt();
for (int i=0; i
import java.util.Scanner; public class LinkedList

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!