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