Professional Documents
Culture Documents
Chapter 3 - Linked List
Chapter 3 - Linked List
LINKED LIST
FUNDAMENTALS OF DATA STRUCTURES (CSC248)
PREPARED BY:
MISS MAZNIE MANAF
TYPE OF LISTS
Linked List
Linked structure – store elements at anywhere in memory and linked by a
reference/pointer
head
null
WHAT IS 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.
Data in a linked list are stored in nodes which are not necessarily in contiguous
memory locations
Every node in a linked list has two components:
one to store relevant information (the data)
one to store address (the link) of next node in list
Address of first node in list stored in separate location, called the head or first
Link component of each node is a reference variable
THE STRUCTURE
null
Reference link
head First node Last node
Insertions and removals can be made without moving any elements; only
the links are altered.
There are 6 methods which are not available in the arraylist class:
public boolean addFirst( Object element)
public boolean addLast(Object element)
public Object getFirst()
public Object getLast()
public Object removeFirst()
public Object removeLast()
EXERCISE 1
Given the details of the nodes in a linked list named infoList.
Node Address of Node Data Next
1 B2300C AA B2445D
2 B2445D BB B2500C
3 B2500C CC C2555F
4 C2555F DD null
a) Draw a diagram to represent the initial linked list. e) Insert a new node at front with the values (YY, B2300C).
b) Insert a new node at front with the values (XX, B2300C). f) Insert a new node at the back with the values (ZA, null).
c) Insert a new node at the back with the values (ZZ, null). Address ZA (C2553C).
Address ZZ (C2552C) g) Draw the latest diagram.
d) Delete the first element
EXERCISE 2
first last
17 2800 92 1500 63 3600 45 null
Value
first.data
first.next
first.next.data
first.next.next
CONCEPT IN VARIATION OF
LINKED LIST
TYPES OF LINKED LIST
If each Entry object also includes a link to the Entry object that holds the previous
element in the list, we have a doubly linked list.
DOUBLY LINKED LIST CONTINUE
Every node:
has a next reference variable and a back reference variable
(except the last node) contains the address of the next node
(except the first node) contains the address of the previous node
A linked list in which the last node points to the first node is called a circular linked
list
CIRCULARLY LISTS CONTINUE
Advantage: can traverse in forward or reverse direction even after you have
passed the last or first node
Can visit all the list elements from any starting point
Can never fall off the end of a list
Disadvantage: infinite loop!
IMPLEMENTATION OF LINKED
LIST
IMPLEMENTATION OF LINKED LIST
JAVA provides class LinkedList for implementing and manipulating linked list in
JAVA package –(java.util.*)
Programmer can also create self-defined linked list by defining own linked list
class to represent the node and linked list structure.
For this course, we will create our own linked list class using singly linked list
THE LINKED LIST GENERIC CLASS
Declaration Creating
LinkedList theList; theList = new LinkedList();
LinkedList <String> listname; listname = new LinkedList();
LinkedList <Integer> numberList; numberList = new LinkedList();
LinkedList <Student> studentList; studentList = new LinkedList();
INSERTING
L A B C
L x A B C
Inserted value
DELETING
L A B C
L A B
SEARCHING
Assume that you are given TWO nodes. Node 1 contains the integer value
of 1 and node 2 contains the integer value 2. If you are going to create a
linked list of these two nodes in order, draw the diagram to show the
structure of the linked list with head and tail.
Then, draw a complete diagram to show the contents of the linked list after
each of the following operations:
Insert three(3) integers at the front, which are 8, 10 and 66.
Insert another three (3) integers at the back, which are 68, 3 and 1.
Remove one(1) element from the front.
Remove two(2) elements from the back.
Remove all even integers.
APPLICATION OF LINKED LIST
USER-DEFINED LINKED LIST class Node
Node object
data next
data type (primitive or ADT) store reference link to next node
Class : Node
Fields/Data : data, next;
Methods : Node ( Object obj ) // default constructor
Node( Object obj, ListNode nextNode ) // normal constructor
Object getObject() // return the data
Node getNext() // return link/reference to next data
USER-DEFINED LINKED LIST class Node
Class : LinkedList
Fields: first //first node
last //last node
current //current node
Methods :
Constructor (default & normal)
boolean isEmpty() // check whether list is empty
void insertAtFront(object) // insert at the front of list
void insertAtBack(object) // insert at the end of list
Object removeFromFront() // delete element from front
Object removeFromBack() // delete element from back
Object getFirst() // get the first node
Object getNext() // get the reference for next node
USER-DIFINED LINKED LIST class LinkedList
Based on user-define LinkedList in previous slide, write the main application to:
create an empty linked list named theLink
add some data into the linked list
Value 7
Value 3 at front
Value 12 at back
Value 6 at back
Value 15 at back
Value 4 at front (*Show the logical diagram*)
display all the data in the linked list (using getFirst()and getNext())
Calculate the average of the data in the linked list
to delete the first and last data. Display an appropriate message if list is empty (*Show the logical
diagram*)