CS611 - L03 - Linked Lists - Saleh
CS611 - L03 - Linked Lists - Saleh
Linked Lists
List ADT
2
What is a List?
Ordered sequence of elements A1, A2, …, AN
Elements may be of arbitrary type, but all are
of the same type
Common List operations are:
Insert,
Find, Delete, IsEmpty, IsLast,
FindPrevious, First, Kth, Last, Print, etc.
Simple Examples of List Use
3
Polynomials
25 + 4x2 + 75x85
Unbounded Integers
4576809099383658390187457649494578
Text
“This is an example of text”
List Implementations
4
Pointer-Based
List: Array Implementation
5
Basic Idea:
Pre-allocate a big array of size MAX_SIZE
Keep track of current size using a variable count
Shift elements when you have to insert or delete
0 1 2 3 … count-1 MAX_SIZE-1
A1 A2 A3 A4 … AN
List: Array Implementation
6
0 1 2 3 4 5 MAX_SIZE-1
A B C D E F
0 1 2 3 4 5 6 MAX_SIZE-1
A B Z C D E F
Array List Insert Running Time
7
Basic Idea:
Allocate little blocks of memory (nodes) as
elements are added to the list
Keep track of list by linking the nodes together
L node node
Value Next Value Next
NULL
Preliminaries
A lost cell
Displaying the Contents of a Linked List
4-18
Inserting a Node into a Specified Position of a Linked List
4-19
Inserting a Node into a Specified Position of a Linked List
4-20
Inserting a Node into a Specified Position of a Linked List
find prev
Constructors and Destructors
Default constructor initializes size and head
Copy constructor allows a deep copy
Copies the array of list items and the number of
items
A destructor is required for dynamically
allocated memory
Comparing Array-Based and Pointer-Based Implementations
Size
Increasing
the size of a resizable array can waste
storage and time
Storage requirements
Array-based
implementations require less
memory than a pointer-based ones
Comparing Array-Based and Pointer-Based Implementations
Access time
Array-based: constant access time
Pointer-based: the time to access the ith node
depends on i
Insertion and deletions
Array-based: require shifting of data
Pointer-based: require a list traversal
Saving and Restoring a Linked List by Using a File
4-30
Doubly Linked Lists
Each node points to both its predecessor and its successor
Circular doubly linked list
precede pointer of the dummy head node points to the last node
next reference of the last node points to the dummy head node