Skip to main content

Implement Linked List in java

1a) Implement Linked List in java

  It uses a doubly linked list to store the elements.

      Features

It contains duplicate elements

It maintains insertion order

It can be used as a list, stack, or queue.

      Constructors

LinkedList()-constructs an empty list

LinkedList(Collection c)-constructs list containing elements of Collection.

      Methods

add(int index,Object element)-adds element at the specified position in list

addFirst(Object o)-adds the element at the beginning of list

addLast(Object o)-appends the specified element at the end of the list

size()-returns the number of elements in list

remove(Object o)-removes the first occurrence of the specified element in the list

Program:


import java.util.LinkedList; //LinkedList class uses a doubly linked list for storing elements

import java.util.Scanner; //Scanner class reads data from the input stream

class Linkedlistnew

{

 public static void main(String[] args)

 {

  LinkedList<Integer> obj=new LinkedList<Integer>();

  int e;

  Object x;

  Scanner s=new Scanner(System.in);

  while(true)

  {

   System.out.println("1.Add Element at beginning");

   System.out.println("2.Add Element at End");

   System.out.println("3.Add Element at specified position");

   System.out.println("4.Delete Element at beginning");

   System.out.println("5.Delete Element at End");

   System.out.println("6.Delete specified element");

   System.out.println("7.Display");

   System.out.println("8.Quit");

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

   int choice=s.nextInt();

   switch(choice)

   {

     case 1:System.out.println("Enter the element to be added at beginning");

            e=s.nextInt();

            obj.addFirst(e);

            break;

     case 2:System.out.println("Enter the element to be added at end");

            e=s.nextInt();

            obj.addLast(e);

            break;

     case 3:System.out.println("Enter position");

            int pos=s.nextInt();

            System.out.println("Enter the element to be added at"+pos+" position");

            e=s.nextInt();

            obj.add(pos,e);

            break;

     case 4:if(obj.isEmpty())

            System.out.println("List is empty");

            else

            {

            x=obj.getFirst();//getFirst() returns the first element in linkedlist and returns an Object reference

            if(obj.remove(x))

            System.out.println(x+ "is deleted");

            }

            break;

     case 5:if(obj.isEmpty())

            System.out.println("List is empty");

            else

            {

            x=obj.getLast();//getLast() returns the last element in linkedlist and returns Object reference

            if(obj.remove(x))

            System.out.println(x+ "is deleted");

            }

            break;

     case 6:if(obj.isEmpty())

            System.out.println("List is empty");

            else

            {

            System.out.println("Enter position to delete the element");

            pos=s.nextInt();

            if(pos<obj.size())

            {

            x=obj.get(pos);//get() returns the position of element in linkedlist

            obj.remove(x);

            System.out.println(x+"is deleted"); 

            }

            else

            System.out.println("element not found in list at that position");

            }

            break;

     case 7:if(obj.isEmpty())

            System.out.println("List is empty");

            else

            {

            System.out.println("Linked List elements");

            for(int i=0;i<obj.size();++i)//obj.size() gives no of elements in LinkedList

            System.out.println(obj.get(i));//get()-returns the element from LinkedList based on position

            }

           break;   

     case 8:System.exit(0);

     default:System.out.println("Wrong choice");

    }

   }

  }

}

Expected Output

1.Add Element at beginning

2.Add Element at End

3.Add Element at specified position

4.Delete Element at beginning

5.Delete Element at End

6.Delete specified element

7.Display

8.Quit

Enter choice

1

Enter the element to be added at beginning

10

1.Add Element at beginning

2.Add Element at End

3.Add Element at specified position

4.Delete Element at beginning

5.Delete Element at End

6.Delete specified element

7.Display

8.Quit

Enter choice

2

Enter the element to be added at end

20

1.Add Element at beginning

2.Add Element at End

3.Add Element at specified position

4.Delete Element at beginning

5.Delete Element at End

6.Delete specified element

7.Display

8.Quit

Enter choice

1

Enter the element to be added at beginning

30

1.Add Element at beginning

2.Add Element at End

3.Add Element at specified position

4.Delete Element at beginning

5.Delete Element at End

6.Delete specified element

7.Display

8.Quit

Enter choice

3

Enter position

2

Enter the element to be added at2 position

40

1.Add Element at beginning

2.Add Element at End

3.Add Element at specified position

4.Delete Element at beginning

5.Delete Element at End

6.Delete specified element

7.Display

8.Quit

Enter choice

7

Linked List elements

30

10

40

20

1.Add Element at beginning

2.Add Element at End

3.Add Element at specified position

4.Delete Element at beginning

5.Delete Element at End

6.Delete specified element

7.Display

8.Quit

Enter choice

4

30is deleted

1.Add Element at beginning

2.Add Element at End

3.Add Element at specified position

4.Delete Element at beginning

5.Delete Element at End

6.Delete specified element

7.Display

8.Quit

Enter choice

7

Linked List elements

10

40

20

1.Add Element at beginning

2.Add Element at End

3.Add Element at specified position

4.Delete Element at beginning

5.Delete Element at End

6.Delete specified element

7.Display

8.Quit

Enter choice

6

Enter position to delete the element

2

20is deleted

1.Add Element at beginning

2.Add Element at End

3.Add Element at specified position

4.Delete Element at beginning

5.Delete Element at End

6.Delete specified element

7.Display

8.Quit

Enter choice

7

Linked List elements

10

40

1.Add Element at beginning

2.Add Element at End

3.Add Element at specified position

4.Delete Element at beginning

5.Delete Element at End

6.Delete specified element

7.Display

8.Quit

Enter choice

5

40is deleted

1.Add Element at beginning

2.Add Element at End

3.Add Element at specified position

4.Delete Element at beginning

5.Delete Element at End

6.Delete specified element

7.Display

8.Quit

Enter choice

7

Linked List elements

10

1.Add Element at beginning

2.Add Element at End

3.Add Element at specified position

4.Delete Element at beginning

5.Delete Element at End

6.Delete specified element

7.Display

8.Quit

Enter choice

4

10is deleted

1. Add Element at beginning

2. Add Element at End

3. Add Element at the specified position

4. Delete Element at beginning

5. Delete Element at End

6. Delete specified element

7. Display

8. Quit

Enter choice

7

List is empty

1. Add Element at beginning

2. Add Element at End

3. Add Element at the specified position

4. Delete Element at beginning

5. Delete Element at End

6. Delete specified element

7. Display

8. Quit

Enter choice

8


Comments

Popular posts from this blog

Big Data Analytics Programs

  List of Programs for Big Data Analytics   CLICK ON ME 1.  Implement the following Data structures in Java       a)  Linked Lists            b)   Stacks       c)  Queues     d)   Set            e)   Map 2.  Perform setting up and Installing Hadoop in its three operating modes:      Standalone,     Pseudo distributed,     Fully distributed. 3.  Implement the following file management tasks in Hadoop:    a) Adding files and directories    b) Retrieving files    c) Deleting files 4. Run a basic Word Count Map Reduce program to understand Map Reduce Paradigm. 5. Write a Map Reduce program that mines weather data.     Weather sensors collecting data every hour at many locations across the globe gather a large volume of log data, which is a ...

How to Install Parrot Operating System in Virtual Box using OVA

Step by Step Process of Parrot OS Installation What is Parrot OS Parrot is a free and open-source Linux system based on Debian that is popular among security researchers, security experts, developers, and privacy-conscious users. It comes with cyber security and digital forensics arsenal that is totally portable. It also includes everything you'll need to make your own apps and protect your online privacy. Parrot is offered in Home and Security Editions, as well as a virtual machine and a Docker image, featuring the KDE and Mate desktop environments. Features of Parrot OS The following are some of the features of Parrot OS that set it apart from other Debian distributions: Tor, Tor chat, I2P, Anonsurf, and Zulu Crypt, which are popular among developers, security researchers, and privacy-conscious individuals, are included as pre-installed development, forensics, and anonymity applications. It has a separate "Forensics Mode" that does not mount any of the system's hard...

Hadoop file Management Tasks

  Implement the following file management tasks in Hadoop: a) Adding files and directories b) Retrieving files c) Deleting files Hint: A typical Hadoop workflow creates data files (such as log files) elsewhere and copies them into HDFS using one of the above command line utilities. Program:  The most common file management tasks in Hadoop includes: Adding files and directories to HDFS Retrieving files from HDFS to local filesystem Deleting files from HDFS Hadoop file commands take the following form:     hadoop fs - cmd Where cmd is the specific file command and <args> is a variable number of arguments. The command cmd is usually named after the corresponding Unix equivalent. For example, the command for listing files is ls as in Unix. a) Adding Files and Directories to HDFS Creating Directory in HDFS    $ hadoop fs - mkdir foldername (syntax)  $ ha...