Question

In: Computer Science

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 adds a new item at the provided position. For example, suppose that SinglyLikedList is 5->10->3->15, add(22, 4), the resulting list should be 5->10->3->15->22.

2. Write the method reverseList() for the SinglyLikedList that returns the items in a reverse order. For example, given 5->10->3->15->22, the resulting list should be 22->15->3->10->5.

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

Note: I have changed the class name from SinglyLikedList to SinglyLinkedList. You may use whichever name you want. But make sure to update it everywhere.

//SinglyLinkedList.java (name corrected)

public class SinglyLinkedList {

      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);

      }

      // method to add item to index=position

      public void add(int item, int position) {

            // if position is negative, exiting method

            if (position < 0) {

                  return;

            }

            // if posi8tion is 0, adding new node before current first

            if (position == 0) {

                  first = new Node(item, first);

                  return;

            }

            // else taking a reference to first

            Node temp = first;

            // advancing it position-1 times. or until temp is null, whichever comes

            // first

            for (int i = 0; i < position - 1 && temp != null; i++) {

                  temp = temp.next;

            }

            // if temp is null, that means position is invalid, in which case we

            // simply exit the method

            if (temp == null) {

                  return;

            }

            // else adding new node between temp and temp.next

            temp.next = new Node(item, temp.next);

      }

      // method to return a reversed list

      public SinglyLinkedList reverseList() {

            // creating a SinglyLinkedList

            SinglyLinkedList result = new SinglyLinkedList();

            // looping through each node in this list

            for (Node n = first; n != null; n = n.next) {

                  // adding data of this node before the current first element of the

                  // result list

                  result.first = new Node(n.item, result.first);

            }

            // returning reversed list

            return result;

      }

}


Related Solutions

public class MyLinked {    static class Node {        public Node (double item, Node...
public class MyLinked {    static class Node {        public Node (double item, Node next) { this.item = item; this.next = next; }        public double item;        public Node next;    }    int N;    Node first;     // remove all occurrences of item from the list    public void remove (double item) {        // TODO    } Write the remove function. Do NOT add any fields to the node/list classes, do...
Use this implementation of Integer node, public class IntegerNode { public int item; public IntegerNode next;...
Use this implementation of Integer node, public class IntegerNode { public int item; public IntegerNode next; public IntegerNode(int newItem) { item = newItem; next = null; } // end constructor public IntegerNode(int newItem, IntegerNode nextNode) { item = newItem; next = nextNode; } // end constructor } // end class IntegerNode You need to implement add( ), delete( ), traverse( ) methods for an ordered linked list. And after insertion and deletion, your linked list will remain ordered. Your code...
Using the textbook implementation of integer node given below; public class IntegerNode { public int item;...
Using the textbook implementation of integer node given below; public class IntegerNode { public int item; public IntegerNode next; public IntegerNode(int newItem) { item = newItem; next = null; } // end constructor public IntegerNode(int newItem, IntegerNode nextNode) { item = newItem; next = nextNode; } // end constructor } // end class IntegerNode You need to implement add( ), delete( ), traverse( ) methods for an ordered linked list. And after insertion and deletion, your linked list will remain...
Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int...
Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int size; public CircularLinkedList() { tail= null; size = 0; } public int size(){ return size; } public boolean isEmpty() { return size==0; } //if list is not empty return the first element public E first() { if (isEmpty()) return null; //code here return 0; } //if list not empty return last element public E last() { if (isEmpty()) return null; return tail.getElement(); } /*...
public class Clock { private int hr; private int min; private int sec; public Clock() {...
public class Clock { private int hr; private int min; private int sec; public Clock() { setTime(0, 0, 0); } public Clock(int hours, int minutes, int seconds) { setTime(hours, minutes, seconds); } public void setTime(int hours, int minutes, int seconds) { if (0 <= hours && hours < 24) hr = hours; else hr = 0; if (0 <= minutes && minutes < 60) min = minutes; else min = 0; if(0 <= seconds && seconds < 60) sec =...
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...
public class Node<T> { Public T Item { get; set; } Public Node<T> Next; { get;...
public class Node<T> { Public T Item { get; set; } Public Node<T> Next; { get; set; } public Node (T item, Node<T> next) { … } } public class Polynomial { // A reference to the first node of a singly linked list private Node<Term> front; // Creates the polynomial 0 public Polynomial ( ) { } // Inserts term t into the current polynomial in its proper order // If a term with the same exponent already exists...
class nodeType                    // class used to implement a node { public:         int data;   &n
class nodeType                    // class used to implement a node { public:         int data;                        // data member of node         nodeType * next;        // pointer member of node }; int main() {         int x;         nodeType * head =________ ;                     // initialize head pointer         nodeType * tail = _______ ;                        // initialize tail pointer _______ * p;                                                 // create an auxiliary pointer to a node         for (x = 0; x < 10; ++x)         {                 p =   _________ nodeType; // create a node ___________ = x + 10;                                // store...
public class IntNode               {            private int data;            pri
public class IntNode               {            private int data;            private IntNode link;            public IntNode(int data, IntNode link){.. }            public int     getData( )          {.. }            public IntNode getLink( )           {.. }            public void    setData(int data)     {.. }            public void    setLink(IntNode link) {.. }         } All questions are based on the above class, and the following declaration.   // Declare an empty, singly linked list with a head and a tail reference. // you need to make sure that head always points to...
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int...
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int stNo,String name) {         student_Number=stNo;         student_Name=name;      }     public String getName() {       return student_Name;     }      public int getNumber() {       return student_Number;      }     public void setName(String st_name) {       student_Name = st_name;     } } Write a Tester class named StudentTester which contains the following instruction: Use the contractor to create a student object where student_Number =12567, student_Name = “Ali”. Use the setName method to change the name of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT