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 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...
JAVA DATA STRUCTURE (Linked Lists/Queue) public class Node {    int value;    Node nextNode;   ...
JAVA DATA STRUCTURE (Linked Lists/Queue) public class Node {    int value;    Node nextNode;    Node(int v, Node n){        value = v;        nextNode = n;    }    Node (int v){        this(v,null);    } } public class Stack {    protected Node top;    Stack(){        top = null;    }    boolean isEmpty(){        return( top == null);    }    void push(int v){        Node tempPointer;       ...
public class Date { private int dMonth; //variable to store the month private int dDay; //variable...
public class Date { private int dMonth; //variable to store the month private int dDay; //variable to store the day private int dYear; //variable to store the year //Default constructor //Data members dMonth, dDay, and dYear are set to //the default values //Postcondition: dMonth = 1; dDay = 1; dYear = 1900; public Date() { dMonth = 1; dDay = 1; dYear = 1900; } //Constructor to set the date //Data members dMonth, dDay, and dYear are set //according to...
package applications; public class Matrix { private int[][] m; public Matrix(int x, int y) { m...
package applications; public class Matrix { private int[][] m; public Matrix(int x, int y) { m = new int[x][y]; } public Matrix(int x, int y, int z) { m = new int[x][y]; for(int i = 0; i < x; i++) { for(int j = 0; j < y; j++) { m[i][j] = z; } } } public int rowsum(int i) throws IndexOutOfBoundsException { if (i < 0 || i > m.length-1) { throw new IndexOutOfBoundsException("Invalid Row"); } int sum =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT