SlideShare a Scribd company logo
1 of 5
Download to read offline
package linkedLists;
import java.util.Iterator;
/** A class representing a singly linked list from scratch. Fill in code.
*
* Note: you may NOT use any of Java's built in classes that store a collection of elements
* such as ArrayList, LinkedList (Java's built in), HashMap, HashTree, HashSet etc. */
public class LinkedList {
private Node head, tail;
/** Constructor */
public LinkedList() {
head = null;
tail = null;
}
/**
* Creates a new node with the given element and adds it to the back of the
* list. No need to change this method.
*/
public void append(int elem) {
Node newNode = new Node(elem);
if (tail != null) {
tail.setNext(newNode);
tail = newNode;
} else {
head = tail = newNode;
}
}
/** Prints all the nodes in the link list. No need to change this method. */
public void printNodes() {
Node current = head;
while (current != null) {
System.out.print(current.elem() + " ");
current = current.next();
}
}
/**
* Return a sublist of this list where the values of elements are in the range
* from value1 to value2, inclusive.
* Your method should not destroy the original list and its nodes should *not* reference
* the nodes in the input list (you need to create new nodes instead).
* Example:
* If the list is 6->40->3->17->1 and value1 is 3 and value2 is 20,
* then the result should be 6->3->17.
* @param value1 value 1
* @param value2 value 2
* @return a sublist of this list where the values of elements are in the range
* * from value1 to value2, inclusive.
*/
public LinkedList sublist(int value1, int value2) {
LinkedList res = new LinkedList();
// FILL IN CODE
return res;
}
/**
* Insert a new node with the given element into the sorted linked list.
* Insert it in the right place based on the value in the node. Assume the
* list is sorted by the elem, from smallest to largest. The
* list should remain sorted after this insert operation.
* Example: If this list is 5->10->18 and we insert 15, then after that the operation,
* the list will become 5->10->15->18.
*/
public void insertInSortedList(int elem) {
// insert a node into the sorted list
// FILL IN CODE
}
/**
* Assume this linked list is sorted in ascending order, and we do not know the
* * number of elements.
* * Return a LinkedList that contains k largest elements in the list.
* * Use slow & fast pointers to find the k-th node from the end (required). Note: This method
* * should be linear and should not count the number of nodes. Do NOT use reverse().
* @param k index from the end
* @return linked list that contains k largest elements (k elements from the end of the list)
*/
public LinkedList getKLargest(int k) {
LinkedList result = new LinkedList();
// FILL IN CODE
return result;
}
/**
* Merge two sorted linked lists into a single sorted linked list.
*
* @param list1
* @param list2
* Your method should not destroy the original list and its nodes should *not* reference
* the nodes in the input list (you need to create new nodes instead).
*/
public static LinkedList mergeSortedLists(LinkedList list1, LinkedList list2) {
LinkedList res = new LinkedList();
// FILL IN CODE
return res;
}
/** Return an iterator that allows to traverse the list */
public Iterator<Node> iterator() {
return new ListIterator(0);
}
/**
* The inner class that represents the iterator for the linked list.
* Iterates over the nodes of the list. No need to modify.
*/
private class ListIterator implements Iterator<Node> {
Node curr = head;
public ListIterator(int index) {
int count = 0;
while (curr != null && count < index) {
curr = curr.next();
count++;
}
// System.out.println(curr == head);
}
@Override
public boolean hasNext() {
return curr != null;
}
@Override
public Node next() {
Node currNode = curr;
curr = curr.next();
return currNode;
}
}
}
package linkedLists;
/* A class representing a node in a singly linked list */
public class Node {
private int elem;
private Node next;
public Node(int elem, Node next) {
this.elem = elem;
this.next = next;
}
public Node(int elem) {
this.elem = elem;
next = null;
}
public int elem() {
return elem;
}
public void setElem(int elem) {
this.elem = elem;
}
public Node next() {
return next;
}
public void setNext(Node other) {
next = other;
}
}
package linkedLists- import java-util-Iterator- --- A class representi.pdf

More Related Content

Similar to package linkedLists- import java-util-Iterator- --- A class representi.pdf

Problem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdfProblem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
kingsandqueens3
 
Dividing a linked list into two sublists of almost equal sizesa. A.pdf
Dividing a linked list into two sublists of almost equal sizesa. A.pdfDividing a linked list into two sublists of almost equal sizesa. A.pdf
Dividing a linked list into two sublists of almost equal sizesa. A.pdf
tesmondday29076
 
Hi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdfHi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdf
annaelctronics
 
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
 
Select three methods in the ObjectList class to work through algori.pdf
Select three methods in the ObjectList class to work through algori.pdfSelect three methods in the ObjectList class to work through algori.pdf
Select three methods in the ObjectList class to work through algori.pdf
aroraopticals15
 
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
 
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
 
A popular implementation of List is ArrayList- Look up how to instanti.pdf
A popular implementation of List is ArrayList- Look up how to instanti.pdfA popular implementation of List is ArrayList- Look up how to instanti.pdf
A popular implementation of List is ArrayList- Look up how to instanti.pdf
arsarees
 
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
 
Lecture 18Dynamic Data Structures and Generics (II).docx
Lecture 18Dynamic Data Structures and Generics (II).docxLecture 18Dynamic Data Structures and Generics (II).docx
Lecture 18Dynamic Data Structures and Generics (II).docx
SHIVA101531
 
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
 
public class MyLinkedListltE extends ComparableltEgtg.pdf
public class MyLinkedListltE extends ComparableltEgtg.pdfpublic class MyLinkedListltE extends ComparableltEgtg.pdf
public class MyLinkedListltE extends ComparableltEgtg.pdf
accostinternational
 
you will implement some sorting algorithms for arrays and linked lis.pdf
you will implement some sorting algorithms for arrays and linked lis.pdfyou will implement some sorting algorithms for arrays and linked lis.pdf
you will implement some sorting algorithms for arrays and linked lis.pdf
clearvisioneyecareno
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdf
EricvtJFraserr
 
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdf
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdfNeed Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdf
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdf
Edwardw5nSlaterl
 
File LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdfFile LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdf
Conint29
 
Use C++ Write a function to merge two doubly linked lists. The input.pdf
Use C++ Write a function to merge two doubly linked lists. The input.pdfUse C++ Write a function to merge two doubly linked lists. The input.pdf
Use C++ Write a function to merge two doubly linked lists. The input.pdf
shalins6
 
How to do insertion sort on a singly linked list with no header usin.pdf
How to do insertion sort on a singly linked list with no header usin.pdfHow to do insertion sort on a singly linked list with no header usin.pdf
How to do insertion sort on a singly linked list with no header usin.pdf
arihantelehyb
 
Implement the interface you wrote for Lab B (EntryWayListInterface)..pdf
Implement the interface you wrote for Lab B (EntryWayListInterface)..pdfImplement the interface you wrote for Lab B (EntryWayListInterface)..pdf
Implement the interface you wrote for Lab B (EntryWayListInterface)..pdf
rishabjain5053
 

Similar to package linkedLists- import java-util-Iterator- --- A class representi.pdf (20)

Problem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdfProblem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
 
Dividing a linked list into two sublists of almost equal sizesa. A.pdf
Dividing a linked list into two sublists of almost equal sizesa. A.pdfDividing a linked list into two sublists of almost equal sizesa. A.pdf
Dividing a linked list into two sublists of almost equal sizesa. A.pdf
 
Hi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdfHi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.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
 
Select three methods in the ObjectList class to work through algori.pdf
Select three methods in the ObjectList class to work through algori.pdfSelect three methods in the ObjectList class to work through algori.pdf
Select three methods in the ObjectList class to work through algori.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
 
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
 
A popular implementation of List is ArrayList- Look up how to instanti.pdf
A popular implementation of List is ArrayList- Look up how to instanti.pdfA popular implementation of List is ArrayList- Look up how to instanti.pdf
A popular implementation of List is ArrayList- Look up how to instanti.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
 
Lecture 18Dynamic Data Structures and Generics (II).docx
Lecture 18Dynamic Data Structures and Generics (II).docxLecture 18Dynamic Data Structures and Generics (II).docx
Lecture 18Dynamic Data Structures and Generics (II).docx
 
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
 
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
 
public class MyLinkedListltE extends ComparableltEgtg.pdf
public class MyLinkedListltE extends ComparableltEgtg.pdfpublic class MyLinkedListltE extends ComparableltEgtg.pdf
public class MyLinkedListltE extends ComparableltEgtg.pdf
 
you will implement some sorting algorithms for arrays and linked lis.pdf
you will implement some sorting algorithms for arrays and linked lis.pdfyou will implement some sorting algorithms for arrays and linked lis.pdf
you will implement some sorting algorithms for arrays and linked lis.pdf
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdf
 
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdf
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdfNeed Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdf
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdf
 
File LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdfFile LinkedList.java Defines a doubly-l.pdf
File LinkedList.java Defines a doubly-l.pdf
 
Use C++ Write a function to merge two doubly linked lists. The input.pdf
Use C++ Write a function to merge two doubly linked lists. The input.pdfUse C++ Write a function to merge two doubly linked lists. The input.pdf
Use C++ Write a function to merge two doubly linked lists. The input.pdf
 
How to do insertion sort on a singly linked list with no header usin.pdf
How to do insertion sort on a singly linked list with no header usin.pdfHow to do insertion sort on a singly linked list with no header usin.pdf
How to do insertion sort on a singly linked list with no header usin.pdf
 
Implement the interface you wrote for Lab B (EntryWayListInterface)..pdf
Implement the interface you wrote for Lab B (EntryWayListInterface)..pdfImplement the interface you wrote for Lab B (EntryWayListInterface)..pdf
Implement the interface you wrote for Lab B (EntryWayListInterface)..pdf
 

More from arcellzone

package ex2- public class Exercise2-E- { private static class N.pdf
package ex2- public class Exercise2-E- {        private static class N.pdfpackage ex2- public class Exercise2-E- {        private static class N.pdf
package ex2- public class Exercise2-E- { private static class N.pdf
arcellzone
 
Part A List five 21st Century Skills you need to develop- Part B Ass.pdf
Part A List five 21st Century Skills you need to develop-   Part B Ass.pdfPart A List five 21st Century Skills you need to develop-   Part B Ass.pdf
Part A List five 21st Century Skills you need to develop- Part B Ass.pdf
arcellzone
 
package hw1-public class Runner { public static void main(String-- arg (1).pdf
package hw1-public class Runner { public static void main(String-- arg (1).pdfpackage hw1-public class Runner { public static void main(String-- arg (1).pdf
package hw1-public class Runner { public static void main(String-- arg (1).pdf
arcellzone
 

More from arcellzone (20)

package ex2- public class Exercise2-E- { private static class N.pdf
package ex2- public class Exercise2-E- {        private static class N.pdfpackage ex2- public class Exercise2-E- {        private static class N.pdf
package ex2- public class Exercise2-E- { private static class N.pdf
 
Part A List five 21st Century Skills you need to develop- Part B Ass.pdf
Part A List five 21st Century Skills you need to develop-   Part B Ass.pdfPart A List five 21st Century Skills you need to develop-   Part B Ass.pdf
Part A List five 21st Century Skills you need to develop- Part B Ass.pdf
 
Part 5- Use the 105CLl to Gather Device Information (10 points) Issue.pdf
Part 5- Use the 105CLl to Gather Device Information (10 points) Issue.pdfPart 5- Use the 105CLl to Gather Device Information (10 points) Issue.pdf
Part 5- Use the 105CLl to Gather Device Information (10 points) Issue.pdf
 
Part 8- Use the simplified sum-of-minterms expressions to generate the (1).pdf
Part 8- Use the simplified sum-of-minterms expressions to generate the (1).pdfPart 8- Use the simplified sum-of-minterms expressions to generate the (1).pdf
Part 8- Use the simplified sum-of-minterms expressions to generate the (1).pdf
 
Part 2- Character -- Reminder- all data objects should have getters an.pdf
Part 2- Character -- Reminder- all data objects should have getters an.pdfPart 2- Character -- Reminder- all data objects should have getters an.pdf
Part 2- Character -- Reminder- all data objects should have getters an.pdf
 
Part 1- Research the Importance of an IT Asset Inventory Conduct an in.pdf
Part 1- Research the Importance of an IT Asset Inventory Conduct an in.pdfPart 1- Research the Importance of an IT Asset Inventory Conduct an in.pdf
Part 1- Research the Importance of an IT Asset Inventory Conduct an in.pdf
 
Part 2 Water Trace a molecule of water from the renal artery to the re.pdf
Part 2 Water Trace a molecule of water from the renal artery to the re.pdfPart 2 Water Trace a molecule of water from the renal artery to the re.pdf
Part 2 Water Trace a molecule of water from the renal artery to the re.pdf
 
Part 2 1- Create a list of all the IP addresses used to attempt to log.pdf
Part 2 1- Create a list of all the IP addresses used to attempt to log.pdfPart 2 1- Create a list of all the IP addresses used to attempt to log.pdf
Part 2 1- Create a list of all the IP addresses used to attempt to log.pdf
 
Part 11 atratighisini and youngist will the in lith- thery are fiponit.pdf
Part 11 atratighisini and youngist will the in lith- thery are fiponit.pdfPart 11 atratighisini and youngist will the in lith- thery are fiponit.pdf
Part 11 atratighisini and youngist will the in lith- thery are fiponit.pdf
 
Parsing food data lab - Please answer in JAVA Given a text file contai.pdf
Parsing food data lab - Please answer in JAVA Given a text file contai.pdfParsing food data lab - Please answer in JAVA Given a text file contai.pdf
Parsing food data lab - Please answer in JAVA Given a text file contai.pdf
 
Paris Just Got Cheaper for American Tourists Paris- France- Americans.pdf
Paris Just Got Cheaper for American Tourists Paris- France- Americans.pdfParis Just Got Cheaper for American Tourists Paris- France- Americans.pdf
Paris Just Got Cheaper for American Tourists Paris- France- Americans.pdf
 
Parent generation Cross-fertilization F1 generation F2 generationRead.pdf
Parent generation Cross-fertilization F1 generation F2 generationRead.pdfParent generation Cross-fertilization F1 generation F2 generationRead.pdf
Parent generation Cross-fertilization F1 generation F2 generationRead.pdf
 
Paf is a small country- Its currency is the pif- and the exchange rate.pdf
Paf is a small country- Its currency is the pif- and the exchange rate.pdfPaf is a small country- Its currency is the pif- and the exchange rate.pdf
Paf is a small country- Its currency is the pif- and the exchange rate.pdf
 
package hw1-public class Runner { public static void main(String-- arg (1).pdf
package hw1-public class Runner { public static void main(String-- arg (1).pdfpackage hw1-public class Runner { public static void main(String-- arg (1).pdf
package hw1-public class Runner { public static void main(String-- arg (1).pdf
 
P1) Given the Von Neumann architecture- answer the following questions.pdf
P1) Given the Von Neumann architecture- answer the following questions.pdfP1) Given the Von Neumann architecture- answer the following questions.pdf
P1) Given the Von Neumann architecture- answer the following questions.pdf
 
P1) Answer the following questions (a) We discussed about bottleneck i.pdf
P1) Answer the following questions (a) We discussed about bottleneck i.pdfP1) Answer the following questions (a) We discussed about bottleneck i.pdf
P1) Answer the following questions (a) We discussed about bottleneck i.pdf
 
P1) Answer the following questions ( 40 points) (a) We discussed about.pdf
P1) Answer the following questions ( 40 points) (a) We discussed about.pdfP1) Answer the following questions ( 40 points) (a) We discussed about.pdf
P1) Answer the following questions ( 40 points) (a) We discussed about.pdf
 
Our Space is a social media site that is growing in popularity- The fi.pdf
Our Space is a social media site that is growing in popularity- The fi.pdfOur Space is a social media site that is growing in popularity- The fi.pdf
Our Space is a social media site that is growing in popularity- The fi.pdf
 
P(Zn-kZn1-j)-k!(j)kej (5 points) Let T be the minimal value of n such.pdf
P(Zn-kZn1-j)-k!(j)kej (5 points) Let T be the minimal value of n such.pdfP(Zn-kZn1-j)-k!(j)kej (5 points) Let T be the minimal value of n such.pdf
P(Zn-kZn1-j)-k!(j)kej (5 points) Let T be the minimal value of n such.pdf
 
P(Zn-kZn1-j)-k!(j)kejfn-P(T-10Z1-n)gn-P(T-20Z1-n).pdf
P(Zn-kZn1-j)-k!(j)kejfn-P(T-10Z1-n)gn-P(T-20Z1-n).pdfP(Zn-kZn1-j)-k!(j)kejfn-P(T-10Z1-n)gn-P(T-20Z1-n).pdf
P(Zn-kZn1-j)-k!(j)kejfn-P(T-10Z1-n)gn-P(T-20Z1-n).pdf
 

Recently uploaded

SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
Peter Brusilovsky
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
中 央社
 

Recently uploaded (20)

Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge App
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 

package linkedLists- import java-util-Iterator- --- A class representi.pdf

  • 1. package linkedLists; import java.util.Iterator; /** A class representing a singly linked list from scratch. Fill in code. * * Note: you may NOT use any of Java's built in classes that store a collection of elements * such as ArrayList, LinkedList (Java's built in), HashMap, HashTree, HashSet etc. */ public class LinkedList { private Node head, tail; /** Constructor */ public LinkedList() { head = null; tail = null; } /** * Creates a new node with the given element and adds it to the back of the * list. No need to change this method. */ public void append(int elem) { Node newNode = new Node(elem); if (tail != null) { tail.setNext(newNode); tail = newNode; } else { head = tail = newNode; } } /** Prints all the nodes in the link list. No need to change this method. */ public void printNodes() { Node current = head; while (current != null) { System.out.print(current.elem() + " "); current = current.next(); } } /** * Return a sublist of this list where the values of elements are in the range * from value1 to value2, inclusive. * Your method should not destroy the original list and its nodes should *not* reference * the nodes in the input list (you need to create new nodes instead).
  • 2. * Example: * If the list is 6->40->3->17->1 and value1 is 3 and value2 is 20, * then the result should be 6->3->17. * @param value1 value 1 * @param value2 value 2 * @return a sublist of this list where the values of elements are in the range * * from value1 to value2, inclusive. */ public LinkedList sublist(int value1, int value2) { LinkedList res = new LinkedList(); // FILL IN CODE return res; } /** * Insert a new node with the given element into the sorted linked list. * Insert it in the right place based on the value in the node. Assume the * list is sorted by the elem, from smallest to largest. The * list should remain sorted after this insert operation. * Example: If this list is 5->10->18 and we insert 15, then after that the operation, * the list will become 5->10->15->18. */ public void insertInSortedList(int elem) { // insert a node into the sorted list // FILL IN CODE } /** * Assume this linked list is sorted in ascending order, and we do not know the * * number of elements. * * Return a LinkedList that contains k largest elements in the list. * * Use slow & fast pointers to find the k-th node from the end (required). Note: This method * * should be linear and should not count the number of nodes. Do NOT use reverse(). * @param k index from the end * @return linked list that contains k largest elements (k elements from the end of the list) */ public LinkedList getKLargest(int k) { LinkedList result = new LinkedList(); // FILL IN CODE return result; }
  • 3. /** * Merge two sorted linked lists into a single sorted linked list. * * @param list1 * @param list2 * Your method should not destroy the original list and its nodes should *not* reference * the nodes in the input list (you need to create new nodes instead). */ public static LinkedList mergeSortedLists(LinkedList list1, LinkedList list2) { LinkedList res = new LinkedList(); // FILL IN CODE return res; } /** Return an iterator that allows to traverse the list */ public Iterator<Node> iterator() { return new ListIterator(0); } /** * The inner class that represents the iterator for the linked list. * Iterates over the nodes of the list. No need to modify. */ private class ListIterator implements Iterator<Node> { Node curr = head; public ListIterator(int index) { int count = 0; while (curr != null && count < index) { curr = curr.next(); count++; } // System.out.println(curr == head); } @Override public boolean hasNext() { return curr != null; } @Override public Node next() {
  • 4. Node currNode = curr; curr = curr.next(); return currNode; } } } package linkedLists; /* A class representing a node in a singly linked list */ public class Node { private int elem; private Node next; public Node(int elem, Node next) { this.elem = elem; this.next = next; } public Node(int elem) { this.elem = elem; next = null; } public int elem() { return elem; } public void setElem(int elem) { this.elem = elem; } public Node next() { return next; } public void setNext(Node other) { next = other; } }