Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Hi, this is for programming 2 with the java language. I missed lab class so I am a good bit lost. I could really use
Hi, this is for programming 2 with the java language.
I missed lab class so I am a good bit lost. I could really use help understanding this. In this assignment we are dealing with linked lists. Overall we are to modify only two codes so I believe. Those codes are links.java and search.java. The other programs we are supposed to write out what we expect will happen. Im not sure what Links3.java is for since I did not see it mentioned in the assignment sheet below. I have included copyable codes from each program. Please help.
Below are programs from the the assignment. Includes:
Links program (needs to be modified)
Links2 program
Links3 program
Nodes program (is a constructor program)
Search program (needs to be modified)
///////////////////////////////////
//Links.java program
public class Links
{
public static void main(String[] args)
{
Node pos1 = null;
Node pos2 = null;
pos1 = new Node(new Integer(13));
pos1.setNext(new Node(new Integer(15), null));
pos2 = new Node(new Integer(11), null);
pos2.setNext(pos1);
printList(pos2);
}
private static void printList(Node head)
{
if (head != null)
{
System.out.println(head.getItem());
printList(head.getNext());
}
}
}
///////////////////////////////////////////////
//Links2.java program
import java.io.*;
public class Links2 {
public static void main(String args[])
{
Node head = null;
head = insert(head, new Integer(13));
head = insert(head, new Integer(-1));
head = insert(head, new Integer(0));
head = insert(head, new Integer(50));
printList(head);
}
private static Node insert(Node head, Comparable newValue)
{
Node prev, curr = head;
for (prev = null, curr = head;
curr != null && newValue.compareTo(curr.getItem()) > 0;
prev = curr, curr = curr.getNext() ) {}
Node newNode = new Node(newValue, curr);
if (prev != null)
{
prev.setNext(newNode);
return head;
}
else
return newNode;
}
private static void printList(Node head)
{
if (head != null)
{
System.out.println(head.getItem());
printList(head.getNext());
}
}
}
//////////////////////////////////////
//Links3.java program
import java.io.*;
public class Links3 {
public static void main(String args[])
{
Node head = null;
head = insert(head, new Integer(3));
head = insert(head, new Integer(1));
head = insert(head, new Integer(0));
head = insert(head, new Integer(-1));
printList(head);
}
private static Node insert(Node head, Comparable newValue)
{
Node prev, curr = head;
for (prev = null, curr = head;
curr != null && newValue.compareTo(curr.getItem()) > 0;
prev = curr, curr = curr.getNext() ) {}
Node newNode = new Node(newValue, curr);
if (prev != null)
{
prev.setNext(newNode);
return head;
}
else
return newNode;
}
private static void printList(Node head)
{
if (head != null)
{
System.out.println(head.getItem());
printList(head.getNext());
}
}
}
///////////////////////////////////////////
//Node.java constructor program
public class Node {
private Object item;
private Node next;
public Node(Object newItem) {
item = newItem;
next = null;
} // end constructor
public Node(Object newItem, Node nextNode) {
item = newItem;
next = nextNode;
} // end constructor
public void setItem(Object newItem) {
item = newItem;
} // end setItem
public Object getItem() {
return item;
} // end getItem
public void setNext(Node nextNode) {
next = nextNode;
} // end setNext
public Node getNext() {
return next;
} // end getNext
} // end class Node
//////////////////////////////////////////////
//Search.java program
import java.io.*;
import java.util.*;
public class Search {
public static void main(String argv[]) throws IOException {
Scanner stdin = new Scanner(System.in);
System.out.println("Please input 0 or more values at keyboard");
Node head = buildList();
System.out.println("Now printing list");
printList(head);
System.out.println(" What key in list are you searching for? ");
int key = stdin.nextInt();
System.out.print("Your key was ");
if (search(head, key))
System.out.println("found.");
else
System.out.println("not found.");
}
private static void printList(Node head)
{
if (head != null)
{
System.out.print(head.getItem() + " ");
printList(head.getNext());
}
}
private static Node buildList() throws IOException
{
Scanner in = new Scanner(System.in);
Node head;
if(in.hasNext())
{
head = new Node(new Integer(in.nextInt()),null);
}
else
{
head = new Node(null,null);
return head;
}
while(in.hasNext())
head = insert(head,new Integer(in.nextInt()));
return head;
}
private static Node insert(Node head, Comparable newValue)
{
Node prev, curr = head;
for (prev = null, curr = head;
curr != null && newValue.compareTo(curr.getItem()) > 0;
prev = curr, curr = curr.getNext() ) {}
Node newNode = new Node(newValue, curr);
if (prev != null)
{
prev.setNext(newNode);
return head;
}
else
return newNode;
}
private static boolean search(Node head, Comparable key)
{
// PRE: head points to the front of linked list; list may be
// empty or non-empty; key is item searching for
// POST: returns true or false regarding whether key is found in
// list
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started