I NEED THIS CODE TO COMPILE AND PASS THE TWO DRIVERS BELOW. PLEASE USE COMMENTS
ID: 3539378 • Letter: I
Question
I NEED THIS CODE TO COMPILE AND PASS THE TWO DRIVERS BELOW. PLEASE USE COMMENTS // TO EXPLAIN YOUR CODE.
For this homework, all methods will go in a class called LLMethods. As
always, your program must compile and be submitted on time to be eligible for credit. Additionally, you will need the following classes:
.
You will write four methods for this homework. The first method has the
following header:
This method will return true if list contains num. Implement the method recursively. For example, iflinky, an object of type LinkedList contains 7, 3, 10 in that order, then a call find(linky, 5) will returnfalse. On the other hand, find(linky, 7) will return true.
The second part of the homework wants you to implement stacks using linked lists. We can implement stacks using linked lists by making the first linked list object's value the thing on the top of the stack, and the first linked list object's next be the rest of the stack (everything underneath the top of the stack). Then, to push something on to the stack, we create a new linked list object, make it point to the first linked list object, and set the new linked list object's value to the value that should be on the top of the stack. Then we make the new linked list object is the first linked list object. To pop something off the stack, we remember the value on top of the stack and make the second linked list object the first linked list object. To test whether the stack is not empty (or is empty), we check if there is a first linked list object (or if that first linked list object is null). If we implement a stack with linked list, the stack will never be full.
Implement the four methods for stacks:
Two cases must be considered when pushing a number on the stack. if s.firstLinkedList is null, thens.firstLinkedList should be set to a new LinkedList containing num. If s.firstLinkedList is not null, then we want to create a new linked list. we will set the value of this linked list to be num, and set the next of this linked list to s.firstLinkedList.
For the pop method, simply store s.firstLinkedList.value in a tempo- rary variable, set s.firstLinkedListto its next, and return the temorary variable.
The isEmpty method only returns true when s.firstLinkedList is null.
The isFull method should never return false, because we can always add a value on the beginning of a linked list.
public class DriverOne
{
//output (correct)
//true
//false
//8
//4
public static void main(String[] args)
{
Stack stak = new Stack();
LLMethods.push(stak, 4);
LLMethods.push(stak, 8);
System.out.println(LLMethods.find(stak.firstLinkedList, 4));
System.out.println(LLMethods.find(stak.firstLinkedList, 7));
System.out.println(LLMethods.pop(stak));
System.out.println(LLMethods.pop(stak));
}
}
public class DriverTwo
{
//correct output:
//false
//true
//false
//3
//2
//true
//false
public static void main(String[] args)
{
Stack stak = new Stack();
LLMethods.push(stak, 2);
LLMethods.push(stak, 3);
System.out.println(LLMethods.find(stak.firstLinkedList, 9));
System.out.println(LLMethods.find(stak.firstLinkedList, 2));
System.out.println(LLMethods.isFull(stak));
System.out.println(LLMethods.pop(stak));
System.out.println(LLMethods.pop(stak));
System.out.println(LLMethods.isEmpty(stak));
LinkedList empty = null;
System.out.println(LLMethods.find(empty, 5));
}
}
Explanation / Answer
/* MAKE SURE THE CLASSES LinkedList and Stack are available for use when you are compiling this code */
public class LLMethods
{
//recursive find function
public static boolean find(LinkedList list, int num)
{
//if list empty return false
if(list==null)
{ return false; }
//either its the first value, or do find on the rest of the list
if( list.value==num)
{ return true; }
else
{ return find(list.next,num); }
}
//stack functions
//push
public static void push(Stack s, int num)
{
//initialize new list object
LinkedList newVal=new LinkedList();
newVal.value=num;
newVal.next=null;
//if empty make it first value
if(isEmpty(s))
{ s.firstLinkedList = newVal; }
else
{
//else insert at top
// make previous first value follow new value
newVal.next=s.firstLinkedList;
//make new value the first value
s.firstLinkedList = newVal;
}
}
//pop
public static int pop(Stack s)
{
//if stack empty can't pop
if(isEmpty(s))
{ return 0; }
else
{
//else pop first value
int tmp= s.firstLinkedList.value;
//set first value as the second value
s.firstLinkedList = s.firstLinkedList.next;
//return popped value
return tmp;
}
}
//isEmpty
public static boolean isEmpty(Stack s)
{
//true if first value is null
if( s.firstLinkedList==null) return true;
return false;
}
//isFull, always true
public static boolean isFull(Stack s)
{ return true; }
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.