Question: package ds; public class LinkedList { public ListNode head; public LinkedList () { head = null; } /* * Implement the LIST-SEARCH(L, k) function */

package ds;
public class LinkedList { public ListNode head; public LinkedList () { head = null; } /* * Implement the LIST-SEARCH(L, k) function */ public ListNode search (int k) { } /* * Implement the LIST-INSERT(L, x) function * Note that x is a integer value, not a ListNode */ public void insert (int x) { } /* * Implement the LIST-DELETE(L, x) function */ public void delete (ListNode x) { } /* * Convert a LinkedList to a string in the format of [#elements] */ public String toString () { String str; ListNode n; str = "["; n = this.head; while (n != null) { str += n.key + ","; n = n.next; } str += "]"; return str; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub LinkedList l; l = new LinkedList(); for (int i = 0; i
}
package ds;
public class ListNode {
public int key;public ListNode prev;
public ListNode next;
public ListNode () {
prev = next = null;
}
public ListNode (int _key) {
key = _key;
prev = next = null;
}
}
struggling find the output, not running
and heres what i did
package ds;
public class LinkedList { public ListNode head; public LinkedList () { head = null; } /* * Implement the LIST-SEARCH(L, k) function */ public ListNode search (int k) {
ListNode start=head; //if empty if(this.head==null){ return this.head; } while(start!=null){ //if search is success if(start.key==k){ return start; } //goto next node start=start.next; } //if k not found return null; } /* * Implement the LIST-INSERT(L, x) function * Note that x is a integer value, not a ListNode */ public void insert (int x) {
//creates a new node ListNode tempNode=new ListNode(x); //insertion at front tempNode.next=this.head; tempNode.prev=null; //change the previous of 1st node to new node if(this.head!=null){ this.head.prev=tempNode; } //point the head to new node this.head=tempNode; } /* * Implement the LIST-DELETE(L, x) function */ public void delete (ListNode x) {
//creates a new node ListNode tempNode=new ListNode(x); //insertion at front tempNode.next=this.head; tempNode.prev=null; //change the previous of 1st node to new node if(this.head!=null){ this.head.prev=tempNode; } //point the head to new node this.head=tempNode;
}
/* * Convert a LinkedList to a string in the format of [#elements] */ public String toString () { String str; ListNode n; str = "["; n = this.head; while (n != null) { str += n.key + ","; n = n.next; } str += "]"; return str; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub LinkedList l; l = new LinkedList(); for (int i = 0; i
}
Task 3 (40 pts). Implement the search(int k), insert(int i), delete() func- tion in LinkedList.java as discussed in Lecture 6
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
