Answered step by step
Verified Expert Solution
Link Copied!

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. image text in transcribed
image text in transcribed
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
}
}
CSC 205 Lab 10 : Linked Lists Goals After completing this lab, you should be able to: Understand object references and the advantages of a dynamic linked list over an array. Be able to display and count the contents of a linked list Insert a node into a specified position of a linked list. Lab Startup Change into your Labs directory, and let's create and change into a Lab10 directory. Now, let's copy over some files by typing: cp /pub/digh/CSC205/Lab10/ Building and Tracing a Simple Linked List Take a look at the Links.java program in your attached handout. Draw the linked list that would be created by the ma in method. Now, compile and run the Links program and check to see if your list prints out in the manner that you would expect given a head pointer to the front of your list. Writing Simple Methods for Processing Linked Lists Add a recursive method named count to your Links program that can be used to count and return the number of nodes pointed to by a head pointer. Your method will have one parameter, the pointer head. Add an iterative method named findMax to your Links program that returns the largest value in a linked list pointed to by a head pointer. Your method will have one parameter, the pointer head. You should not be concerned about the data type of the elements stored in your nodes. Use the Comparable interface appropriately in your method Include method calls in ma in to test both of your new methods. CSC 205 Lab 10 : Linked Lists Goals After completing this lab, you should be able to: Understand object references and the advantages of a dynamic linked list over an array. Be able to display and count the contents of a linked list Insert a node into a specified position of a linked list. Lab Startup Change into your Labs directory, and let's create and change into a Lab10 directory. Now, let's copy over some files by typing: cp /pub/digh/CSC205/Lab10/ Building and Tracing a Simple Linked List Take a look at the Links.java program in your attached handout. Draw the linked list that would be created by the ma in method. Now, compile and run the Links program and check to see if your list prints out in the manner that you would expect given a head pointer to the front of your list. Writing Simple Methods for Processing Linked Lists Add a recursive method named count to your Links program that can be used to count and return the number of nodes pointed to by a head pointer. Your method will have one parameter, the pointer head. Add an iterative method named findMax to your Links program that returns the largest value in a linked list pointed to by a head pointer. Your method will have one parameter, the pointer head. You should not be concerned about the data type of the elements stored in your nodes. Use the Comparable interface appropriately in your method Include method calls in ma in to test both of your new methods

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Principles Programming And Performance

Authors: Patrick O'Neil

1st Edition

1558603921, 978-1558603929

Students also viewed these Databases questions

Question

b. Does senior management trust the team?

Answered: 1 week ago

Question

How many Tables Will Base HCMSs typically have? Why?

Answered: 1 week ago

Question

What is the process of normalization?

Answered: 1 week ago