dElmARk Admin
Posts : 92 Join date : 09/04/2012
| Subject: Linked List Java Program Tue Oct 23, 2012 5:28 pm | |
| - LinkedList Syntax:
LinkedList LL = new LinkedList(); //Adding LL.add(); LL.addLast(); LL.addFirst();
//Adding @ specific index LL.add(index, element);
//Removing LL.remove(); LL.removeLast(); LL.removeFirst();
//Removing @ specific index LL.remove(ind);
| |
|
dElmARk Admin
Posts : 92 Join date : 09/04/2012
| Subject: Re: Linked List Java Program Tue Oct 23, 2012 8:10 pm | |
| - Code:
-
import java.util.*; class LLS{ public static void main(String [] ecs){ LinkedList LLL = new LinkedList(); Scanner sc=new Scanner(System.in); System.out.println("LinkedList Elements " + LLL); System.out.println("Enter an Option\n1 - AddFirst\t2 - AddLast\t3 - Add @ specified index\t4 - RemoveFirst\n\t5 - RemoveLast\t6 - Remove @ specified index\t7 - exit"); int ch = 0; int elem = 0; int ind = 0; do{ System.out.print("Choice: "); ch = sc.nextInt(); if(ch==1){ System.out.print("Enter element to add\n"); elem = sc.nextInt(); LLL.addFirst(elem); } if(ch==2){ System.out.print("Enter element to add\n"); elem = sc.nextInt(); LLL.addLast(elem); } if(ch==3){ System.out.print("Enter index: "); ind = sc.nextInt(); System.out.print("Enter element: "); elem = sc.nextInt(); LLL.add(ind,elem); } if(ch==4){ LLL.removeFirst(); }; if(ch==5){ LLL.removeLast(); } if(ch==6){ System.out.print("Enter index: "); ind = sc.nextInt(); LLL.remove(ind); } System.out.println("LinkedList Elements " + LLL); }while(ch!=7); } } | |
|