Professional Documents
Culture Documents
List (P.1) Array List and Singly Linked List: Data Structures and Algorithms
List (P.1) Array List and Singly Linked List: Data Structures and Algorithms
1)
Dept. Computer
Science
List (P.1)
Linear list concepts
Array List and Singly Linked List List ADT
Array implementation
How to store?
Implementation in C++
Comparison of
implementations
Dept. Computer Science
Faculty of Computer Science and Engineering
Ho Chi Minh University of Technology, VNU-HCM
List (P.1).1
List (P.1)
Overview
Dept. Computer
Science
4 Comparison of implementations
List (P.1).2
List (P.1)
Dept. Computer
Science
Array implementation
Comparison of
implementations
List (P.1).3
List (P.1)
Linear list concepts
Dept. Computer
Science
Definition
Linear list concepts
A linear list is a finite, ordered sequence of List ADT
Array implementation
data items known as elements. ”Ordered” in How to store?
Implementation in C++
this definition means that each element has a Singly linked list
Conceptual idea
Comparison of
implementations
List (P.1).4
List (P.1)
Linear list concepts
Dept. Computer
Science
Array implementation
How to store?
Implementation in C++
Comparison of
implementations
List (P.1).5
List (P.1)
Linear list concepts
Dept. Computer
Science
General list:
Comparison of
implementations
ticular order.
• Sorted list: data are arranged according to
a key.
List (P.1).6
List (P.1)
Linear list concepts
Dept. Computer
Science
Restricted list:
Linear list concepts
• Only some operations can be used on the List ADT
Array implementation
list. How to store?
Implementation in C++
Comparison of
• Queue: FIFO (First-In-First-Out). implementations
List (P.1).7
List (P.1)
List ADT
Dept. Computer
Science
Definition
Array implementation
Basic concepts: How to store?
Implementation in C++
ments. Operations
Comparison of
Basic operations:
List (P.1).9
List (P.1)
List ADT
Dept. Computer
Science
Extended operations:
•
Implementation in C++
List (P.1).10
List (P.1)
List ADT: Implementation in C++
Dept. Computer
Science
List (P.1).11
List (P.1)
Dept. Computer
Science
Array implementation
Comparison of
implementations
List (P.1).12
List (P.1)
Dynamically Allocated Array
Dept. Computer
Science
Array implementation
How to store?
Implementation in C++
List (P.1).13
List (P.1)
Array List: Implementation in C++ with Integer
Dept. Computer
Science
1 class IntArrayList : public IList < int > {
2 protected :
3 int * data ;
4 int capacity ;
5 int count ;
Linear list concepts
6 public : List ADT
7 IntArrayList ();
Array implementation
8 virtual ~ IntArrayList ();
How to store?
9 virtual void add ( int element ); Implementation in C++
10 virtual void add ( int index , int element );
Singly linked list
11 virtual int removeAt ( int index ); Conceptual idea
12 virtual bool removeItem ( int item ); Implementation in C++
List (P.1).14
List (P.1)
Array List: Implementation in C++ with Integer
Dept. Computer
Science
List (P.1).15
List (P.1)
Array List: Implementation in C++ with Integer
Dept. Computer
Science
List (P.1).16
List (P.1)
Array List: Implementation in C++ with Integer
Dept. Computer
Science
1 void IntArrayList :: add ( int element )
2 {
3 this - > ensureC apacity ( this - > count + 1);
4
5 this - > data [ this - > count ] = element ;
Linear list concepts
6 this - > count ++; List ADT
7 }
Array implementation
8
How to store?
9 void IntArrayList :: add ( int index , int element ) Implementation in C++
10 {
Singly linked list
11 this - > checkIndex ( index ); Conceptual idea
12 this - > ensureC apacity ( this - > count + 1); Implementation in C++
13 Operations
List (P.1).17
List (P.1)
Array List: Implementation in C++ with Integer
Dept. Computer
1 int IntArrayList :: removeAt ( int index ) { Science
2 this - > checkIndex ( index );
3 int e le me nt T oR em ov e = this - > data [ index ];
4
5 int moveCount = this - > count - index - 1;
6 if ( moveCount > 0) Linear list concepts
7 memmove ( this - > data + index , List ADT
13 } Implementation in C++
Operations
14
15 bool IntArrayList :: removeItem ( int element ) { Comparison of
implementations
16 for ( int index = 0; index < this - > count ; index ++)
17 {
18 if ( this - > data [ index ] == element ) {
19 this - > removeAt ( index );
20 return true ;
21 }
22 }
23 return false ;
24 }
List (P.1).18
List (P.1)
Array List: Implementation in C++ with Integer
Dept. Computer
Science
List (P.1).19
List (P.1)
Array List: Implementation in C++ with Integer
Dept. Computer
Science
1 int IntArrayList :: get ( int index ) {
2 this - > checkIndex ( index );
3
4 return this - > data [ index ];
5 }
Linear list concepts
6
List ADT
7 void IntArrayList :: set ( int index , int element ) {
Array implementation
8 this - > checkIndex ( index );
How to store?
9 Implementation in C++
10 this - > data [ index ] = element ;
Singly linked list
11 } Conceptual idea
12 Implementation in C++
14 for ( int index = 0; index < this - > count ; index ++) Comparison of
15 if ( this - > data [ index ] == element ) implementations
16 return index ;
17 return -1;
18 }
19
20 bool IntArrayList :: contains ( int element ) {
21 return this - > indexOf ( element ) != -1;
22 }
List (P.1).20
List (P.1)
Array List: Implementation in C++ with Integer
Dept. Computer
Science
7 if ( count > 0) ss << data [ count - 1] << " ] " ; Array implementation
8 else ss << " ] " ; How to store?
9 Implementation in C++
List (P.1).21
List (P.1)
Array List: Implementation in C++ with Integer
Dept. Computer
Science
List (P.1).22
List (P.1)
Contiguous Implementation of List
Dept. Computer
Science
Comparison of
implementations
List (P.1).23
List (P.1)
Dept. Computer
Science
Array implementation
Comparison of
implementations
List (P.1).24
List (P.1)
Linked List
Dept. Computer
Science
Array implementation
Figure: Singly Linked List How to store?
Implementation in C++
Comparison of
list // Linked Implementation of List implementations
head <pointer>
tail <pointer> // (optional)
count <integer> // number of elements (optional)
end list
List (P.1).25
List (P.1)
Nodes
Dept. Computer
Science
Definition
The elements in a linked list are called nodes.
A node in a linked list is a structure that has at least two
fields: Linear list concepts
List ADT
• the data,
Array implementation
• the address of the next node. How to store?
Implementation in C++
Comparison of
implementations
List (P.1).26
List (P.1)
Nodes
Dept. Computer
Science
List (P.1).27
List (P.1)
Example
Dept. Computer
Science
Array implementation
How to store?
Implementation in C++
Comparison of
implementations
List (P.1).28
List (P.1)
Example
Dept. Computer
Science
Array implementation
How to store?
Implementation in C++
Comparison of
implementations
List (P.1).29
List (P.1)
Implementation in C++
Dept. Computer
Science
Array implementation
How to store?
Implementation in C++
List (P.1).30
List (P.1)
Implementation in C++ with struct
Dept. Computer
Science
Example
Comparison of
1 template < class T > implementations
2 struct Node {
3 T data ;
4 Node <T > * next ;
5 };
List (P.1).31
List (P.1)
Node implementation in C++ with nested class
Dept. Computer
1 class Int SLinked List : public IList < int > { Science
2 public :
3 class Node ; // Forward d e c l a r a t i o n
4
5 protected :
6 Node * head ;
7 Node * tail ; Linear list concepts
List ADT
8 int count ;
9 Array implementation
How to store?
10 public :
Implementation in C++
11 IntSL inkedLi st () :
12 head ( NULL ) , tail ( NULL ) , count (0) {}; Singly linked list
Conceptual idea
13
Implementation in C++
14 public : Operations
Comparison of
• Destroy a linked list implementations
• ...
List (P.1).33
List (P.1)
Insertion: Implementation in C++
Dept. Computer
Science
List (P.1).34
List (P.1)
Insert an item into a linked list
Dept. Computer
Science
Array implementation
1 Case 1: Empty list How to store?
Implementation in C++
2 Case 2: Non-empty list
Singly linked list
• Prepend an item into a list Conceptual idea
List (P.1).35
List (P.1)
Insertion: Prepend to an empty list
Dept. Computer
Science
Array implementation
How to store?
Implementation in C++
18 Operations
Comparison of
implementations
pNew
List (P.1).36
List (P.1)
Insertion: Prepend to an empty list
Dept. Computer
Science
Array implementation
How to store?
Implementation in C++
18 Operations
Comparison of
implementations
pNew
List (P.1).36
List (P.1)
Insertion: Prepend to an empty list
Dept. Computer
Science
Array implementation
How to store?
Implementation in C++
18 Operations
Comparison of
implementations
pNew
List (P.1).36
List (P.1)
Insertion: Prepend to a non-empty list
Dept. Computer
Science
head tail
Linear list concepts
List ADT
Array implementation
How to store?
Implementation in C++
Comparison of
implementations
18
pNew
List (P.1).37
List (P.1)
Insertion: Prepend to a non-empty list
Dept. Computer
Science
head tail
Linear list concepts
List ADT
Array implementation
How to store?
Implementation in C++
Comparison of
implementations
18
pNew
List (P.1).37
List (P.1)
Insertion: Prepend to a non-empty list
Dept. Computer
Science
head tail
Linear list concepts
List ADT
Array implementation
How to store?
Implementation in C++
Comparison of
implementations
18
pNew
List (P.1).37
List (P.1)
Insertion: Append to the list
Dept. Computer
Science
head tail
Linear list concepts
List ADT
Array implementation
How to store?
Implementation in C++
Comparison of
implementations
18
pNew
List (P.1).38
List (P.1)
Insertion: Append to the list
Dept. Computer
Science
head tail
Linear list concepts
List ADT
Array implementation
How to store?
Implementation in C++
Comparison of
implementations
18
pNew
List (P.1).38
List (P.1)
Insertion: Append to the list
Dept. Computer
Science
head tail
Linear list concepts
List ADT
Array implementation
How to store?
Implementation in C++
Comparison of
implementations
18
pNew
List (P.1).38
List (P.1)
Insertion: At the index i
Dept. Computer
Science
Insert 18 at index 2.
head tail
Linear list concepts
List ADT
Array implementation
How to store?
Implementation in C++
Comparison of
implementations
18
pNew
List (P.1).39
List (P.1)
Insertion: At the index i
Dept. Computer
Science
Insert 18 at index 2.
head pre tail
Linear list concepts
List ADT
Array implementation
How to store?
Implementation in C++
Comparison of
implementations
18
pNew
List (P.1).39
List (P.1)
Insertion: At the index i
Dept. Computer
Science
Insert 18 at index 2.
head pre tail
Linear list concepts
List ADT
Array implementation
How to store?
Implementation in C++
Comparison of
implementations
18
pNew
List (P.1).39
List (P.1)
Insertion: At the index i
Dept. Computer
Science
Insert 18 at index 2.
head pre tail
Linear list concepts
List ADT
Array implementation
How to store?
Implementation in C++
Comparison of
implementations
18
pNew
List (P.1).39
List (P.1)
Removal: Implementation in C++
Dept. Computer
Science
7 }; Operations
Comparison of
implementations
List (P.1).40
List (P.1)
Removal: Delete the first element
Dept. Computer
Science
head tail
Linear list concepts
List ADT
Array implementation
How to store?
Implementation in C++
Comparison of
implementations
List (P.1).41
List (P.1)
Removal: Delete the first element
Dept. Computer
Science
head tail
Linear list concepts
List ADT
Array implementation
How to store?
Implementation in C++
Comparison of
implementations
temp
List (P.1).41
List (P.1)
Removal: Delete the first element
Dept. Computer
Science
head tail
Linear list concepts
List ADT
Array implementation
How to store?
Implementation in C++
Comparison of
implementations
temp
List (P.1).41
List (P.1)
Removal: Delete the first element
Dept. Computer
Science
head tail
Linear list concepts
List ADT
Array implementation
How to store?
Implementation in C++
Comparison of
implementations
temp
List (P.1).41
List (P.1)
Removal: Delete the first element
Dept. Computer
Science
head tail
Linear list concepts
List ADT
Array implementation
How to store?
Implementation in C++
Comparison of
implementations
List (P.1).41
List (P.1)
Removal: Delete the last element
Dept. Computer
Science
Array implementation
How to store?
Implementation in C++
25 6 47 Comparison of
implementations
List (P.1).42
List (P.1)
Removal: Delete the last element
Dept. Computer
Science
Array implementation
How to store?
Implementation in C++
25 6 47 Comparison of
implementations
List (P.1).42
List (P.1)
Removal: Delete the last element
Dept. Computer
Science
Array implementation
How to store?
Implementation in C++
25 6 47 Comparison of
implementations
List (P.1).42
List (P.1)
Removal: Delete the last element
Dept. Computer
Science
Array implementation
How to store?
Implementation in C++
25 6 Comparison of
implementations
List (P.1).42
List (P.1)
Removal: Delete at index i
Dept. Computer
Science
Array implementation
How to store?
Implementation in C++
25 6 47 Operations
Comparison of
implementations
List (P.1).43
List (P.1)
Removal: Delete at index i
Dept. Computer
Science
Array implementation
How to store?
Implementation in C++
25 6 47 Operations
Comparison of
implementations
List (P.1).43
List (P.1)
Removal: Delete at index i
Dept. Computer
Science
Array implementation
How to store?
Implementation in C++
25 6 47 Operations
Comparison of
implementations
List (P.1).43
List (P.1)
Removal: Delete at index i
Dept. Computer
Science
Array implementation
How to store?
Implementation in C++
25 6 47 Operations
Comparison of
implementations
List (P.1).43
List (P.1)
Removal: Delete at index i
Dept. Computer
Science
Array implementation
How to store?
Implementation in C++
25 47 Operations
Comparison of
implementations
List (P.1).43
List (P.1)
Search and other methods: Implementation in C++
Dept. Computer
Science
1 class Int SLinked List : public IList < int > { Linear list concepts
List ADT
2 // D e c l a r a t i o n of a t t r i b u t e s .
3 // D e c l a r a t i o n of constructor , d e s t r u c t o r . Array implementation
How to store?
4 // D e c l a r a t i o n of nested classes . Implementation in C++
5 public :
Singly linked list
6 virtual int get ( int index );
Conceptual idea
7 virtual void set ( int index , int element ); Implementation in C++
8 virtual int indexOf ( int item ); Operations
List (P.1).44
List (P.1)
Searching
Dept. Computer
Science
Array implementation
head tail How to store?
Implementation in C++
Comparison of
implementations
25 6 47
List (P.1).45
List (P.1)
Searching
Dept. Computer
Science
Comparison of
implementations
25 6 47
List (P.1).45
List (P.1)
Searching
Dept. Computer
Science
Comparison of
implementations
25 6 47
List (P.1).45
List (P.1)
Searching
Dept. Computer
Science
Comparison of
implementations
25 6 47
List (P.1).45
List (P.1)
Dept. Computer
Science
Comparison of
implementations
List (P.1).46
List (P.1)
Arrays: Pros and Cons
Dept. Computer
Science
• Pros:
• Access to an array element is fast since
we can compute its location quickly. Linear list concepts
List ADT
Array implementation
Comparison of
ements which slows our computation implementations
down.
• We need a large enough block of mem-
ory to hold our array.
List (P.1).47
List (P.1)
Linked Lists: Pros and Cons
Dept. Computer
Science
• Pros:
• Inserting and deleting data does not re- Linear list concepts
Array implementation
elements. How to store?
Implementation in C++
Comparison of
implementations
we need to traverse the list from the
head of the list to find it which can
take longer than an array access.
List (P.1).48
List (P.1)
Comparison of implementations of list
Dept. Computer
Science
List (P.1).49
List (P.1)
Dept. Computer
Science
Array implementation
Comparison of
implementations
List (P.1).50