Lec 3 (Linked List)
Lec 3 (Linked List)
1
Creation of Linked list
A linked list is created by using the node class we studied in the last chapter. We create a Node
object and create another class to use this ode object. We pass the appropriate values through
the node object to point the to the next data elements.
import java.util.LinkedList;
public class st
{
public static void main(String args[ ])
{
LinkedList<String> cars = new LinkedList<>();
cars.add("Volvo") ;
cars.add("BMW") ;
cars.add("Ford") ;
System.out.println(cars); [Volvo, BMW, Ford]
}
2
}
Creation of Linked list
A linked list is created by using the node class we studied in the last chapter. We create a Node
object and create another class to use this ode object. We pass the appropriate values through
the node object to point the to the next data elements.
import java.util.LinkedList;
public class stt
{
public static void main(String args[ ])
{
LinkedList<Integer> NN= new LinkedList<>();
NN.add(17) ;
NN.add(-4) ;
NN.add(6) ;
System.out.println(NN);
} [17, -4, 6]
}
3
import java.util.LinkedList;
public class stt
{
public static void main(String args[ ])
{
LinkedList NN= new LinkedList();
NN.add(17) ;
NN.add(-4) ;
NN.add(“SALAM”) ;
System.out.println(NN);
}
}
[17, -4, SALAM]
4
public class stt
{
public static void main(String args[ ])
{
LinkedList<Integer> X= new LinkedList<>();
for (int i=0;i<7 ; i++)
X.add(i) ;
System.out.println(X);
}
}
[0, 1, 2, 3, 4, 5, 6]
5
import java.util.Scanner;
import java.util.LinkedList;
public class stt {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter integer number: ");
int num = scan.nextInt();
scan.close();
LinkedList<Integer> X= new LinkedList<>();
X.add(num);
System.out.println("The Linked List : "+X);
}
Enter integer number: 77
} The Linked List : [77]
6
Adds an item to the beginning of the list.
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
LinkedList<String> cars = new LinkedList<>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.addFirst("Mazda");
System.out.println(cars);
}
}
7
Add an item to the end of the list
AA.addLast("Mazda");
System.out.println(AA);
}
}
8
import java.util.LinkedList; Remove Element from the first
public class Main {
public static void main(String[ ] args) {
LinkedList<String> BB= new LinkedList<>();
BB.add("Volvo");
BB.add("BMW");
BB.add("Ford");
BB.add("Mazda");
System.out.println(BB);
System.out.println("Linked list after Removing the first element");
BB.removeFirst();
System.out.println(BB);
}
}
9
import java.util.LinkedList; Remove Element from the last
public class st{
public static void main(String[] args) {
LinkedList<String> cars = new LinkedList<>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
System.out.println(cars);
System.out.println("Linked list after Removing the last element");
cars.removeLast();
System.out.println(cars);
}
}
10
import java.util.LinkedList; Get the first Element
class stt {
public static void main(String[] args) {
LinkedList<String> cars = new LinkedList<>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
12