SlideShare a Scribd company logo
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

Similar to public class MyLinkedListltE extends ComparableltEgtg.pdf

Please complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docxPlease complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docx
cgraciela1
 
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdfpackage com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
aptind
 
Note- Can someone help me with the private E get(int index- int curren (1).docx
Note- Can someone help me with the private E get(int index- int curren (1).docxNote- Can someone help me with the private E get(int index- int curren (1).docx
Note- Can someone help me with the private E get(int index- int curren (1).docx
VictorzH8Bondx
 
we using java code DynamicArrayjava Replace all .pdf
we using java code   DynamicArrayjava   Replace all .pdfwe using java code   DynamicArrayjava   Replace all .pdf
we using java code DynamicArrayjava Replace all .pdf
gudduraza28
 
Note- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdfNote- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdf
Stewart29UReesa
 
please read below it will tell you what we are using L.pdf
please read below it will tell you what we are using   L.pdfplease read below it will tell you what we are using   L.pdf
please read below it will tell you what we are using L.pdf
ankit11134
 
Class DiagramIn the Assignment #10, you are given three files Ass.pdf
Class DiagramIn the Assignment #10, you are given three files Ass.pdfClass DiagramIn the Assignment #10, you are given three files Ass.pdf
Class DiagramIn the Assignment #10, you are given three files Ass.pdf
xlynettalampleyxc
 
please read the steps below and it will tell you what we usi.pdf
please read the steps below and it will tell you what we usi.pdfplease read the steps below and it will tell you what we usi.pdf
please read the steps below and it will tell you what we usi.pdf
aggarwalopticalsco
 
please help me in C++Objective Create a singly linked list of num.pdf
please help me in C++Objective Create a singly linked list of num.pdfplease help me in C++Objective Create a singly linked list of num.pdf
please help me in C++Objective Create a singly linked list of num.pdf
aminbijal86
 
This is problem is same problem which i submitted on 22017, I just.pdf
This is problem is same problem which i submitted on 22017, I just.pdfThis is problem is same problem which i submitted on 22017, I just.pdf
This is problem is same problem which i submitted on 22017, I just.pdf
fcaindore
 
Implementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdfImplementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdf
maheshkumar12354
 
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjhlinked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
vasavim9
 
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdfimport java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
Stewart29UReesa
 
package linkedLists- import java-util-Iterator- --- A class representi.pdf
package linkedLists- import java-util-Iterator- --- A class representi.pdfpackage linkedLists- import java-util-Iterator- --- A class representi.pdf
package linkedLists- import java-util-Iterator- --- A class representi.pdf
arcellzone
 
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
 The MyLinkedList class used in Listing 24.6 is a one-way directional .docx The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
Komlin1
 
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdfImplement the additional 5 methods as indicated in the LinkedList fi.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
footstatus
 
To complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdfTo complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdf
ezycolours78
 
Please do parts labeled TODO LinkedList.java Replace.pdf
Please do parts labeled TODO LinkedList.java Replace.pdfPlease do parts labeled TODO LinkedList.java Replace.pdf
Please do parts labeled TODO LinkedList.java Replace.pdf
aioils
 
Fix my codeCode.pdf
Fix my codeCode.pdfFix my codeCode.pdf
Fix my codeCode.pdf
Conint29
 
Note- Can someone help me with the public boolean isEmpty()- public bo.pdf
Note- Can someone help me with the public boolean isEmpty()- public bo.pdfNote- Can someone help me with the public boolean isEmpty()- public bo.pdf
Note- Can someone help me with the public boolean isEmpty()- public bo.pdf
Augstore
 

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

Please complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docxPlease complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docx
 
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdfpackage com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
 
Note- Can someone help me with the private E get(int index- int curren (1).docx
Note- Can someone help me with the private E get(int index- int curren (1).docxNote- Can someone help me with the private E get(int index- int curren (1).docx
Note- Can someone help me with the private E get(int index- int curren (1).docx
 
we using java code DynamicArrayjava Replace all .pdf
we using java code   DynamicArrayjava   Replace all .pdfwe using java code   DynamicArrayjava   Replace all .pdf
we using java code DynamicArrayjava Replace all .pdf
 
Note- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdfNote- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdf
 
please read below it will tell you what we are using L.pdf
please read below it will tell you what we are using   L.pdfplease read below it will tell you what we are using   L.pdf
please read below it will tell you what we are using L.pdf
 
Class DiagramIn the Assignment #10, you are given three files Ass.pdf
Class DiagramIn the Assignment #10, you are given three files Ass.pdfClass DiagramIn the Assignment #10, you are given three files Ass.pdf
Class DiagramIn the Assignment #10, you are given three files Ass.pdf
 
please read the steps below and it will tell you what we usi.pdf
please read the steps below and it will tell you what we usi.pdfplease read the steps below and it will tell you what we usi.pdf
please read the steps below and it will tell you what we usi.pdf
 
please help me in C++Objective Create a singly linked list of num.pdf
please help me in C++Objective Create a singly linked list of num.pdfplease help me in C++Objective Create a singly linked list of num.pdf
please help me in C++Objective Create a singly linked list of num.pdf
 
This is problem is same problem which i submitted on 22017, I just.pdf
This is problem is same problem which i submitted on 22017, I just.pdfThis is problem is same problem which i submitted on 22017, I just.pdf
This is problem is same problem which i submitted on 22017, I just.pdf
 
Implementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdfImplementation The starter code includes List.java. You should not c.pdf
Implementation The starter code includes List.java. You should not c.pdf
 
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjhlinked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
 
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdfimport java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
 
package linkedLists- import java-util-Iterator- --- A class representi.pdf
package linkedLists- import java-util-Iterator- --- A class representi.pdfpackage linkedLists- import java-util-Iterator- --- A class representi.pdf
package linkedLists- import java-util-Iterator- --- A class representi.pdf
 
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
 The MyLinkedList class used in Listing 24.6 is a one-way directional .docx The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
 
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdfImplement the additional 5 methods as indicated in the LinkedList fi.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
 
To complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdfTo complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdf
 
Please do parts labeled TODO LinkedList.java Replace.pdf
Please do parts labeled TODO LinkedList.java Replace.pdfPlease do parts labeled TODO LinkedList.java Replace.pdf
Please do parts labeled TODO LinkedList.java Replace.pdf
 
Fix my codeCode.pdf
Fix my codeCode.pdfFix my codeCode.pdf
Fix my codeCode.pdf
 
Note- Can someone help me with the public boolean isEmpty()- public bo.pdf
Note- Can someone help me with the public boolean isEmpty()- public bo.pdfNote- Can someone help me with the public boolean isEmpty()- public bo.pdf
Note- Can someone help me with the public boolean isEmpty()- public bo.pdf
 

More from accostinternational

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

More from accostinternational (20)

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

Recently uploaded

Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
Celine George
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 

Recently uploaded (20)

Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.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; } } }