Question

In: Computer Science

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 the head node and tail always points to the tail node.
         IntNode head, tail;
         head = tail = null;    

// 1.) Insert a node with data 2 into this empty list

// 2.) Insert a node with data 4 at the end of the list.

// 3.) Insert a node with data 3 between the first and the last node.

// 4.) Insert a node with data 1 before the first node.

// 5.) delete the node with data 2

// 6.) delete the node with data 1

Solutions

Expert Solution

Program

public class IntNode
{
   private int data;
    private IntNode link;
public IntNode(int data, IntNode link){
   this.data = data;
   this.link = link;
}
public int getData(){
   return data;
}
public IntNode getLink(){
   return link;
}
public void setData(int data){
   this.data = data;
}
public void setLink(IntNode link){
   this.link = link;
}
}

class SLinkedList{
   private IntNode head;
   private IntNode tail;
  
   //constructor
   public SLinkedList(){
       head = null;
       tail = null;
   }
  
   //method to insert an item to beginning of the list
   public void insertAtBegin(int item)
   {
       IntNode newnode = new IntNode(item, head);
       if(head==null)
       {
           head = tail = newnode;
       }
       else
       {
           head = newnode;
       }
   }
  
   //method to insert an item to end of the list
   public void insertAtEnd(int item)
   {
       IntNode newnode = new IntNode(item, null);
       if(tail==null)
       {
           head = tail = newnode;
       }
       else
       {
           tail.setLink(newnode);
           tail = newnode;
       }
   }
  
   //method to insert an item to the list
   public void insert(int item)
   {
       IntNode newnode = new IntNode(item, null);
      
       if(head==null)
       {
           head = tail = newnode;
       }
       else
       {
           IntNode temp = head;
           IntNode pre = null;
          
           while(temp!= null && temp.getData()<item)
           {
               pre = temp;
               temp = temp.getLink();
           }
          
           pre.setLink(newnode);
           newnode.setLink(temp);
       }
   }
  
   //method to delete an item from the list
   public void delete(int item)
   {
       if(head==null){
           System.out.println ("List is empty");
           return;
       }
  
       IntNode temp = head;
       IntNode prev = null;
      
       if(temp.getData()==item)
       {
           head = temp.getLink();
           System.out.println (item + " has been deleted");
           return;
       }
      
       while(temp!= null && temp.getData()!=item)
       {
           prev = temp;
           temp = temp.getLink();
       }
      
       if(temp==null)
       {
           System.out.println (item + " was not found");
           return;
       }
      
       prev.setLink(temp.getLink());
       System.out.println (item + " has been deleted");
   }
  
   //method to print the list
   public void printList()
   {
       if(head==null)
       {
           System.out.println ("List is empty");
           return;
       }
      
       System.out.print("List: ");
       IntNode temp = head;
      
       while(temp!= null)
       {
           System.out.print (temp.getData() + " ");
           temp = temp.getLink();
       }
       System.out.println ();
   }
}

class LinkedListDemo
{
   //main method
   public static void main (String[] args)
   {
       //create object of SLinkedList
       SLinkedList list = new SLinkedList();
      
       // 1.) Insert a node with data 2 into this empty list
       list.insert(2);
       System.out.println ("After insert 2: ");
       list.printList();
       // 2.) Insert a node with data 4 at the end of the list.
       list.insertAtEnd(4);
       System.out.println ("After insert 4: ");
       list.printList();
       // 3.) Insert a node with data 3 between the first and the last node.
       list.insert(3);
       System.out.println ("After insert 3: ");
       list.printList();
       // 4.) Insert a node with data 1 before the first node.
       list.insertAtBegin(1);
       System.out.println ("After insert 1: ");
       list.printList();
       // 5.) delete the node with data 2
       list.delete(2);
       System.out.println ("After delete 2: ");
       list.printList();
       // 6.) delete the node with data 1
       list.delete(1);
       System.out.println ("After delete 1: ");
       list.printList();
   }
}

Output:

After insert 2:
List: 2
After insert 4:
List: 2 4
After insert 3:
List: 2 3 4
After insert 1:
List: 1 2 3 4
2 has been deleted
After delete 2:
List: 1 3 4
1 has been deleted
After delete 1:
List: 3 4

Solving your question and helping you to well understand it is my focus. So if you face any difficulties regarding this please let me know through the comments. I will try my best to assist you.
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...
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 =...
class A { public: //constructors // other members private: int a; int b; }; Give declatations...
class A { public: //constructors // other members private: int a; int b; }; Give declatations of operator functions for each of the following ways to overload operator + You must state where the declatation goes, whether within the class in the public or private section or outside the class. The operator + may be overloaded. a) as friend function b) as member function c) as non-friend, non-member function
public class ProductThread { static class ProductThreads extends Thread{ private int begin, end; int[] v1, v2;...
public class ProductThread { static class ProductThreads extends Thread{ private int begin, end; int[] v1, v2; long ris; public ProductThreads(String name, int [] v1, int [] v2, int begin, int end) { setName(name); this.v1 = v1; this.v2 = v2; this.begin = begin; this.end = end; this.ris = 0; } public void run() { System.out.println("Thread " + Thread.currentThread().getName() + "[" + begin + "," + end + "] started"); ris = 1; for(int i = begin; i <= end; i++) ris...
import javax.swing.JOptionPane; public class Animal {    private int numTeeth = 0;    private boolean spots...
import javax.swing.JOptionPane; public class Animal {    private int numTeeth = 0;    private boolean spots = false;    public int weight = 0;       public Animal(int numTeeth, boolean spots, int weight){        this.numTeeth =numTeeth;        this.spots = spots;        this.weight =weight;    }       public int getNumTeeth(){        return numTeeth;    }    public void setNumTeeth(int numTeeth) {        this.numTeeth = numTeeth;    }       public boolean getSpots() {       ...
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...
With the code that is being tested is: import java.util.Random; public class GVdate { private int...
With the code that is being tested is: import java.util.Random; public class GVdate { private int month; private int day; private int year; private final int MONTH = 1; private final int DAY = 9; private static Random rand = new Random(); /** * Constructor for objects of class GVDate */ public GVdate() { this.month = rand.nextInt ( MONTH) + 1; this.day = rand.nextInt ( DAY );    } public int getMonth() {return this.month; } public int getDay() {return this.day;...
public class SumMinMaxArgs { private int[] array; // You will need to write the following: //...
public class SumMinMaxArgs { private int[] array; // You will need to write the following: // // 1. A constructor that takes a reference to an array, // and initializes an instance variable with this // array reference // // 2. An instance method named sum, which will calculate // the sum of the elements in the array. If the array // is empty (contains no elements), then this will // will return 0. You will need a loop for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT