Link List & their Operations-Data Structure

SayantanDass 12 views 20 slides Oct 20, 2024
Slide 1
Slide 1 of 20
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20

About This Presentation

Link List


Slide Content

10/20/24 1
List Operations
15-111
Advanced Programming
Ananda Gunawardena

10/20/24 2
Advantages of Linked Lists
•The advantages of linked lists include:
–Overflow can never occur unless the memory
is actually full.
–Insertions and deletions are easier than for
contiguous (array) lists.
–With large records, moving pointers is easier
and faster than moving the items themselves.

10/20/24 3
Disadvantage of Linked Lists
•The disadvantages of linked lists include:
–The pointers require extra space.
–Linked lists do not allow random access.
–Time must be spent traversing and changing
the pointers.
–Programming is typically trickier with pointers.

10/20/24 4
Linked List Operations

10/20/24 5
List Operations
•Basic Operations on a Linked List are
–Insertion
–Deletion
•Other operations include
– build list
–Append and prepend (special cases of insert)
–empty
–length
–toString
–traversing

10/20/24 6
List Traversal
head
null

10/20/24 7
Insertion
•Two cases to consider
–List is empty
•Insert to the beginning of the list
–List is non-empty
•Locate the place to insert
•Manipulate the references to make new links
head

10/20/24 8
Insert Operation
P
New node

10/20/24 9
Delete Operation
P
Node

10/20/24 10
Other Operations

10/20/24 11
Prepend
head
New node
Insert to the beginning of the list
null

10/20/24 12
Append
head
New node
Insert to the end of the list
null

10/20/24 13
Length
head
Insert to the end of the list
null

10/20/24 14
List Types

10/20/24 15
Doubly Linked Lists
•Typically a linked list with two pointers (next and
previous) is called a doubly linked list
class Node {
Object data;
Node next;
Node previous;
…..
}
head

10/20/24 16
Inserting into a Doubly Linked List
•We have to manipulate both next and
previous pointers. Insert new node N
between P and Q
Node P Node Q Node R
Node N

10/20/24 17
Deleting from a Doubly Linked List
•Write the code to delete Node Q
Node P Node Q Node R

10/20/24 18
Building a Doubly Linked List
•Build a doubly linked list with 3 nodes (always insert to the front of the list)
•Node Head = new Node(new Integer(3), null, null);
•Node N = new Node(new Integer(4), null, null);
•Connect the two nodes
•Insert the next node to the list

10/20/24 19
Make it a Circular Doubly Linked
List
•Write the code to create a circular doubly
linked list.

10/20/24 20
Multi Linked List - An
Example
Tags