Friday, 20 December 2013

LINKED LIST



import java.io.*;
import java.util.*;

public class LinkedListDemo {
    static public void main(String args[]) throws IOException {
        LinkedList<String> ll = new LinkedList<String>();
        ll.add("AMERICA");
        ll.add("INDIA");
        ll.add("JAPAN");
        System.out.println("LIST = " + ll);
         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String element;
        int position, choice = 0;
        while (choice < 4) {
            System.out.println("\n LinkedList operation: ");
            System.out.println("1. ADD an element: ");
            System.out.println("2. REMOVE an element: ");
            System.out.println("3. CHANGE an element: ");
            System.out.println("4. EXIT");
            System.out.println("Enter your choice: ");
            choice = Integer.parseInt(br.readLine());
            switch (choice) {
                case 1:
                    System.out.println("Enter the element: ");
                    element = br.readLine();
                    System.out.println("At what position: ");
                    position = Integer.parseInt(br.readLine());
                    ll.add(position - 1, element);
                    break;
                case 2:
                    System.out.println("At what position: ");
                    position = Integer.parseInt(br.readLine());
                    ll.remove(position - 1);
                    break;
                case 3:
                    System.out.println("Enter the position: ");
                    position = Integer.parseInt(br.readLine());
                    System.out.println("Enter the new Element: ");
                    element = br.readLine();
                    ll.set(position - 1, element);
                    break;
                default:
                    return;
            }
            System.out.println("List = ");
            Iterator it = ll.iterator();
            while (it.hasNext()) {
                System.out.println(it.next() + " ");
            }
        }
    }
}

Linked List: A data structure is said to linear if its elements from a sequence i.e., a list which displays the relationship of adjacency between the elements is said to be linear any other list is said to be non-linear.
Ex: week days – SUN, MON, TUE, WED, THU, FRI, SAT

Single Linked List: A single linked list or one-way list is a collection of data elements called nodes. Here each node is divided into two parts.

The first part contains the information of the elements. The second part contains the address of the next node in the list.

The first part is called data field or information field and the second part is called the Linked field or next address field.
                                The schematic diagram of a linked list is given below
    

No comments:

Post a Comment

java binary tree program for inorder, preorder, postorder

import java.util.Queue; import java.util.LinkedList; public class TreeTraverse { private static class Node<T> { public Node<T> ...