SlideShare a Scribd company logo
Select three methods in the ObjectList class to work through algorithmically. Describe any
special cases or boundary conditions that might exist for each of the three methods selected. Be
sure to draw pictures as you work through the methods. Remember that you cannot work on
linked lists without drawing pictures. The homework will not be accepted without submitting the
pictures! Write a Java method with the signature: public objectList intersect (objectList list1,
objectList list2) that accepts two unordered linear linked lists and returns a third linear linked list
whose nodes contains the intersection of the two original linear linked lists. Note that the
intersection of two lists is a new list containing the elements that the two lists have in common,
without modifying the two original lists. Describe any boundary conditions that might exist.
Solution
Node.java
//Defines each node of a linked list
public class Node{
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
ObjectList.java
public class ObjectList{
public Node head;
/* Utility function to print list */
public void printList()
{
Node temp = head;
while(temp != null)
{
System.out.print(temp.data+" ");
temp = temp.next;
}
System.out.println();
}
/* Inserts a node at start of linked list */
public void push(int new_data)
{
/* 1 & 2: Allocate the Node &
Put in the data*/
Node new_node = new Node(new_data);
/* 3. Make next of new Node as head */
new_node.next = head;
/* 4. Move the head to point to new Node */
head = new_node;
}
/* A utilty function that returns true if data is present
in linked list else return false */
public boolean isPresent (int data){
Node t = this.head;
while (t != null)
{
if (t.data == data)
return true;
t = t.next;
}
return false;
}
}
Intersection.java
//Implements the intersection method.
class Intersection{
static public ObjectList intersect(ObjectList list1, ObjectList list2){
ObjectList result = new ObjectList();
//Node t1 = head1;
Node t1 = list1.head;
Node t2 = list2.head;
// Traverse list1 and search each element of it in list2.
// If the element is present in list 2, then insert the
// element to result
while (t1 != null){
if (list2.isPresent(t1.data))
result.push(t1.data);
t1 = t1.next;
}
return result;
}
/* Driver program to test above functions */
public static void main(String args[]){
ObjectList llist1 = new ObjectList();
ObjectList llist2 = new ObjectList();
ObjectList unin = new ObjectList();
ObjectList intersecn = new ObjectList();
/*create a linked lits 10->15->5->20 */
llist1.push(20);
llist1.push(4);
llist1.push(15);
llist1.push(10);
/*create a linked lits 8->4->2->10 */
llist2.push(10);
llist2.push(2);
llist2.push(4);
llist2.push(8);
intersecn = intersect(llist1, llist2);
intersecn.printList();
}
}

More Related Content

PDF
There are a couple of new methods that you will be writing for this pr.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
PDF
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
PDF
import java-util--- public class MyLinkedList{ public static void.pdf
PDF
File LinkedList.java Defines a doubly-l.pdf
PDF
Given below is the completed implementation of MyLinkedList class. O.pdf
PDF
Implement the interface you wrote for Lab B (EntryWayListInterface)..pdf
There are a couple of new methods that you will be writing for this pr.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
please i need help Im writing a program to test the merge sort alg.pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
import java-util--- public class MyLinkedList{ public static void.pdf
File LinkedList.java Defines a doubly-l.pdf
Given below is the completed implementation of MyLinkedList class. O.pdf
Implement the interface you wrote for Lab B (EntryWayListInterface)..pdf

Similar to Select three methods in the ObjectList class to work through algori.pdf (20)

PDF
For each task, submit your source java code file.(1) Objective Im.pdf
PDF
Implementation The starter code includes List.java. You should not c.pdf
PDF
public class MyLinkedListltE extends ComparableltEgtg.pdf
PDF
I am failing a few tests with my Double Linked List program. Can som.pdf
PDF
-JAVA-provide a test class that do the required -you may add met.pdf
DOCX
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
PDF
using the single linked list code written in the class or in the lab,.pdf
PDF
Hi,I have added the methods and main class as per your requirement.pdf
PDF
Implement the following specification of UnsortedType using circular.pdf
PDF
Note             Given Code modified as required and required met.pdf
PDF
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
PDF
STAGE 2 The Methods 65 points Implement all the methods t.pdf
PDF
Objective The purpose of this exercise is to create a Linke.pdf
PDF
Objective The purpose of this exercise is to create a Linke.pdf
PDF
implement the ListLinked ADT (the declaration is given in ListLinked.pdf
PDF
Lab02kdfshdfgajhdfgajhdfgajhdfgjhadgfasjhdgfjhasdgfjh.pdf
PDF
hi i have to write a java program involving link lists. i have a pro.pdf
PDF
Linked List Objective The purpose of this exercise is to cr.pdf
PDF
package linkedLists- import java-util-Iterator- --- A class representi.pdf
PDF
Problem 1 Create Node class (or use what you have done in Lab4)• .pdf
For each task, submit your source java code file.(1) Objective Im.pdf
Implementation The starter code includes List.java. You should not c.pdf
public class MyLinkedListltE extends ComparableltEgtg.pdf
I am failing a few tests with my Double Linked List program. Can som.pdf
-JAVA-provide a test class that do the required -you may add met.pdf
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
using the single linked list code written in the class or in the lab,.pdf
Hi,I have added the methods and main class as per your requirement.pdf
Implement the following specification of UnsortedType using circular.pdf
Note             Given Code modified as required and required met.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
STAGE 2 The Methods 65 points Implement all the methods t.pdf
Objective The purpose of this exercise is to create a Linke.pdf
Objective The purpose of this exercise is to create a Linke.pdf
implement the ListLinked ADT (the declaration is given in ListLinked.pdf
Lab02kdfshdfgajhdfgajhdfgajhdfgjhadgfasjhdgfjhasdgfjh.pdf
hi i have to write a java program involving link lists. i have a pro.pdf
Linked List Objective The purpose of this exercise is to cr.pdf
package linkedLists- import java-util-Iterator- --- A class representi.pdf
Problem 1 Create Node class (or use what you have done in Lab4)• .pdf

More from aroraopticals15 (20)

PDF
For a given H0 and level of significance, if you reject the H0 for a.pdf
PDF
Find all elements of our ring R that have norm 1. Show that no elemen.pdf
PDF
Evaluate the decision to have a computer usage policy and the potent.pdf
PDF
(java) eclipse PleaseDevelop an application that implements a pro.pdf
PDF
At a sudden contraction in a pipe the diameter changes from D_1 to D_.pdf
PDF
Above is a trace depicting mechanical activity of frog heart. A stud.pdf
PDF
A gymnosperm, such as Juniperus virginiana, that produces female con.pdf
PDF
Write short descriptive answer to the following questions Discuss wh.pdf
PDF
Why should anyone else care about what I do with my sewage on my own .pdf
PDF
Why do viral particles, zymosan on fungi, endotoxin (LPS) from gram .pdf
PDF
Which of the following used historicalcomparative methods in their .pdf
PDF
Which of following is not a class of the phylum platyhelminthes Tre.pdf
PDF
What is wrong with this code Please fix.code#include stdio.h.pdf
PDF
What are two ways that meiosis could produce gametes that contai.pdf
PDF
what is the process in society that made this change to advertising .pdf
PDF
What is the best way to go about designing an Android AppSoluti.pdf
PDF
Use the following word bank to complete the numbers 51-100. Acoeloma.pdf
PDF
Two cards are randomly selected from a 52-card deck. What is the pro.pdf
PDF
Two labs are being compared to determine if they are providing the sa.pdf
PDF
Todopackage hwk6; This class contains the configuration of a t.pdf
For a given H0 and level of significance, if you reject the H0 for a.pdf
Find all elements of our ring R that have norm 1. Show that no elemen.pdf
Evaluate the decision to have a computer usage policy and the potent.pdf
(java) eclipse PleaseDevelop an application that implements a pro.pdf
At a sudden contraction in a pipe the diameter changes from D_1 to D_.pdf
Above is a trace depicting mechanical activity of frog heart. A stud.pdf
A gymnosperm, such as Juniperus virginiana, that produces female con.pdf
Write short descriptive answer to the following questions Discuss wh.pdf
Why should anyone else care about what I do with my sewage on my own .pdf
Why do viral particles, zymosan on fungi, endotoxin (LPS) from gram .pdf
Which of the following used historicalcomparative methods in their .pdf
Which of following is not a class of the phylum platyhelminthes Tre.pdf
What is wrong with this code Please fix.code#include stdio.h.pdf
What are two ways that meiosis could produce gametes that contai.pdf
what is the process in society that made this change to advertising .pdf
What is the best way to go about designing an Android AppSoluti.pdf
Use the following word bank to complete the numbers 51-100. Acoeloma.pdf
Two cards are randomly selected from a 52-card deck. What is the pro.pdf
Two labs are being compared to determine if they are providing the sa.pdf
Todopackage hwk6; This class contains the configuration of a t.pdf

Recently uploaded (20)

PDF
A systematic review of self-coping strategies used by university students to ...
PDF
Classroom Observation Tools for Teachers
PPTX
Lesson notes of climatology university.
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
master seminar digital applications in india
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Complications of Minimal Access Surgery at WLH
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
01-Introduction-to-Information-Management.pdf
PDF
Computing-Curriculum for Schools in Ghana
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
VCE English Exam - Section C Student Revision Booklet
A systematic review of self-coping strategies used by university students to ...
Classroom Observation Tools for Teachers
Lesson notes of climatology university.
STATICS OF THE RIGID BODIES Hibbelers.pdf
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
master seminar digital applications in india
Microbial diseases, their pathogenesis and prophylaxis
Complications of Minimal Access Surgery at WLH
Chinmaya Tiranga quiz Grand Finale.pdf
Cell Types and Its function , kingdom of life
2.FourierTransform-ShortQuestionswithAnswers.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
01-Introduction-to-Information-Management.pdf
Computing-Curriculum for Schools in Ghana
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Supply Chain Operations Speaking Notes -ICLT Program
VCE English Exam - Section C Student Revision Booklet

Select three methods in the ObjectList class to work through algori.pdf

  • 1. Select three methods in the ObjectList class to work through algorithmically. Describe any special cases or boundary conditions that might exist for each of the three methods selected. Be sure to draw pictures as you work through the methods. Remember that you cannot work on linked lists without drawing pictures. The homework will not be accepted without submitting the pictures! Write a Java method with the signature: public objectList intersect (objectList list1, objectList list2) that accepts two unordered linear linked lists and returns a third linear linked list whose nodes contains the intersection of the two original linear linked lists. Note that the intersection of two lists is a new list containing the elements that the two lists have in common, without modifying the two original lists. Describe any boundary conditions that might exist. Solution Node.java //Defines each node of a linked list public class Node{ int data; Node next; Node(int d) { data = d; next = null; } } ObjectList.java public class ObjectList{ public Node head; /* Utility function to print list */ public void printList() { Node temp = head; while(temp != null) { System.out.print(temp.data+" "); temp = temp.next; } System.out.println();
  • 2. } /* Inserts a node at start of linked list */ public void push(int new_data) { /* 1 & 2: Allocate the Node & Put in the data*/ Node new_node = new Node(new_data); /* 3. Make next of new Node as head */ new_node.next = head; /* 4. Move the head to point to new Node */ head = new_node; } /* A utilty function that returns true if data is present in linked list else return false */ public boolean isPresent (int data){ Node t = this.head; while (t != null) { if (t.data == data) return true; t = t.next; } return false; } } Intersection.java //Implements the intersection method. class Intersection{ static public ObjectList intersect(ObjectList list1, ObjectList list2){ ObjectList result = new ObjectList(); //Node t1 = head1; Node t1 = list1.head; Node t2 = list2.head; // Traverse list1 and search each element of it in list2.
  • 3. // If the element is present in list 2, then insert the // element to result while (t1 != null){ if (list2.isPresent(t1.data)) result.push(t1.data); t1 = t1.next; } return result; } /* Driver program to test above functions */ public static void main(String args[]){ ObjectList llist1 = new ObjectList(); ObjectList llist2 = new ObjectList(); ObjectList unin = new ObjectList(); ObjectList intersecn = new ObjectList(); /*create a linked lits 10->15->5->20 */ llist1.push(20); llist1.push(4); llist1.push(15); llist1.push(10); /*create a linked lits 8->4->2->10 */ llist2.push(10); llist2.push(2); llist2.push(4); llist2.push(8); intersecn = intersect(llist1, llist2); intersecn.printList(); } }