Write a getMatching() method for the MyLinkedList class. The method receives a linked list as parameter and must return a linked list with all elements that are present in both lists. Example calling list:[5.2.7,3,1,4] parameter list: [8,9,2,1,4,2] returned list: [2,1,4]. Note the uniqueness of items in the returned list. Weit test the mothed the

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

 

 

Write a getMatching() method for the MyLinked List class. The method receives a linked list as parameter and must return a linked list
with all elements that are present in both lists. Example calling list:[5,2,7,3,1,4] parameter list: [8,9,2,1,4,2] returned list: [2,1,4]. Note
the uniqueness of items in the returned list.
Write a test program to test the method thoroughly.
Additional resources for assignment
public class MyLinkedList<E> {
private Node<E> head, tail;
public MyLinkedList ()
head = null;
}
}
/** Return the head element in the list */
public E getFirst() {
if (head == null) {
return null;
}
}
}
/** Return the last element in the list */
public E getLast () {
if (head==null) {
}
}
else {
}
}
/** Add an element to the beginning of the list */
public void prepend (E e) {
Node<E> newNode
}
}
}
else {
}
/** Add an element to the end of the list */
public void append (E item) {
Node<E> newNode = new Node<> (item); // Create a new for element
if (head == null) {
head = tail = newNode; // The new node is the only node in list
}
tail = null;
return head.element;
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
the new node is the only node in list
}
return null;
if (tail == null)
tail = head;
/** Remove the head node and
return the object that is contained in the removed node.
public E removeFirst () {
if (head == null) {
return null;
return tail.element;
}
else {
tail.next = newNode; // Link the new with the last node
tail = newNode; // tail now points to the last node
}
{
}
else {
E temp = head.element;
}
public boolean delete (E item)
head = head.next;
if (head == null) {
tail = null;
}
return temp;
}
Node<E> ptr = head;
Node<E> prvptr = null;
while (ptr! = null && ( (Comparable) ptr.element) .compareTo (item) != 0)
{
}
}
if (ptr == null) //item not found
return false;
if (ptr=-head) // item is first element
head- head.next;
general case
else
public String tostring() {
string result = " [";
prvptr=ptr;
ptr=ptr.next;
if (ptr==tail) //
tail-prvPtr;
prvPtr.next-ptr.next;
last element
return true;
Node<E> ptr = head;
for (ptr= head; ptr !=null; ptr ptr.next)
{
result += "]";
return result;
public void clear () {
head = tail = null;
result = result + ptr.element.tostring();
if (ptr.next != null)
result = result + ","; // add commas but not to the final 1
Insert the closing ] in the string
private static class Node <E> {
E element;
Node<E> next;
public Node (E element) {
this.element = element;
next = null;
// end myLinkedList class
Transcribed Image Text:Write a getMatching() method for the MyLinked List class. The method receives a linked list as parameter and must return a linked list with all elements that are present in both lists. Example calling list:[5,2,7,3,1,4] parameter list: [8,9,2,1,4,2] returned list: [2,1,4]. Note the uniqueness of items in the returned list. Write a test program to test the method thoroughly. Additional resources for assignment public class MyLinkedList<E> { private Node<E> head, tail; public MyLinkedList () head = null; } } /** Return the head element in the list */ public E getFirst() { if (head == null) { return null; } } } /** Return the last element in the list */ public E getLast () { if (head==null) { } } else { } } /** Add an element to the beginning of the list */ public void prepend (E e) { Node<E> newNode } } } else { } /** Add an element to the end of the list */ public void append (E item) { Node<E> newNode = new Node<> (item); // Create a new for element if (head == null) { head = tail = newNode; // The new node is the only node in list } tail = null; return head.element; 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 the new node is the only node in list } return null; if (tail == null) tail = head; /** Remove the head node and return the object that is contained in the removed node. public E removeFirst () { if (head == null) { return null; return tail.element; } else { tail.next = newNode; // Link the new with the last node tail = newNode; // tail now points to the last node } { } else { E temp = head.element; } public boolean delete (E item) head = head.next; if (head == null) { tail = null; } return temp; } Node<E> ptr = head; Node<E> prvptr = null; while (ptr! = null && ( (Comparable) ptr.element) .compareTo (item) != 0) { } } if (ptr == null) //item not found return false; if (ptr=-head) // item is first element head- head.next; general case else public String tostring() { string result = " ["; prvptr=ptr; ptr=ptr.next; if (ptr==tail) // tail-prvPtr; prvPtr.next-ptr.next; last element return true; Node<E> ptr = head; for (ptr= head; ptr !=null; ptr ptr.next) { result += "]"; return result; public void clear () { head = tail = null; result = result + ptr.element.tostring(); if (ptr.next != null) result = result + ","; // add commas but not to the final 1 Insert the closing ] in the string private static class Node <E> { E element; Node<E> next; public Node (E element) { this.element = element; next = null; // end myLinkedList class
Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Threads in linked list
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education