Linked List ADT

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 27

In this Lecture we will

• Learn about linked lists


• Become aware of the basic properties of linked lists
• Explore the insertion and deletion operations on linked
lists
• Discover how to build and manipulate a linked list

1
Introduction
Data can be organized and processed sequentially
using an array, called a sequential list
Problems with an array
Array size is fixed
Unsorted array: searching for an item is slow
Sorted array: insertion and deletion is slow

2
Singly link list
All the nodes in a singly linked list are arranged
sequentially by linking with a pointer.
A singly linked list can grow or shrink, because it is a
dynamic data structure.

3
figure explains the different
operations on a singly linked list.

4
Insertion and Deletion

5
Linked Lists
Linked list: a list of items (nodes), in which the order
of the nodes is determined by the address, called the
link, stored in each node

Link field in last node


is NULL
6
Linked Lists (cont'd.)
Because each node of a linked list has two components,
we need to declare each node as a class or struct
Data type of a node depends on the specific application
The link component of each node is a pointer

7
Linked Lists: Some Properties

8
Linked Lists: Some Properties (cont'd.)
current = head;
Copies value of head into current

9
Linked Lists: Some Properties (cont'd.)
current = current->link;

10
Traversing a Linked List
The basic operations of a linked list are:
Search to determine if an item is in the list
Insert an item in the list
Delete an item from the list
Traversal: given a pointer to the first node of the list,
step through the nodes of the list

11
Traversing a Linked List (cont'd.)
To traverse a linked list:

Example:

12
Item Insertion and Deletion
Consider the following definition of a node:

We will use the following variable declaration:

13
Insertion
Consider the following linked list:

A new node with info 50 is to be created and


inserted after p

14
Insertion (cont'd.)

15
Insertion (cont'd.)
Using two pointers, we can simplify the insertion code
somewhat

To insert newNode between p and q:

The order in which these statements


execute does not matter

16
Insertion (cont'd.)

17
Deletion

Node with info 34 is removed from the list, but


memory is still occupied; node is dangling
18
Deletion (cont’d.)

19
Building a Linked List
If data is unsorted
The list will be unsorted
Can build a linked list forward or backward
Forward: a new node is always inserted at the end of the
linked list
Backward: a new node is always inserted at the
beginning of the list

20
Building a Linked List Forward
You need three pointers to build the list:
One to point to the first node in the list, which cannot be
moved
One to point to the last node in the list
One to create the new node

21
Building a Linked List Forward
(cont’d.)

22
Building a Linked List Forward
(cont’d.)

23
Building a Linked List Forward
(cont'd.)

We now repeat statements 1 through 6b three more times:

24
Building a Linked List Forward
(cont’d.)

25
Building a Linked List Backward
The algorithm is:
Initialize first to NULL
For each item in the list
 Create the new node, newNode
 Store the item in newNode
 Insert newNode before first
 Update the value of the pointer first

26
Building a Linked List Backward
(cont’d.)

27

You might also like