Question
Other solutions here for these questions were either incorrect or incomplete. Thanks! Implement the search(int k), insert(int x), delete() function in LinkedList.java as discussed in
Other solutions here for these questions were either incorrect or incomplete. Thanks!
Implement the search(int k), insert(int x), delete() function in LinkedList.java as discussed in Lecture 6.
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 < 5; i++) l.insert(i); System.out.println(l.toString()); for (int i = 0; i < 2; i++) l.delete(l.head.next); System.out.println(l.toString()); } }
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