0% found this document useful (0 votes)
5 views9 pages

1.3 Linked List

lecture note of CSC508 data structure

Uploaded by

2023217748
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)
5 views9 pages

1.3 Linked List

lecture note of CSC508 data structure

Uploaded by

2023217748
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/ 9

LINKED LIST

TOPIC 1: LIST

Prepared by: Nik Marsyahariani Nik Daud

Content

Linked List Concept


Differences with ArrayList
LinkedList in Collection Frameworks
Application using LinkedList

Implementing User Defined Linked List


Application using User Defined Linked List

1
Linked List Concept

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.

Every node in a linked list has two components:


• one to store data
• one to store address (the link) of next node in list

Node

Linked List Concept

▪ Address of first node in list stored in separate location, called


the head or first
▪ Head should always point to the first node
▪ Data type for the data component can be anything, depending on
the application
▪ Link component of each node is a variable of reference data type
▪ Data type of this reference variable is node ADT itself

2
Linked List Concept

• Program accesses a linked list via a reference to the first node in list
• Program accesses a subsequent nodes via the reference link stored in
the previous node
• Reference in the last node of a list is set to null

Reference link
Firstnode Lastnode

……
null

Linked List Concept

3
Linked List Concept

LinkedList Advantages

• Appropriate data structure:


• When the number of data is unpredictable

• Dynamic data structure:


• The length of a list can increase or decrease as necessary

• Simple to maintain in sorted order:


• By inserting each new element at proper point in the list

4
Differences with Array

ArrayList LinkedList
Fast access to elements Slower access to elements

Slower insertion/deletion of Faster insertion / deletion of


elements elements

10

LinkedList in Collection Framework

• JAVA provide class LinkedList for implementing and manipulating linked


list in JAVA package (java.util.*)

• Linked list class in JAVA uses doubly linked list implementation

11

5
LinkedList in Collection Framework

12

LinkedList Application

import java.util.LinkedList;
class app{
public static void main(String a[]){
LinkedList<Integer> lsNum = new LinkedList<>();
lsNum.add(10);
lsNum.add(20);
lsNum.addFirst(5);
System.out.println(lsNum);
int num = lsNum.get(1);
num = num+2;
lsNum.add(num);
System.out.println(lsNum);

13

6
User Defined Linked List

• Programmer can also create self-defined linked list by defining their


own classes to represent the node and linked list structure.
• Identify properties for both node and linkedlist concepts.
• Identify behavior for both node and linkedlist concepts.

15

Linked List Properties

Node Properties
• A node must have data
• A node must have a reference to another node

LinkedList Properties
• Linked list must have a head (the first node)
• Linked list may have tail(the last node)
• Traversing linked list started from head to tail (moving from one node to
the next one) using a next node (next node)

16

7
LinkedList Operations

▪ Search the list to determine whether a particular item is in the list.


▪ Insert an item in the list
▪ Insert an item as the first element in the list
▪ Insert an item at the end of the list
▪ Delete an item from the list
▪ Delete the first item from the list
▪ Delete the last item from the list

17

LinkedList Structure
• Class: LinkedList

• Attributes
first node
last node
current node //use to traverse the list
• Methods
constructor
boolean isEmpty() //check whether list is empty
void insertAtFront(Object data) //insert at front of list
void insertAtBack(Object data) // insert at the end of list
Object removeFromFront() //delete element form front
Object removeFromBack() //delete element form back
Object getFirst() //get the first node
Object getNext() //get the reference for the next node

18

8
Summary

List Concepts
List in Collection Frameworks

Sequential list Concept


Differences with Array
ArrayList in Collection Frameworks
Application using ArrayList

Implementing User Defined Sequential List


Application using User Defined Sequential List

19

You might also like