1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
schepotkina [342]
3 years ago
15

Develop Java methods to perform the following operations on an unordered list (using linked lists). What is the complexity of ea

ch of these algorithms. 1. Insert an element at the start (front) of the list 2. Insert an element at the end (rear) of the list 3. Insert an element at the middle of the list following an element already in the list. 4. Remove an element from the middle of the list
Computers and Technology
1 answer:
maxonik [38]3 years ago
4 0

Answer:

Check the explanation

Explanation:

Java Program to Implement Singly Linked List

import java. util. Scanner;

/* Class Node */

class Node

{

   protected int data;

   protected Node link;

   /* Constructor */

   public Node()

   {

       link = null;

       data = 0;

   }  

   /* Constructor */

   public Node(int d,Node n)

   {

       data = d;

       link = n;

   }  

   /* Function to set link to next Node */

   public void setLink(Node n)

   {

       link = n;

   }  

   /* Function to set data to current Node */

   public void setData(int d)

   {

       data = d;

   }  

   /* Function to get link to next node */

   public Node getLink()

   {

       return link;

   }  

   /* Function to get data from current Node */

   public int getData()

   {

       return data;

   }

}

/* Class linkedList */

class linkedList

{

   protected Node start;

   protected Node end ;

   public int size ;

   /* Constructor */

   public linkedList()

   {

       start = null;

       end = null;

       size = 0;

   }

   /* Function to check if list is empty */

   public boolean isEmpty()

   {

       return start == null;

   }

   /* Function to get size of list */

   public int getSize()

   {

       return size;

   }  

   /* Function to insert an element at begining */

   public void insertAtStart(int val)

   {

       Node nptr = new Node(val, null);  

       size++ ;  

       if(start == null)

       {

           start = nptr;

           end = start;

       }

       else

       {

           nptr. setLink(start);

           start = nptr;

       }

   }

   /* Function to insert an element at end */

   public void insertAtEnd(int val)

   {

       Node nptr = new Node(val,null);  

       size++ ;  

       if(start == null)

       {

           start = nptr;

           end = start;

       }

       else

       {

           end. setLink(nptr);

           end = nptr;

       }

   }

   /* Function to insert an element at position */

   public void insertAtPos(int val , int pos)

   {

       Node nptr = new Node(val, null);              

       Node ptr = start;

       pos = pos - 1 ;

       for (int i = 1; i < size; i++)

       {

           if (i == pos)

           {

               Node tmp = ptr. getLink() ;

               ptr. setLink(nptr);

               nptr. setLink(tmp);

               break;

           }

           ptr = ptr. getLink();

       }

       size++ ;

   }

   /* Function to delete an element at position */

   public void deleteAtPos(int pos)

   {      

       if (pos == 1)

       {

           start = start. getLink();

           size--;

           return ;

       }

       if (pos == size)

       {

           Node s = start;

           Node t = start;

           while (s != end)

           {

               t = s;

               s = s. getLink();

           }

           end = t;

           end. setLink(null);

           size --;

           return;

       }

       Node ptr = start;

       pos = pos - 1 ;

       for (int i = 1; i < size - 1; i++)

       {

           if (i == pos)

           {

               Node tmp = ptr. getLink();

               tmp = tmp. getLink();

               ptr. setLink(tmp);

               break;

           }

           ptr = ptr. getLink();

       }

       size-- ;

   }  

   /* Function to display elements */

   public void display()

   {

       System.out. print("\nSingly Linked List = ");

       if (size == 0)

       {

           System. out. print("empty\n");

           return;

       }  

       if (start. getLink() == null)

       {

           System. out. println(start. getData() );

           return;

       }

       Node ptr = start;

       System. out. print(start. getData()+ "->");

       ptr = start. getLink();

       while (ptr. getLink() != null)

       {

           System. out. print(ptr. getData()+ "->");

           ptr = ptr. getLink();

       }

       System. out. print(ptr. getData()+ "\n");

   }

}

/* Class SinglyLinkedList */

public class SinglyLinkedList

{  

   public static void main(String[] args)

   {            

       Scanner scan = new Scanner(System.in);

       /* Creating object of class linkedList */

       linkedList list = new linkedList();

       System. out. println("Singly Linked List Test\n");        

       char ch;

       /* Perform list operations */

       do

       {

           System. out. println("\nSingly Linked List Operations\n");

           System. out. println("1. insert at the start");

           System. out. println("2. insert at the rear");

           System. out. println("3. insert at the middle of the list");

           System. out. println("4. Remove from the middle of list");

   

           int choice = scan. nextInt();          

           switch (choice)

           {

           case 1 :

               System. out. println("Enter integer element to insert");

               list. insertAtStart( scan. nextInt() );                    

               break;                        

           case 2 :

               System. out. println("Enter integer element to insert");

               list. insertAtEnd( scan. nextInt() );                    

               break;                        

           case 3 :

               System. out. println("Enter integer 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("Invalid position\n");

               else

                   list. insertAtPos(num, pos);

               break;                                        

           case 4 :

               System. out. println("Enter position");

               int p = scan. nextInt() ;

               if (p < 1 || p > list. getSize() )

                   System. out. println("Invalid position\n");

               else

                   list. deleteAtPos(p);

               break;

            default :

               System. out. println("Wrong Entry \n ");

               break;

           }

           /* Display List */

           list. display();

           System. out. println("\nDo you want to continue (Type y or n) \n");

           ch = scan. next(). charAt(0);                      

       } while (ch == 'Y'|| ch == 'y');              

   }

}

You might be interested in
Which of the following is a benefit of using a parallel circuit system?
Ad libitum [116K]

The answer is stability. In a parallel circuit the potential source or voltage remains constant even as you add additional load to the circuit. Next would be simplicity and security, do note that the normal and typical circuitry found on household uses parallel circuit model.

7 0
3 years ago
How many bytes are in 1 kilobyte of storage
topjm [15]

Answer:

1000

Explanation I SEARCHED IT Up

5 0
4 years ago
Emilee is watching an online video. The video is being sent to her laptop by a server over the Internet which splits the video i
Tresset [83]

Answer:

The answer is "Option C".

Explanation:The packet includes clients and modifies information both of these are often referred to as the payload. To achieve payload data by monitor data to get the control signals. It is a unit that will be an information material consisting of a specific network path into such a single packet and web browser transmissions and other types of networks will be used in the IP transmissions, in this, the data packet may arrive in out of order, and the wrong choice can be defined as follows:

  • In choice A, when the connection is good so, all the data is sent on the computer, that's why it is incorrect.
  • In choice B, It is wrong because it can't send back to the server.
  • In choice D, It can't be sent in order, that's why it is wrong.  
6 0
4 years ago
Which exercise can help you prevent Carpel Tunnel Syndrome?
g100num [7]
Make a Fist, hold, and stretch your fingers is the correct answer
8 0
4 years ago
Read 2 more answers
What does it mean by “Your circles should shrink by half” sorry if this isn’t a clear picture.
damaskus [11]

Answer:

I'm not seeing anything so sorry

5 0
3 years ago
Other questions:
  • Walter’s health insurance premium increased by 22 percent this year. Now he pays $488 every month for health insurance. What was
    10·1 answer
  • Instructions: Type the correct answer in the box. Spell the word correctly.
    5·2 answers
  • Give a big-O estimate the number of operations, where an operation is a comparison or a multiplication, used in this segment of
    10·1 answer
  • Please I need all the help I can get
    11·1 answer
  • What should you do in order to have access to the header and footer tools? Open the View tab. Open the Page Setup dialog box. Op
    6·1 answer
  • Organizations following Waterfall methodology usually begin with requirements gathering, in which the development team attempts
    14·1 answer
  • An enhancement to a computer accelerates some mode of execution by a factor of 10. The enhanced mode is used 50% of the time, me
    11·1 answer
  • What is a frame injection attack in a wireless network?
    13·2 answers
  • Explain the procedure you will undertake to create a new partition​
    6·1 answer
  • How do even do anything on Rblx Creator?? Will give branliest for whoever gives me the most info.
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!