0% found this document useful (0 votes)
2 views

Lecture 11-Linked Lists

The document discusses linked lists as a data structure consisting of nodes arranged in a linear order, with each node containing a reference to the next node. It covers basic operations, implementations, and the differences between singly and doubly linked lists, including their advantages and disadvantages. Additionally, it outlines how linked lists can be used to implement stacks and queues, as well as the concept of sentinel nodes in doubly linked lists.

Uploaded by

pythonpooja2
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture 11-Linked Lists

The document discusses linked lists as a data structure consisting of nodes arranged in a linear order, with each node containing a reference to the next node. It covers basic operations, implementations, and the differences between singly and doubly linked lists, including their advantages and disadvantages. Additionally, it outlines how linked lists can be used to implement stacks and queues, as well as the concept of sentinel nodes in doubly linked lists.

Uploaded by

pythonpooja2
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Linked lists

LISTS
Consider Every Day Lists
Groceries to be purchased
Job to-do list
List of assignments for a course
Dean's list

Can you name some others??

3 Nyhoff, ADTs, Data Structures and Problem Solving


with C++, Second Edition, © 2005 Pearson Educatio
n, Inc. All rights reserved. 0-13-140909-3
Properties of Lists
Can have a single element
Can have no elements
There can be lists of lists

We will look at the list as an abstract data


type
Homogeneous
Finite length
Sequential elements
 There is a first element and a last element
 Every element has a unique successor and a unique
predecessor except the first and last elements

4
Basic Operations
Construct an empty list
Determine whether or not empty
Insert an element into the list
Delete an element from the list
Traverse (iterate through) the list to
Modify
Output
Search for a specific value
Copy or save
Rearrange

5
Designing a List Class
Should contain at least the following
function members

Isempty()
insert()
delete()
display()
Implementation involves
Defining data members
Defining function members from design
phase
6
Array-Based Implementation
of Lists

An array is a viable choice for


storing list elements
Element are sequential
It is a commonly available data type
Algorithm development is easy
Normally sequential orderings of list
elements match with array elements

7 Nyhoff, ADTs, Data Structures and Problem Solving


with C++, Second Edition, © 2005 Pearson Educatio
n, Inc. All rights reserved. 0-13-140909-3
Implementing Operations
 Constructor
 Static array allocated at compile time
 IsEmpty
 Check if size == 1
 Traverse
 Use a loop from 0th element to size – 1
 Insert
 Shift elements to
right of insertion point Also
Also
 Delete adjust
adjust
sizeup
size up
 Shift elements back or down
or down

8
How much time for each operation, if
the list is implemented using an array?
LINKED LISTS
A linked list is a data structure in
which the objects are arranged in a
linear order.
Unlike an array, however, in which the
linear order is determined by the array
indices, the order in a linked list is
determined by a pointer in each object.
Linked lists
The linked list uses dynamic memory
allocation, that
is, it allocates memory for new list
elements as needed.
A linked list is made up of a series of
objects, called the nodes of the list.
Because a node is a distinct object (as
opposed to simply a cell in an array), it is
good practice to make a separate list node
class.

An additional benefit to creating a node


class is that it can be reused by the linked
list implementations for the stack
and queue data structures.
Singly Linked Lists
 A singly linked list is a
concrete data structure next
consisting of a
sequence of nodes
 Each node stores
 element elem node
 link to the next node

A B C D
14 Linked Lists
D e fin itio n : A lin k e d lis t is a c o lle c ito n o f n o d e s th a t
to g e th e r fo rm a lin e a r o rd e rin g .

n o d e : A c o m p o u n d o b je c t th a t s to re s a re fe re n c e to a n
e le m e n t a n d a re fe re n c e , c a lle d n e x t, to a n o th e r n o d e .

E le m e n t

N ode
R e fe re n c e to a n
e le m e n t
R e fe re n c e to next
a n o th e r n o d e
head

next next next next

e le m e n t e le m e n t e le m e n t e le m e n t
B a lt im o r e R om e S e a tt le T o ro n to

lin k : T h e n e x t re fe re n c e in s id e a n o d e is a lin k o r p o in te r to
a n o th e r n o d e .
W e c a n s ta rt fro m a g iv e n n o d e , a n d m o v e fro m it to th e n e x t
a n d s o o n . T h is is c a lle d lin k h o p p in g o r p o in te r h o p p in g .

head

next next next next

e le m e n t e le m e n t e le m e n t e le m e n t
B a lt im o r e R om e S e a tt le T o ro n to
h e a d : T h e firs t n o d e o f a lin k e d lis t
ta il: T h e la s t n o d e o f a lin k e d lis t - it h a s a n u ll n e x t re fe re n c e .

head tail

next next next next

e le m e n t e le m e n t e le m e n t e le m e n t
B a lt im o r e R om e S e a tt le T o ro n to

S u c h a lin k e d lis t is c a lle d a s in g ly lin k e d lis t .


Illustration of a linked list in memory:

50B0

5110 Toronto

50A0

5100
5090 node pointer to
next node
5070 50F0
5080 50D0

Rome pointer to
50E0 an elemen
0
5070 5110
Seattle
5080 50D0
5060 50E0
Baltimore
5060 50C0
50C0
5050
50B0

5110 Toronto

50A0

5100
5090 node pointer to a
next node
5070 50F0
5080 50D0
pointer to
50E0 Rome an element
0
5070 5110
Seattle
5080 50D0
5060 50E0
Baltimore
5060 50C0
50C0
5050
head
50B0

5110 Toronto

50A0

5100
5090
node pointer to a
5070 50F0
next node
5080 50D0

50E0 Rome pointer to


0
5110 an element
5070
Seattle
5080 50D0
5060 50E0

Baltimore
5060 50C0
50C0
5050
head
50B0

5110 Toronto

50A0

5100
5090 pointer to a
node
50F0 next node
5070
5080 50D0
pointer to
50E0 Rome
0 an element
5070 5110
Seattle
5080 50D0
5060 50E0
Baltimore
5060 50C0
50C0
5050
head
50B0

5110 Toronto

50A0

5100
5090

5070 50F0
5080 50D0

50E0 Rome
0
5070 5110
Seattle
5080 50D0
5060 50E0

Baltimore
5060 50C0
50C0
5050
head
Inserting at the Head

1. Allocate a new
node
2. Insert new
element
3. Make new node
point to old head
4. Update head to
point to new node

24 Linked Lists
Removing at the Head
1. Update head to
point to next
node in the list
2. Allow garbage
collector to
reclaim the
former first node

Linked Lists 25
Inserting at the Tail
1. Allocate a new
node
2. Insert new
element
3. Have new node
point to null
4. Have old last
node point to
new node
5. Update tail to
point to new
Linked Lists 26
Removing at the Tail
Removing at the
tail of a singly
linked list will not
be efficient!
There is no
constant-time way
to update the tail
to point to the
previous node

Linked Lists 27
Stack with a Singly Linked List
 We can implement a stack with a singly linked list
 The top element is stored at the first node of the list
 The space used is O(n) and each operation of the
Stack ADT takes O(1) time

nodes

t 

elements
28 Linked Lists
Queue with a Singly Linked List
 We can implement a queue with a singly linked list
 The front element is stored at the first node
 The rear element is stored at the last node
 The space used is O(n) and each operation of the
Queue ADT takes O(1) time
r
nodes

f

elements
29 Linked Lists
Doubly Linked List
 A doubly linked list is often more
convenient! prev next
 Nodes store:
 element
 link to the previous node
 link to the next node
 Special trailer and header nodes elem node
which are sentinels or dummy
nodes (they don’t store any element

header nodes/positions trailer

elements
30 Linked Lists
Insertion
 We visualize operation insertAfter(p, X), which returns position q
p

A B C

A B q C

X
p q

A B X C
31 Linked Lists
Deletion
 We visualize remove(p), where p == last()
p

A B C D

A B C p

A B C
32 Linked Lists
Sentinel Nodes
To simplify programming, two special nodes
have been added at both ends of the doubly-
linked list.
Head and tail are dummy nodes, also called
sentinels, do not store any data elements.
Head: header sentinel has a null-prev reference
(link).
Tail: trailer sentinel has a null-next reference
(link).

33 Dr Zeinab Eid
What we see from a Douby-
linked List?
A doubly-linked list object would need to
store the following:
1. Reference to sentinel head-node;
2. Reference to sentinel tail-node; and
3. Size-counter that keeps track of the
number of nodes in the list (excluding the
two sentinels).

34 Dr Zeinab Eid
Empty Doubly-Linked
List: header trailer
Using sentinels, we have no
null-links; instead, we have:
head.next = tail
tail.prev = head firs las
t t
Singl Node List: header trailer
Size = 1
This single node is the first
node, and also is the last
node:
first node is head.next
35 last node is tail.prev Dr Zeinab Eid
Worst-cast running time
In a doubly linked list
+ insertion at head or tail is in O(1)
+ deletion at either end is on O(1)
-- element access is still in O(n)…..we need to
search for the element i.e. ‘key’

36 Linked Lists
Implement the following data
types with a doubly linked list
Stack
Queue
Circular lists (no head or tail –just an entry
point)

You might also like