Question

In: Computer Science

public class StringNode { private String item; private StringNode next; } public class StringLL { private...

public class StringNode {

      private String item;
      private StringNode next;
}


public class StringLL {

      private StringNode head;
      private int size;

      public StringLL(){
            head = null;
            size = 0;
      }

      public void add(String s){
           add(size,s);
      }

      public boolean add(int index, String s){
        ...
     }

      public String remove(int index){
        ...
      }
}

In the above code add(int index, String s) creates a StringNode and adds it to the linked list at position index, and remove(int index) removes the StringNode at position index.

Which cases does add(index,s) need to consider? and

Complete the next code fragment in remove(int index) that locates the predecessor of the node to be removed.

            // locate predecessor
            curr = head;
            for(

                                                              ){

                  curr=curr.getNext();

            }

Solutions

Expert Solution

Java code for above problem

public class StringNode
{
   private String item;
    private StringNode next;
    public StringNode(String item)
    {
       this.item=item;
       this.next=null;
    }
    public String getItem()
    {
       return this.item;
    }
    public void setItem(String data)
    {
       this.item=item;
    }
    public StringNode getNext()
    {
       return this.next;
    }
    public void setNext(StringNode next)
    {
       this.next=next;
    }
}

public class StringLL
{
   private StringNode head;
    private int size;

    public StringLL()
    {
          head = null;
          size = 0;
    }
    public void add(String s)
    {
         add(size,s);
    }
    public boolean add(int index, String s)
    {
       StringNode new_node=new StringNode(s);
       StringNode node=this.head;
       StringNode prev=null;
       while(node!=null && index>0)
       {
           prev=node;
           node=node.getNext();
           index--;
       }
       if(prev==null)
       {
           new_node.setNext(this.head);
           this.head=new_node;
       }
       else
       {
           new_node.setNext(prev.getNext());
           prev.setNext(new_node);
       }
       size++;
       return true;
    }

    public String remove(int index)
    {
       StringNode node=this.head;
       StringNode prev=null;
       while(node!=null && index>0)
       {
           prev=node;
           node=node.getNext();
           index--;
       }
       if(node==null)
       {
           return "";
       }
       String result="";
       if(prev==null)
       {
           result=this.head.getItem();
           this.head=this.head.getNext();
       }  
       else
       {
           result=node.getItem();
           prev.setNext(node.getNext());
       }
       return result;
    }
}

Mention in comments if any mistakes or errors are found, Thank you.


Related Solutions

public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
java programing Q: Given the following class: public class Student { private String firstName; private String...
java programing Q: Given the following class: public class Student { private String firstName; private String lastName; private int age; private University university; public Student(String firstName, String lastName, int age, University university) { this.firstName = fisrtName; this.lastName = lastName; this.age = age; this.university = university; } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public University getUniversity(){ return university; } public String toString() { return "\nFirst name:" + firstName +...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity;...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity; public Room() { this.capacity = 0; } /** * Constructor for objects of class Room * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Room(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room number. * * @param rN a new room number...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity;...
Room.java: public class Room { // fields private String roomNumber; private String buildingName; private int capacity; public Room() { this.capacity = 0; } /** * Constructor for objects of class Room * * @param rN the room number * @param bN the building name * @param c the room capacity */ public Room(String rN, String bN, int c) { setRoomNumber(rN); setBuildingName(bN); setCapacity(c); }    /** * Mutator method (setter) for room number. * * @param rN a new room number...
Please add comments to this code! Item Class: import java.text.NumberFormat; public class Item {    private...
Please add comments to this code! Item Class: import java.text.NumberFormat; public class Item {    private String name;    private double price;    private int bulkQuantity;    private double bulkPrice;    /***    *    * @param name    * @param price    * @param bulkQuantity    * @param bulkPrice    */    public Item(String name, double price, int bulkQuantity, double bulkPrice) {        this.name = name;        this.price = price;        this.bulkQuantity = bulkQuantity;        this.bulkPrice = bulkPrice;   ...
package mac286.LinkedLists; public class Postfix { private rStack<String> S; private String inSt; private ourLinkedList<String> inList, outList;...
package mac286.LinkedLists; public class Postfix { private rStack<String> S; private String inSt; private ourLinkedList<String> inList, outList; public Postfix(String s) { S = new rStack<String>(); inSt = s; outList = new ourLinkedList<String>(); inList = new ourLinkedList<String>(); } public void reset(String s) { S = new rStack<String>(); inSt = s; outList = new ourLinkedList<String>(); inList = new ourLinkedList<String>(); } private boolean isOperator(char c) { if(c=='-'||c=='+'||c=='*'||c=='/') return true; return false; } private boolean isParenthesis(char c) { if(c == '(' || c==')') return true;...
public class Graph { private ST<String, SET<String>> st; public Graph() { st = new ST<String, SET<String>>();...
public class Graph { private ST<String, SET<String>> st; public Graph() { st = new ST<String, SET<String>>(); } public void addEdge(String v, String w) { // Put v in w's SET and w in v's SET. if (!st.contains(v)) st.put(v, new SET<String>()); if (!st.contains(w)) st.put(w, new SET<String>()); st.get(v).add(w); st.get(w).add(v); } public Iterable<String> adjacentTo(String v) { return st.get(v); } public Iterable<String> vertices() { return st.keys(); } // See Exercises 4.5.1-4 for V(), E(), degree(), // hasVertex(), and hasEdge(). public static void main(String[] args)...
package compstore; public class Desktop{ private String brand; private float price; public Desktop(String brand, float price)...
package compstore; public class Desktop{ private String brand; private float price; public Desktop(String brand, float price) { this.brand = brand; this.price = price; } public String getBrand() { return brand; } public float getPrice() { return price; } } package compstore; public class DeskTopDeals{ // assume proper variables and other methods are here public void dealOfTheDay(Desktop[] items, int numItems){ /**************************** * your code would go here * ****************************/ } } Given the above Desktop class, write code that should go...
package construction; public class Bid{ private String contractor; private float price; public Bid(String contractor, float price)...
package construction; public class Bid{ private String contractor; private float price; public Bid(String contractor, float price) { this.contractor = contractor; this.price = price; } public String getContractor() { return contractor; } public float getPrice() { return price; } } package construction; public class ContractorBids{ // assume proper variables and other methods are here public void winningBid(Bid[] bids, int numBids){ /**************************** * your code would go here * ****************************/ } } You are doing renovations on your building, and multiple contractors...
public class ArraySkills { public static void main(String[] args) { // *********************** // For each item...
public class ArraySkills { public static void main(String[] args) { // *********************** // For each item below you must code the solution. You may not use any of the // methods found in the Arrays class or the Collections classes // You must use Java's built-in Arrays. You are welcome to use the Math and/or Random class if necessary. // You MUST put your code directly beneath the comment for each item indicated. String[] myData; // 1. Instantiate the given...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT