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

Array of Objects

An array can be of any data type including struct. Similarly, we can also have arrays of variables of the type class. Such variables are called arrays of objects. Class Definition: class employee {           char name[30];          float age;     public:          void getdata(void);           void putdata(void); }; The identifier employee is a user-defined data type and can be used to create objects related to different employee categories. employee manage[3];          //aray of managers employee foreman[15];       //array of foreman employee worker[75];        // array of worker the array manager contains three objects(managers), namely, manager[0],  manager[1], and manager[2], of type employee class similarly, the foreman array contains 15 objects. and the worker array contains 75 objectives.(work...

Binning Method by Data smoothing in python

 Binning Method Binning is a technique for smoothing data or dealing with noisy data. The data is sorted first, and then the sorted values are dispersed into a number of buckets or bins in this approach. Binning methods provide local smoothing since they consult the vicinity of values.  Smoothing can be accomplished in three ways: Bin smoothing entails:  Each value in a bin is replaced by the bin's mean value when smoothing by bin means is used.  Smoothing by bin median:  Each bin value is replaced by its bin median value in this method.  Smoothing by bin borders:  In smoothing by bin boundaries, the bin boundaries are determined as the minimum and maximum values in a given bin. The nearest boundary value is then used to replace each bin value. Example: Sorted data for price (in dollars): 4, 8, 9, 15, 21, 21, 24, 25, 26, 28, 29, 34 Smoothing by bin means:       - Bin 1: 9, 9, 9, 9       - Bin 2: 23, 23, 23, 23   ...

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...