Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

CHAPTER 3

LINKED LIST
FUNDAMENTALS OF DATA STRUCTURES (CSC248)

PREPARED BY:
MISS MAZNIE MANAF
TYPE OF LISTS

 Sequential List (Array List)


Already discuss in
 Arrays structure – store elements in a contiguous memory previous chapter

[0] [1] [2] [3] [4]

 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

Structure of a node Structure of a linked list

Each node has two components:


head node 1 node 2

null

The address of the first node in a linked list is stored in the


reference variable head
data next
head should always point to the first node
BASIC LINKED LIST CONCEPT
SOME PROPERTIES

 Linked list basic operations:


 Search the list to determine whether a particular item is in the list
 Insert an item in the list
 Delete an item from the list
 Operations require traversal of the list
 Given a reference variable to the first node of the list, step through each of the
nodes of the list
 Traverse a list using a reference variable of the same type as head
LOGICAL VIEW OF LINKED LIST

Reference link
head First node Last node

2000 MIA 2800 ALI 1500 ITA 3600 EDI null


Address: 2000 Address: 2800 Address: 1500 Address: 3600
ADVANTAGES OF LINKED LISTS

 Linked lists are dynamic


 The list can grow or shrink as necessary
 Linked lists can be maintained in sorted order simply by inserting each new
element at the proper point in the list
 Existing list elements do not need to be moved
 Linked lists are non-contiguous
 The logical sequence of the items in the structure is decoupled from any
physical ordering in memory
ARRAY LIST VS LINKED LIST

 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

There are 3 types of linked list:


a) singly linked list
b) doubly linked list
c) circularly linked list
SINGLY LINKED LIST

Each element is contained in an object, called an Entry object, that also


includes a reference, called a link, that holds the next element in the list.
SINGLY LINKED LIST INSERTING &
REMOVING
DOUBLY 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

 Can be traversed in either direction


DOUBLY LINKED LIST INSERTING
DOUBLY LINKED LIST REMOVING
CIRCULARLY LISTS

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.*)

 LinkedList class in JAVA uses a doubly linked list implementation.

 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

void addFirst(object)// insert at the front of list


 Returns the first element in this list. Throws NoSuchElementException if
this list is empty.

void addLast(object) // insert at the end of list


 Returns the last element in this list. Throws NoSuchElementException if
this list is empty.
INSERTING

 adds one node into a linked list


Before

L A B C

add the value X to list L at first position,


L.addFirst(X)
shifting subsequent elements up
After

L x A B C

Inserted value
DELETING

Object removeFirst() // delete element from front


 Removes and returns the first element from this list. Throws
NoSuchElementException if this list is empty.

Object removeLast() // delete element from back


 Removes and returns the last element from this list. Throws
NoSuchElementException if this list is empty.
DELETING

 adds one node into a linked list


 Return: the removed element
Before

L A B C

Remove the list L at last position,


L.removeLast()
shifting subsequent elements down
After

L A B
SEARCHING

Object getFirst() // get the element first node


 Returns the first element in this list. Throws NoSuchElementException if
this list is empty.

Object getLast() // get the element for last node


 Returns the last element in this list. Throws NoSuchElementException if
this list is empty.
EXERCISE 3

 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

public class Node {


Object data; // data
Node next; // reference to next Node
public Node( Object obj ) { // default constructor
data = obj;
next = null;
}
public Node( Object obj, Node nextNode ) { // normal constructor
data = obj;
next = nextNode;
}
Object getObject()
{ return data; }
Node getNext()
{ return next; }
}
USER-DEFINED LINKED LIST class LinkedList

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

public class LinkedList {


private Node first;
private Node last;
private Node current;
//default constructor
//check if the list is empty
//insert an item at the beginning of the list
//insert an item at the end of the list
//delete an item at the beginning of the list
//delete an item at the end of the list
//retrieve the first item in the list Complete example of
//retrieve the next item in the list program, please refer to
“example LinkedList.pdf”
EXERCISE 4

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*)

You might also like