public class MyLinkedList<E extends Comparable<E>> {
private Node<E> head, tail;
private int size = 0; // Number of elements in the list
public MyLinkedList() {
head=tail=null;
size=0;
}
/** Return the head element in the list */
public E getFirst() {
if (size == 0) {
return null;
}
else {
return head.element;
}
}
/** Return the last element in the list */
public E getLast() {
if (size == 0) {
return null;
}
else {
return tail.element;
}
}
/** Add an element to the beginning of the list */
public void addFirst(E e) {
Node<E> newNode = new Node<>(e); // Create a new node
newNode.next = head; // link the new node with the head
head = newNode; // head points to the new node
size++; // Increase list size
if (tail == null) // the new node is the only node in list
tail = head;
}
/** Add an element to the end of the list */
public void addLast(E e) {
Node<E> newNode = new Node<>(e); // Create a new for element e
if (tail == null) {
head = tail = newNode; // The new node is the only node in list
}
else {
tail.next = newNode; // Link the new with the last node
tail = newNode; // tail now points to the last node
}
size++; // Increase size
}
public void add(int index, E e) {
if (index == 0) {
addFirst(e);
}
else if (index >= size) {
addLast(e);
}
else {
Node<E> current = head;
for (int i = 1; i < index; i++) {
current = current.next;
}
Node<E> temp = current.next;
current.next = new Node<>(e);
(current.next).next = temp;
size++;
}
}
//Add e to the end of this linkedlist
public void add(E e) {
// Left as Exercise
}
/** Remove the head node and
* return the object that is contained in the removed node. */
public E removeFirst() {
if (size == 0) {
return null;
}
else {
E temp = head.element;
head = head.next;
size--;
if (head == null) {
tail = null;
}
return temp;
}
}
/** Remove the last node and
* return the object that is contained in the removed node. */
public E removeLast() {
if (size == 0) {
return null;
}
else if (size == 1) {
E temp = head.element;
head = tail = null;
size = 0;
return temp;
}
else {
Node<E> current = head;
for (int i = 0; i < size - 2; i++) {
current = current.next;
}
E temp = tail.element;
tail = current;
tail.next = null;
size--;
return temp;
}
}
/** Remove the element at the specified position in this
* list. Return the element that was removed from the list. */
public E remove(int index) {
if (index < 0 || index >= size) {
return null;
}
else if (index == 0) {
return removeFirst();
}
else if (index == size - 1) {
return removeLast();
}
else {
Node<E> previous = head;
for (int i = 1; i < index; i++) {
previous = previous.next;
}
Node<E> current = previous.next;
previous.next = current.next;
size--;
return current.element;
}
}
/** Clear the list */
public void clear() {
size = 0;
head = tail = null;
}
/** Override toString() to return elements in the list in [] separated by , */
public String toString() {
StringBuilder result = new StringBuilder("[");
Node<E> current = head;
for (int i = 0; i < size; i++) {
result.append(current.element);
current = current.next;
if (current != null) {
result.append(", "); // Separate two elements with a comma
}
else {
result.append("]"); // Insert the closing ] in the string
}
}
return result.toString();
}
/** Return true if this list contains no elements */
public boolean isEmpty() {
return size==0;
}
/** Return the number of elements in this list */
public int size() {
// Left as exercise
return 0;
}
/** Remove the first occurrence of the element e
* from this list. Return true if the element is removed. */
public boolean remove(E value)
{
// Left as Exercise
return false;
}
/** Adds a new node containing new value after the given index. */
void addAfter(int index, E value)
{
// Left as exercise
}
/** replaces the data in the node at position index with newValue,
* if such a position is in the list and returns the previous (old) value that was at that location.
* Prints an error message and returns null if index is out of range. */
public E set(int index, E e) {
// Left as an exercise
return null;
}
/** Remove all the occurrences of the element e
* from this list. Return true if the element is removed. */
public boolean removeAll(E e) {
// Left as Exercise
return true;
}
/** Return the index of the last matching element in
* this list. Return -1 if no match. */
public int lastIndex(E e) {
// Left as an exercise
return -1;
}
/** returns a new copy of the LinkedList. It creates a copy of MyLinkedList.java.
* It creates a new instance of the class of the current object and initializes all its
* fields with exactly the contents of the corresponding fields of this object. */
public MyLinkedList<E> clone(){
// Left as exercise
return null;
}
/** Check to see if this list contains element e */
public boolean contains(E e) {
// Left as Exercise
return false;
}
/** Add a new element at the specified index in this list in ascending order */
public void addInOrder(E value) {
// Left as Exercise
}
/** Overrides the equals method (found in the Object class). */
public boolean equals(Object o) {
// Left as exercise
return true;
}
/** Split the original list in half. The original
* list will continue to reference the
* front half of the original list and the method
* returns a reference to a new list that stores the
* back half of the original list. If the number of
* elements is odd, the extra element should remain
* with the front half of the list. */
public MyLinkedList<E> split(){
// Left as Exercise
return null;
}
/** Print this list in reverse order */
public void printList() {
// Left as Exercise
}
private static class Node<E> {
E element;
Node<E> next;
public Node(E element) {
this.element = element;
}
}
}

More Related Content

PDF
STAGE 2 The Methods 65 points Implement all the methods t.pdf
PDF
Given below is the completed implementation of MyLinkedList class. O.pdf
PDF
Hi,I have added the methods and main class as per your requirement.pdf
PDF
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
PDF
Fix my codeCode.pdf
PDF
LabProgram.javaimport java.util.NoSuchElementException;public .pdf
PDF
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
PDF
please i need help Im writing a program to test the merge sort alg.pdf
STAGE 2 The Methods 65 points Implement all the methods t.pdf
Given below is the completed implementation of MyLinkedList class. O.pdf
Hi,I have added the methods and main class as per your requirement.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
Fix my codeCode.pdf
LabProgram.javaimport java.util.NoSuchElementException;public .pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
please i need help Im writing a program to test the merge sort alg.pdf

Similar to public class MyLinkedListltE extends ComparableltEgtg.pdf (20)

PDF
File LinkedList.java Defines a doubly-l.pdf
PDF
Rewrite this code so it can use a generic type instead of integers. .pdf
PDF
Class DiagramIn the Assignment #10, you are given three files Ass.pdf
PDF
The LinkedList1 class implements a Linked list. class.pdf
PDF
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
PDF
Copy your completed LinkedList class from Lab 3 into the LinkedList..pdf
PDF
Implementation The starter code includes List.java. You should not c.pdf
PDF
import java-util--- public class MyLinkedList{ public static void.pdf
PDF
import java.util.Iterator; import java.util.NoSuchElementException; .pdf
PDF
Note             Given Code modified as required and required met.pdf
DOCX
Please complete all the code as per instructions in Java programming.docx
PDF
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
PDF
Objective The purpose of this exercise is to create a Linked List d.pdf
PDF
package linkedLists- import java-util-Iterator- --- A class representi.pdf
PDF
Please and Thank youObjective The purpose of this exercise is to .pdf
PDF
here is the starter code public class LinkedListPracticeLab.pdf
PDF
Use the singly linked list class introduced in the lab to implement .pdf
PDF
Complete in JavaCardApp.javapublic class CardApp { private.pdf
PDF
There are a couple of new methods that you will be writing for this pr.pdf
PDF
Note- Can someone help me with the Public boolean add(E value) method.pdf
File LinkedList.java Defines a doubly-l.pdf
Rewrite this code so it can use a generic type instead of integers. .pdf
Class DiagramIn the Assignment #10, you are given three files Ass.pdf
The LinkedList1 class implements a Linked list. class.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
Copy your completed LinkedList class from Lab 3 into the LinkedList..pdf
Implementation The starter code includes List.java. You should not c.pdf
import java-util--- public class MyLinkedList{ public static void.pdf
import java.util.Iterator; import java.util.NoSuchElementException; .pdf
Note             Given Code modified as required and required met.pdf
Please complete all the code as per instructions in Java programming.docx
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
Objective The purpose of this exercise is to create a Linked List d.pdf
package linkedLists- import java-util-Iterator- --- A class representi.pdf
Please and Thank youObjective The purpose of this exercise is to .pdf
here is the starter code public class LinkedListPracticeLab.pdf
Use the singly linked list class introduced in the lab to implement .pdf
Complete in JavaCardApp.javapublic class CardApp { private.pdf
There are a couple of new methods that you will be writing for this pr.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdf
Ad

More from accostinternational (20)

PDF
6 In humans the Rh blood group has only two alleles R and.pdf
PDF
Cual de los siguientes es verdadero El uso tico de las c.pdf
PDF
Consider the transaction database represented by the matrix .pdf
PDF
Complete the following table SourceAnnual Dose .pdf
PDF
Professional Development Initiative Scenario As current o.pdf
PDF
Write python code to do the following Define a class call.pdf
PDF
When attempting to determine the identity of a person who re.pdf
PDF
What are your 2 favorite ways to communicate with someone pe.pdf
PDF
C Jones is a 63yearold female with a fiveyear history of.pdf
PDF
We know the following expected returns for stocks A and B g.pdf
PDF
The following assumptions and exhibits will be used in prepa.pdf
PDF
Task 4 The scope of variables What is the output of the fol.pdf
PDF
A Describir cmo se transporta la informacin gentica en l.pdf
PDF
EXHIBIT 515 Payroll System FlowchartFollowing is a selec.pdf
PDF
Regarding the ENS The descending interneurons release 1 t.pdf
PDF
React mui question I am passing a data into a Item Every.pdf
PDF
please and thank you Adapted from Kristen St John James.pdf
PDF
Joe and Jessie are married and have one dependent child Liz.pdf
PDF
K Direktr olarak David bir eitlilik ynetimi program pla.pdf
PDF
khikdhficdadfbfcabcg Change all expression into c.pdf
6 In humans the Rh blood group has only two alleles R and.pdf
Cual de los siguientes es verdadero El uso tico de las c.pdf
Consider the transaction database represented by the matrix .pdf
Complete the following table SourceAnnual Dose .pdf
Professional Development Initiative Scenario As current o.pdf
Write python code to do the following Define a class call.pdf
When attempting to determine the identity of a person who re.pdf
What are your 2 favorite ways to communicate with someone pe.pdf
C Jones is a 63yearold female with a fiveyear history of.pdf
We know the following expected returns for stocks A and B g.pdf
The following assumptions and exhibits will be used in prepa.pdf
Task 4 The scope of variables What is the output of the fol.pdf
A Describir cmo se transporta la informacin gentica en l.pdf
EXHIBIT 515 Payroll System FlowchartFollowing is a selec.pdf
Regarding the ENS The descending interneurons release 1 t.pdf
React mui question I am passing a data into a Item Every.pdf
please and thank you Adapted from Kristen St John James.pdf
Joe and Jessie are married and have one dependent child Liz.pdf
K Direktr olarak David bir eitlilik ynetimi program pla.pdf
khikdhficdadfbfcabcg Change all expression into c.pdf
Ad

Recently uploaded (20)

PDF
Skin Care and Cosmetic Ingredients Dictionary ( PDFDrive ).pdf
PDF
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
PDF
Myanmar Dental Journal, The Journal of the Myanmar Dental Association (2013).pdf
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PDF
LEARNERS WITH ADDITIONAL NEEDS ProfEd Topic
PDF
Journal of Dental Science - UDMY (2022).pdf
PPTX
INSTRUMENT AND INSTRUMENTATION PRESENTATION
PDF
PowerPoint for Climate Change by T.T.pdf
PDF
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
PDF
Climate and Adaptation MCQs class 7 from chatgpt
PDF
International_Financial_Reporting_Standa.pdf
PDF
IP : I ; Unit I : Preformulation Studies
PPTX
Climate Change and Its Global Impact.pptx
PDF
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
PDF
fundamentals-of-heat-and-mass-transfer-6th-edition_incropera.pdf
PDF
Race Reva University – Shaping Future Leaders in Artificial Intelligence
PDF
Literature_Review_methods_ BRACU_MKT426 course material
PDF
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
PPTX
What’s under the hood: Parsing standardized learning content for AI
PDF
Journal of Dental Science - UDMY (2021).pdf
Skin Care and Cosmetic Ingredients Dictionary ( PDFDrive ).pdf
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
Myanmar Dental Journal, The Journal of the Myanmar Dental Association (2013).pdf
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
LEARNERS WITH ADDITIONAL NEEDS ProfEd Topic
Journal of Dental Science - UDMY (2022).pdf
INSTRUMENT AND INSTRUMENTATION PRESENTATION
PowerPoint for Climate Change by T.T.pdf
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
Climate and Adaptation MCQs class 7 from chatgpt
International_Financial_Reporting_Standa.pdf
IP : I ; Unit I : Preformulation Studies
Climate Change and Its Global Impact.pptx
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
fundamentals-of-heat-and-mass-transfer-6th-edition_incropera.pdf
Race Reva University – Shaping Future Leaders in Artificial Intelligence
Literature_Review_methods_ BRACU_MKT426 course material
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
What’s under the hood: Parsing standardized learning content for AI
Journal of Dental Science - UDMY (2021).pdf

public class MyLinkedListltE extends ComparableltEgtg.pdf

  • 1. public class MyLinkedList<E extends Comparable<E>> { private Node<E> head, tail; private int size = 0; // Number of elements in the list public MyLinkedList() { head=tail=null; size=0; } /** Return the head element in the list */ public E getFirst() { if (size == 0) { return null; } else { return head.element; } } /** Return the last element in the list */ public E getLast() { if (size == 0) { return null; } else { return tail.element; } } /** Add an element to the beginning of the list */ public void addFirst(E e) { Node<E> newNode = new Node<>(e); // Create a new node newNode.next = head; // link the new node with the head head = newNode; // head points to the new node size++; // Increase list size if (tail == null) // the new node is the only node in list tail = head; } /** Add an element to the end of the list */ public void addLast(E e) { Node<E> newNode = new Node<>(e); // Create a new for element e
  • 2. if (tail == null) { head = tail = newNode; // The new node is the only node in list } else { tail.next = newNode; // Link the new with the last node tail = newNode; // tail now points to the last node } size++; // Increase size } public void add(int index, E e) { if (index == 0) { addFirst(e); } else if (index >= size) { addLast(e); } else { Node<E> current = head; for (int i = 1; i < index; i++) { current = current.next; } Node<E> temp = current.next; current.next = new Node<>(e); (current.next).next = temp; size++; } } //Add e to the end of this linkedlist public void add(E e) { // Left as Exercise } /** Remove the head node and * return the object that is contained in the removed node. */ public E removeFirst() {
  • 3. if (size == 0) { return null; } else { E temp = head.element; head = head.next; size--; if (head == null) { tail = null; } return temp; } } /** Remove the last node and * return the object that is contained in the removed node. */ public E removeLast() { if (size == 0) { return null; } else if (size == 1) { E temp = head.element; head = tail = null; size = 0; return temp; } else { Node<E> current = head; for (int i = 0; i < size - 2; i++) { current = current.next; } E temp = tail.element; tail = current; tail.next = null; size--; return temp; } } /** Remove the element at the specified position in this * list. Return the element that was removed from the list. */ public E remove(int index) {
  • 4. if (index < 0 || index >= size) { return null; } else if (index == 0) { return removeFirst(); } else if (index == size - 1) { return removeLast(); } else { Node<E> previous = head; for (int i = 1; i < index; i++) { previous = previous.next; } Node<E> current = previous.next; previous.next = current.next; size--; return current.element; } } /** Clear the list */ public void clear() { size = 0; head = tail = null; } /** Override toString() to return elements in the list in [] separated by , */ public String toString() { StringBuilder result = new StringBuilder("["); Node<E> current = head; for (int i = 0; i < size; i++) { result.append(current.element); current = current.next; if (current != null) { result.append(", "); // Separate two elements with a comma } else {
  • 5. result.append("]"); // Insert the closing ] in the string } } return result.toString(); } /** Return true if this list contains no elements */ public boolean isEmpty() { return size==0; } /** Return the number of elements in this list */ public int size() { // Left as exercise return 0; } /** Remove the first occurrence of the element e * from this list. Return true if the element is removed. */ public boolean remove(E value) { // Left as Exercise return false; } /** Adds a new node containing new value after the given index. */ void addAfter(int index, E value) { // Left as exercise } /** replaces the data in the node at position index with newValue, * if such a position is in the list and returns the previous (old) value that was at that location. * Prints an error message and returns null if index is out of range. */ public E set(int index, E e) { // Left as an exercise return null; } /** Remove all the occurrences of the element e * from this list. Return true if the element is removed. */ public boolean removeAll(E e) { // Left as Exercise return true;
  • 6. } /** Return the index of the last matching element in * this list. Return -1 if no match. */ public int lastIndex(E e) { // Left as an exercise return -1; } /** returns a new copy of the LinkedList. It creates a copy of MyLinkedList.java. * It creates a new instance of the class of the current object and initializes all its * fields with exactly the contents of the corresponding fields of this object. */ public MyLinkedList<E> clone(){ // Left as exercise return null; } /** Check to see if this list contains element e */ public boolean contains(E e) { // Left as Exercise return false; } /** Add a new element at the specified index in this list in ascending order */ public void addInOrder(E value) { // Left as Exercise } /** Overrides the equals method (found in the Object class). */ public boolean equals(Object o) { // Left as exercise return true; } /** Split the original list in half. The original * list will continue to reference the * front half of the original list and the method * returns a reference to a new list that stores the * back half of the original list. If the number of * elements is odd, the extra element should remain * with the front half of the list. */ public MyLinkedList<E> split(){ // Left as Exercise
  • 7. return null; } /** Print this list in reverse order */ public void printList() { // Left as Exercise } private static class Node<E> { E element; Node<E> next; public Node(E element) { this.element = element; } } }