Java Linked List

  1. Creation & Insertion of LinkedList in Java
  2. AddFirst & RemoveFirst - LinkedList in Java
  3. AddLast & RemoveLast - LinkedList in Java
  4. Add using Index & Remove using Index - LinkedList in Java
  5. AddMiddle & RemoveMiddle - LinkedList in Java

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Creation & Insertion of LinkedList in Java


LinkedList_Ex1.java

import java.util.LinkedList;
import java.util.Scanner;
 
public class LinkedList_Ex1 {
 
	Scanner scan;
	LinkedList<String> list;
	
	int n;
	
	void getVal() {
		
		scan = new Scanner(System.in);
		list = new LinkedList<String>();
		
		System.out.println("Linked List - Create & Insert");
		
		System.out.println("\nEnter 'n' value");
		n = scan.nextInt();
		
		System.out.println("Enter the data");
 
		for(int i=0; i<n; i++) {
			
			list.add(scan.next());
		}
	}
	
	void display() {
		
		System.out.println("\nThe Linked List");
 
		for(int i=0; i<list.size(); i++) {
			
			System.out.println(list.get(i));
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		LinkedList_Ex1 obj = new LinkedList_Ex1();
		
		obj.getVal();
		obj.display();
	}
}

Sample Output

Linked List - Create & Insert

Enter 'n' value
5
Enter the data
082IT111
Balaji
IT
BIT
India

The Linked List
082IT111
Balaji
IT
BIT
India




B. AddFirst & RemoveFirst - LinkedList in Java


LinkedList_Ex2.java

import java.util.LinkedList;
import java.util.Scanner;
 
public class LinkedList_Ex2 {
 
	Scanner scan;
	LinkedList<String> list;
	
	int n;
	
	void getVal() {
		
		scan = new Scanner(System.in);
		list = new LinkedList<String>();
		
		System.out.println("Linked List - Add & Remove First");
		
		System.out.println("\nEnter 'n' value");
		n = scan.nextInt();
		
		System.out.println("Enter the data");
 
		for(int i=0; i<n; i++) {
			
			list.add(scan.next());
		}
	}
	
	void display() {
		
		System.out.println("\nThe Linked List");
 
		for(int i=0; i<list.size(); i++) {
			
			System.out.println(list.get(i));
		}
	}
	
	void AddFirst() {
		
		System.out.println("\nAdd First");
		System.out.println("Enter the data");
		String item = scan.next();
		
		list.addFirst(item);
		
		display();
	}
	
	void RemoveFirst() {
		
		System.out.println("\nRemove First");
		
		list.removeFirst();
		
		display();
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		LinkedList_Ex2 obj = new LinkedList_Ex2();
		
		obj.getVal();
		obj.display();
		
		obj.AddFirst();
		obj.RemoveFirst();
 
	}
}

Sample Output

Linked List - Add & Remove First

Enter 'n' value
3
Enter the data
1
2
3

The Linked List
1
2
3

Add First
Enter the data
0

The Linked List
0
1
2
3

Remove First

The Linked List
1
2
3




C. AddLast & RemoveLast - LinkedList in Java


LinkedList_Ex3.java

import java.util.LinkedList;
import java.util.Scanner;
 
public class LinkedList_Ex3 {
 
	Scanner scan;
	LinkedList<String> list;
	
	int n;
	
	void getVal() {
		
		scan = new Scanner(System.in);
		list = new LinkedList<String>();
		
		System.out.println("Linked List - Add & Remove Last");
		
		System.out.println("\nEnter 'n' value");
		n = scan.nextInt();
		
		System.out.println("Enter the data");
 
		for(int i=0; i<n; i++) {
			
			list.add(scan.next());
		}
	}
	
	void display() {
		
		System.out.println("\nThe Linked List");
 
		for(int i=0; i<list.size(); i++) {
			
			System.out.println(list.get(i));
		}
	}
	
	void AddLast() {
		
		System.out.println("\nAdd Last");
		System.out.println("Enter the data");
		String item = scan.next();
		
		list.addLast(item);
		
		display();
	}
	
	void RemoveLat() {
		
		System.out.println("\nRemove Last");
		
		list.removeLast();
		
		display();
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		LinkedList_Ex3 obj = new LinkedList_Ex3();
		
		obj.getVal();
		obj.display();
		
		obj.AddLast();
		obj.RemoveLat();
	}
}

Sample Output

Linked List - Add & Remove Last

Enter 'n' value
3
Enter the data
1
2
3

The Linked List
1
2
3

Add Last
Enter the data
100

The Linked List
1
2
3
100

Remove Last

The Linked List
1
2
3




D. Add using Index & Remove using Index - LinkedList in Java


LinkedList_Ex4.java

import java.util.LinkedList;
import java.util.Scanner;
 
public class LinkedList_Ex4 {
 
	Scanner scan;
	LinkedList<String> list;
	
	int n;
	
	void getVal() {
		
		scan = new Scanner(System.in);
		list = new LinkedList<String>();
		
		System.out.println("Linked List - Add & Remove using index");
		
		System.out.println("\nEnter 'n' value");
		n = scan.nextInt();
		
		System.out.println("Enter the data");
 
		for(int i=0; i<n; i++) {
			
			list.add(scan.next());
		}
	}
	
	void display() {
		
		System.out.println("\nThe Linked List");
 
		for(int i=0; i<list.size(); i++) {
			
			System.out.println(list.get(i));
		}
	}
	
	void AddIndex() {
		
		System.out.println("\nAdd using index");
		
		System.out.println("Enter the index");
		int index = scan.nextInt();
		
		System.out.println("Enter the data");
		String item = scan.next();
		
		list.add(index - 1, item);
		
		display();
	}
	
	void RemoveIndex() {
		
		System.out.println("\nRemove using index");
		
		System.out.println("Enter the index");
		int index = scan.nextInt();
		
		list.remove(index-1);
		
		display();
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		LinkedList_Ex4 obj = new LinkedList_Ex4();
		
		obj.getVal();
		obj.display();
		
		obj.AddIndex();
		obj.RemoveIndex();
	}
}

Sample Output

Linked List - Add & Remove using index

Enter 'n' value
3
Enter the data
1
2
3

The Linked List
1
2
3

Add using index
Enter the index
2
Enter the data
0

The Linked List
1
0
2
3

Remove using index
Enter the index
3

The Linked List
1
0
3




E. AddMiddle & RemoveMiddle - LinkedList in Java


LinkedList_Ex5.java

import java.util.LinkedList;
import java.util.Scanner;
 
public class LinkedList_Ex5 {
 
	Scanner scan;
	LinkedList<String> list;
	
	int n;
	int index;
	
	void getVal() {
		
		scan = new Scanner(System.in);
		list = new LinkedList<String>();
		
		System.out.println("Linked List - Add & Remove Middle");
		
		System.out.println("\nEnter 'n' value");
		n = scan.nextInt();
		
		System.out.println("Enter the data");
 
		for(int i=0; i<n; i++) {
			
			list.add(scan.next());
		}
	}
	
	void display() {
		
		System.out.println("\nThe Linked List");
 
		for(int i=0; i<list.size(); i++) {
			
			System.out.println(list.get(i));
		}
	}
	
	void AddMiddle() {
		
		System.out.println("\nAdd Middle");
		
		index = (list.size()) / 2;
		
		System.out.println("Enter the data");
		String item = scan.next();
		
		list.add(index, item);
		
		display();
	}
	
	void RemoveMiddle() {
		
		System.out.println("\nRemove Middle");
				
		list.remove(index);
		
		display();
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		LinkedList_Ex5 obj = new LinkedList_Ex5();
		
		obj.getVal();
		obj.display();
		
		obj.AddMiddle();
		obj.RemoveMiddle();
	}
}

Sample Output

Linked List - Add & Remove Middle

Enter 'n' value
4
Enter the data
1
2
3
4

The Linked List
1
2
3
4

Add Middle
Enter the data
0

The Linked List
1
2
0
3
4

Remove Middle

The Linked List
1
2
3
4
Linked List - Add & Remove Middle

Enter 'n' value
3
Enter the data
1
2
3

The Linked List
1
2
3

Add Middle
Enter the data
0

The Linked List
1
0
2
3

Remove Middle

The Linked List
1
2
3






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com