Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objectives: After finishing the lab5 part 2, you can: -Understand how to access

ID: 3801177 • Letter: O

Question

Objectives: After finishing the lab5 part 2, you can:

-Understand how to access the nodes in the singly-linked list structure when applying 4 operations: insert, delete, fetch and update

-Apply Iterator to Linked list to traverse the structures.

-Learn how to use the Java LinkedList Requirement Statement: Create the project SP2017_LAB5 then write the application named SP2017_LinkedListDemo_yourLastName that allows users to select one of the following tasks of Linked list to manage the list of Cars or Motocycles

1. Singly Linked List

2. Singly Linked List with Iterator

3. Java Linked List with Iterator

After finishing with one type of Linked List, allow users to select another type until users want to exit For the Singly Linked List

-INSERT: Allow users to insert cars or motocycles until users want to stop.For each car or motocycle, ask users to enter information that needs to create an object then insert it to the Singly Linked List. When users stop inserting the car or motocycles, display the menu of the following tasks to allow users select a task. After finishing one task, re-display the menu to allow users to continue until they want to stop

-FETCH: -Ask for a vin of the car or motocycle that users want to fetch -Display the car or motocyle based on the request

-UPDATE: -Ask users for a vin of the car or motocycle that users want to update -Display the car or motocycle that users ask for -Ask users for new color to update -set new color, update the node to the Singly Linked list and display the message update success or not

-DELETE -Ask for a vin of the car or motocycle that users want to delete -Delete and display the message delete success or not

-SHOW ALL -Display all the nodes in the Singly Linked List

For Singly Linked List with Iterator:

-INSERT: Allow users to insert cars or motocycles at least 4 vehicles or until users want to stop.For each car or motocycle, ask users to enter information that needs to create an object then insert it to the Singly Linked List Iterator. Display all the nodes in the Singly Linked List Iterator

-FETCH: Fetch and display the information of two first node on the Singly Linked List with iterator

-UPDATE: -Add “25” in front of each VIN of all cars and motocycles in the Singly Linked List with iterator The display all the Singly Linked List with iterator to see the change

-DELETE -Delete the node at third location on the Singly Linked List with iterator -Display the Singly Linked List with iterator to see the change

For the Java LinkedList with iterator

-INSERT: Allow users to insert cars or motocycles at least 4 vehicles or until users want to stop.For each car or motocycle, ask users to enter information that needs to create an object then insert it to the Java Linked List Display all the nodes in the Java Linked List

FETCH -Fetch and display 2 first cars or motocycles on the list

UPDATE -Fetch the node at location 2 -Change its color -Update the node with new color to the Java Linked List Display all the nodes in the Java Linked List

DELETE -move forward the iterator one location then delete the car or motocycle at the current iterator position - Display all the nodes in the Java Linked List

Explanation / Answer

Answer:

import java.util.*;

class Node
{
    protected int info;
    protected Node connect;

  
    public Node()
    {
        connect = null;
        info = 0;
    }  

    public Node(int d,Node n)
    {
        info = d;
        connect = n;
    }  
  
    public void setconnect(Node n)
    {
        connect = n;
    }  

    public void setinfo(int d)
    {
        info = d;
    }  

    public Node getconnect()
    {
        return connect;
    }  

    public int getinfo()
    {
        return info;
    }
}

class linkedList
{
    protected Node begin;
    protected Node last ;
    public int size ;

  
    public linkedList()
    {
        begin = null;
        last = null;
        size = 0;
    }
  
    public boolean isEmpty()
    {
        return begin == null;
    }

    public int getSize()
    {
        return size;
    }  
        public void insertAtbegin(int input)
    {
        Node node_pointer = new Node(input, null);  
        size++ ;  
        if(begin == null)
        {
            begin = node_pointer;
            last = begin;
        }
        else
        {
            node_pointer.setconnect(begin);
            begin = node_pointer;
        }
    }

    public void insertAtlast(int input)
    {
        Node node_pointer = new Node(input,null);  
        size++ ;  
        if(begin == null)
        {
            begin = node_pointer;
            last = begin;
        }
        else
        {
            last.setconnect(node_pointer);
            last = node_pointer;
        }
    }

    public void insertAtPos(int input , int pos)
    {
        Node node_pointer = new Node(input, null);              
        Node input_pointer = begin;
        pos = pos - 1 ;
        for (int i = 1; i < size; i++)
        {
            if (i == pos)
            {
                Node read = input_pointer.getconnect() ;
                input_pointer.setconnect(node_pointer);
                node_pointer.setconnect(read);
                break;
            }
            input_pointer = input_pointer.getconnect();
        }
        size++ ;
    }
  
    public void deleteAtPos(int pos)
    {      
        if (pos == 1)
        {
            begin = begin.getconnect();
            size--;
            return ;
        }
        if (pos == size)
        {
            Node s = begin;
            Node t = begin;
            while (s != last)
            {
                t = s;
                s = s.getconnect();
            }
            last = t;
            last.setconnect(null);
            size --;
            return;
        }
        Node input_pointer = begin;
        pos = pos - 1 ;
        for (int i = 1; i < size - 1; i++)
        {
            if (i == pos)
            {
                Node read = input_pointer.getconnect();
                read = read.getconnect();
                input_pointer.setconnect(read);
                break;
            }
            input_pointer = input_pointer.getconnect();
        }
        size-- ;
    }  
  
    public void display()
    {
        System.out.print(" Singly connected List = ");
        if (size == 0)
        {
            System.out.print("empty ");
            return;
        }  
        if (begin.getconnect() == null)
        {
            System.out.println(begin.getinfo() );
            return;
        }
        Node input_pointer = begin;
        System.out.print(begin.getinfo()+ "->");
        input_pointer = begin.getconnect();
        while (input_pointer.getconnect() != null)
        {
            System.out.print(input_pointer.getinfo()+ "->");
            input_pointer = input_pointer.getconnect();
        }
        System.out.print(input_pointer.getinfo()+ " ");
    }
}

class LinkedListApp
{  
    public static void main(String[] args)
    {           
        Scanner scan = new Scanner(System.in);
      
        linkedList list = new linkedList();
        System.out.println("Singly Linked List Test ");        
        char ch;
      
        do
        {
            System.out.println(" Operations of linked list ");
            System.out.println("1. insert at starting position");
            System.out.println("2. insert at last position");
            System.out.println("3. insert at position");
            System.out.println("4. delete at position");
            System.out.println("5. check if empty");
            System.out.println("6. get size");          
            int choice = scan.nextInt();          
            switch (choice)
            {
            case 1 :
                System.out.println("Enter element to insert");
                list.insertAtbegin(scan.nextInt());                   
                break;                        
            case 2 :
                System.out.println("Enter element to insert");
                list.insertAtlast( scan.nextInt() );                   
                break;                       
            case 3 :
                System.out.println("Enter element to insert");
                int num = scan.nextInt() ;
                System.out.println("Enter position");
                int pos = scan.nextInt() ;
                if (pos <= 1 || pos > list.getSize() )
                    System.out.println("not a valid position ");
                else
                    list.insertAtPos(num, pos);
                break;                                        
            case 4 :
                System.out.println("Enter any position");
                int p = scan.nextInt() ;
                if (p < 1 || p > list.getSize() )
                    System.out.println("not a valid position ");
                else
                    list.deleteAtPos(p);
                break;
            case 5 :
                System.out.println("status of empty"+ list.isEmpty());
                break;                 
            case 6 :
                System.out.println("Size = "+ list.getSize() +" ");
                break;                       
             default :
                System.out.println("Inavlid entry ");
                break;
            }
          
            list.display();
            System.out.println(" Want to continue Press y or n ");
            ch = scan.next().charAt(0);                      
        } while (ch == 'Y'|| ch == 'y');             
    }
}