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
An administrator needs to make sure that video conferencing is allocated a high portion of bandwidth within a company network. W
frosja888 [35]

Answer:

QoS

Explanation:

We could use a blacklisting or a MAC filtering to remove from the network, and in this way, we're going to get a high portion of bandwidth, but other must work too, and the whitelisting have almost the same function, but QoS Quality of service is specifically for these cases where we need high bandwidth for a video conference, we can make this with some tools or techniques like jitter buffer and traffic shaping.

4 0
3 years ago
Cell B10 contains a date, such as 9/1/2014. Which formula determines how many days have passed between that date and the current
Jet001 [13]
<span>If using excel, you can use this formula: DATEDIF(start_date, end_date, unit) For instance, DATEDIF(9/1/2014, today, "d") or DATEDIF(B10, today, "d")</span>
3 0
3 years ago
Read 2 more answers
Choose the correct function to yield the shown result.
HACTEHA [7]

Answer:

int

Explanation:

Correct on edg test 2021

8 0
3 years ago
DISEÑAR EL SIGUIENTE FORMULARIO: Y CALCULAR EN PHP: ¿cuánto pagaría un cliente, si elige un plan de pagos de acuerdo a los sigui
Karolina [17]
Sorry don’t get your language
3 0
3 years ago
Wireless attacks avoid the access points to limit detection. <br> a. True <br> b. False
-BARSIC- [3]

Answer:

Option B i.e., False.

Explanation:

The weakness, KRACK tricks a wifi access points to reuse an in-use encryption, enabling the intruder to decode and interpret data intended to remain encrypted. Wireless communication encrypts, that decrypt unencrypted wireless network activity and expose sensitive data.

So, the following scenario is false about the wireless attack.

6 0
3 years ago
Other questions:
  • A weak fuel to air mixture along with normal airflow through a turbine engine may result in?
    8·1 answer
  • Emma has used copyright works of a number of artists in her photo album and cited all of them except one. She may be caught for
    6·2 answers
  • So lets see what you know...Write me a short code and include a detailed explanation of the language used and what the out come
    7·1 answer
  • What is the simplest way to permanently get rid of an unwanted file? A. Cancel the file. B. Go to the Start menu and then delete
    14·1 answer
  • What is infinite recursion?
    7·1 answer
  • Technology that verifies a person's identity is _____. a smart card Automated Fingerprint Verification an EPD a digital ink pen
    13·2 answers
  • Select the correct text in the passage.
    12·1 answer
  • The _____ path the name of the fire for the page and any directories or subdirectories​
    9·1 answer
  • Besides entering a URL to go directly to a website, what else can you enter in a browser address bar to explore the internet?
    9·1 answer
  • As part of your regular system maintenance, you install the latest operating system updates on your Windows 10 computer. After s
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!