0% found this document useful (0 votes)
22 views27 pages

Topic 3 - Linked List 1

Uploaded by

yves
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)
22 views27 pages

Topic 3 - Linked List 1

Uploaded by

yves
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/ 27

CSC508 Data Structures

Topic 3 : Linked List

Compiled & edited by: Zahid Zainal


Recap

 Collection hierarchy
 Array definition
 Array implementation
 ArrayList
 User-defined

Compiled & edited by: Zahid Zainal


Topic Structure

 Linked list
 Definition
 Characteristics
 Properties
 LinkedList class
 Linked list operation

Compiled & edited by: Zahid Zainal


Learning Outcomes

 At the end of this lesson, students should be able to:


 Define the linked list data structure and its characteristics
 Implement built-in LinkedList class
 Define linked list operations

Compiled & edited by: Zahid Zainal


Linked List Definition

 A list of items, called nodes, in which the order of the


nodes is determined by the address, called the link,
stored in each node.
 a series of connected nodes, where each node is a data structure
 Every node in a linked list has two components:
 one to store relevant information
 one to store address (the link) of next node

Compiled & edited by: Zahid Zainal


Linked List Characteristics

 Homogeneous
 Node should be of the same structure.
 Unlimited size
 The node will be created upon addition. No predefined size.
 Sequential Access
 Traversing the linked list starting from first/head node.

Compiled & edited by: Zahid Zainal


Linked List Properties

 Address of first node in list stored in separate location,


called the head or first
 Data type of each node depends on the specific
application — kind of data being processed
 Link component of each node is a reference variable
 Data type of this reference variable is node type itself

Compiled & edited by: Zahid Zainal


Linked List Properties (cont.)

Value
head 2000
head.info 17
head.link 2800
head.link.info 92

Compiled & edited by: Zahid Zainal


Linked List Properties (cont.)

 NodeType current = head;


 Copies value of head into current

Value
current 2000
current.info 17
current.link 2800
current.link.info 92
Compiled & edited by: Zahid Zainal
Linked List Properties (cont.)

 current = current.link;
 Copies value of current.link (2800) into current

Value
current 2800
current.info 92
current.link 1500
current.link.info 63
Compiled & edited by: Zahid Zainal
Linked List Properties (cont.)

Value
head.link.link 1500
head.link.link.info 63
head.link.link.link 3600
head.link.link.link.info 45
current.link.link 3600
current.link.link.info 45
Runtime
current.link.link.link 0 (that is, NULL)
error
current.link.link.link.info Does not exist
Compiled & edited by: Zahid Zainal
Linked List Operation

 Create
 Insert element
 Insert at the beginning, Insert at the end, Insert at specified
location
 Remove element
 Remove the first node, Remove the last node, Remove specified
node
 Searching

Compiled & edited by: Zahid Zainal


Built-in LinkedList Class

Compiled & edited by: Zahid Zainal


LinkedList Class Implementation

import java.util.Scanner;
import java.util.LinkedList;
Create new
LinkedList object
public class BuiltInLL {

public static void main(String[] args) {


LinkedList<Integer> numList = new LinkedList<Integer>();
Scanner input = new Scanner(System.in);
Add new element
as first node
for(int i = 0; i< 3; i++) {
System.out.println("Enter an integer : ");
int number = input.nextInt();
numList.addFirst(number);
}
System.out.println("The list size : " + numList.size());

Get the size of the


linked list
Compiled & edited by: Zahid Zainal
LinkedList Class Implementation (cont.)
Accessing the list
element
for (int i = 0; i < numList.size(); i++)
System.out.println(numList.get(i));

numList.removeLast();

Remove the last System.out.println("\nAfter removal");


node for (int i = 0; i < numList.size(); i++)
System.out.println(numList.get(i));

Compiled & edited by: Zahid Zainal


Linked List Traversal

 Process of visiting and accessing nodes in a linked list


 Needed for the following operation
 Insert at the end of the list, insert at specified location
 Remove last node, remove node at specified location
 Printing the link list
 Check the size
 Search element

Compiled & edited by: Zahid Zainal


Creating a Linked List

 Define the node structure


class Node{
int data;
data next Node next;
}
 Define the linked list class
class MyLinkedList{
Node head;
int size; Initialize the linked
list attribute
MyLinkedList(){
head = null;
size = 0;
}

Compiled & edited by: Zahid Zainal


Insert node

 Insert at the beginning


Create and populate
public void insertFirst(int x) { the new node
Node newNode = new Node();
newNode.data = x;
newNode.next = head;
head = newNode; Link head to the
size++; new node
}

Update the size

Compiled & edited by: Zahid Zainal


Insert node (cont.)

 Insert at the end public void insertLast (int x) {


if (head == null)
If the list is empty, always insertFirst(x);
insert as first element else {
Node newNode = new Node();
Create and populate newNode.data = x;
the new node newNode.next = null;
Node temp = head; Reference variable
for traversal
Traverse until the while (temp.next != null)
last node temp = temp.next;
temp.next = newNode;
size++;
}
} Update the size
Compiled & edited by: Zahid Zainal
Printing linked list

Check whether
list is empty
public void printLL() {
if (head == null)
System.out.println("List is empty"); Reference variable
else { for traversal
Node temp = head;
while (temp != null) {
System.out.println(temp.data);
temp = temp.next;
}
}
}

Traverse until the


Print data on
end of the list
every node visit
Compiled & edited by: Zahid Zainal
Testing - 1

public class Main {

public static void main(String[] args) {


MyLinkedList LL1 = new MyLinkedList();
LL1.printLL();
System.out.println("***");
LL1.insertFirst(6);
LL1.insertFirst(8);
LL1.insertLast(3);
LL1.insertLast(5);
LL1.printLL();
}
//continue next

Compiled & edited by: Zahid Zainal


Remove node

 Remove the first node

Check whether
public void removeFirst() { list is empty
if (head == null)
System.out.println("Empty list");
else {
head = head.next;
size--;
}
}

Update the size Break the link to the


first node and link to
next node
Compiled & edited by: Zahid Zainal
Remove node (cont.)

 Remove the last node public void removeLast() {


if (head == null)
Check whether System.out.println("Empty list");
list is empty else if (size == 1){
removeFirst();
size--;
Case where there is }
only 1 node in the list else {
Node temp = head;
while (temp.next.next != null)
Traverse until the temp = temp.next;
second last node temp.next = null;
size--;
}
Break the link to }
the last node
Compiled & edited by: Zahid Zainal
Testing - 2

//from previous
LL1.removeFirst();
LL1.removeLast();
LL1.printLL();
System.out.println("***");
LL1.removeLast();
LL1.removeLast();
LL1.printLL();
}
}

Compiled & edited by: Zahid Zainal


Summary

 A list of items, called nodes, in which the order of the


nodes is determined by the address, called the link,
stored in each node.
 Linked list characteristics – Homogeneous, Unlimited side,
Sequential access.
 LinkedList class implementation
 Linked list operation – Create, Insert, Remove, Print

Compiled & edited by: Zahid Zainal


Next Topic…

 Linked list variation


 Doubly linked list
 Circular linked list

Compiled & edited by: Zahid Zainal


References

 Carrano, F. & Savitch, W. 2005. Data Structures and


Abstractions with Java, 2nd ed. Prentice-Hall.
 Malik D.S, & Nair P.S., Data Structures Using Java,
Thomson Course Technology, 2003.
 Rada Mihalcea, CSCE 3110 Data Structures and Algorithm
Analysis notes, U of North Texas.

Compiled & edited by: Zahid Zainal

You might also like