0% found this document useful (0 votes)
48 views12 pages

Lec 3 (Linked List)

The document discusses Java linked lists. It provides examples of creating linked lists using the LinkedList class and adding, removing, and accessing elements. Key points include: - Java has a LinkedList class that implements the linked list data structure - Elements can be added to the beginning or end of the list using addFirst() and addLast() - Elements can be removed from the beginning or end using removeFirst() and removeLast() - The getFirst() and getLast() methods return the first and last elements

Uploaded by

Dr Lola
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views12 pages

Lec 3 (Linked List)

The document discusses Java linked lists. It provides examples of creating linked lists using the LinkedList class and adding, removing, and accessing elements. Key points include: - Java has a LinkedList class that implements the linked list data structure - Elements can be added to the beginning or end of the list using addFirst() and addLast() - Elements can be removed from the beginning or end using removeFirst() and removeLast() - The getFirst() and getLast() methods return the first and last elements

Uploaded by

Dr Lola
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Java

A linked list is a sequence of data elements, which are connected


together via links. Java have linked lists in its standard library.

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

import java.util.LinkedList; file : st.java


public class st{
public static void main(String[] args) {
LinkedList<String> AA= new LinkedList<>();
AA.add("Volvo");
AA.add("BMW");
AA.add("Ford");

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");

System.out.println("The first element is " + cars.getFirst());

System.out.println(cars+ " "+"Linked List size =" + cars.size());


}
}
The first element is Volvo
[Volvo, BMW, Ford, Mazda] Linked List size =4
11
import java.util.LinkedList; Get the last Element
public 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");

System.out.println("The last element is "+cars.getLast());

System.out.println("The original Linked List is "+ cars);


}
}

12

You might also like