List (P.1) Array List and Singly Linked List: Data Structures and Algorithms

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 73

List (P.

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++

Data Structures and Algorithms Singly linked list


Conceptual idea
Implementation in C++
Operations

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

1 Linear list concepts


List ADT
Linear list concepts
List ADT
2 Array implementation
Array implementation
How to store? How to store?
Implementation in C++
Implementation in C++
Singly linked list
Conceptual idea
Implementation in C++
3 Singly linked list Operations

Conceptual idea Comparison of


implementations
Implementation in C++
Operations

4 Comparison of implementations

List (P.1).2
List (P.1)

Dept. Computer
Science

Linear list concepts


List ADT

Array implementation

Linear list concepts How to store?


Implementation in C++

Singly linked list


Conceptual idea
Implementation in C++
Operations

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

position in the list. Implementation in C++


Operations

Comparison of
implementations

List (P.1).4
List (P.1)
Linear list concepts
Dept. Computer
Science

Linear list concepts


List ADT

Array implementation
How to store?
Implementation in C++

Singly linked list


Conceptual idea
Implementation in C++
Operations

Comparison of
implementations

List (P.1).5
List (P.1)
Linear list concepts
Dept. Computer
Science

General list:

• No restrictions on which operation can be Linear list concepts


List ADT
used on the list. Array implementation

• No restrictions on where data can be in- How to store?


Implementation in C++

Singly linked list


serted/deleted. Conceptual idea
Implementation in C++

• Unsorted list: data are not arranged in par- Operations

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++

• Data can be inserted/deleted only at the Singly linked list


Conceptual idea

ends of the list. Implementation in C++


Operations

Comparison of
• Queue: FIFO (First-In-First-Out). implementations

• Stack: LIFO (Last-In-First-Out).

List (P.1).7
List (P.1)
List ADT
Dept. Computer
Science

Definition

A list of elements of type T is a finite, ordered


sequence of elements of T. Linear list concepts
List ADT

Array implementation
Basic concepts: How to store?
Implementation in C++

Singly linked list


• A list is empty when it contains no ele- Conceptual idea
Implementation in C++

ments. Operations

Comparison of

• The number of elements currently stored is implementations

called the length (size) of the list.


• The beginning of the list is called the head,
the end of the list is called the tail.
List (P.1).8
List (P.1)
List ADT
Dept. Computer
Science

Basic operations:

• Construct a list, leaving it empty. Linear list concepts


List ADT

• Insert an element. Array implementation


How to store?
Implementation in C++

• Remove an element. Singly linked list


Conceptual idea

• Search an element. Implementation in C++


Operations

• Retrieve an element. Comparison of


implementations

• Traverse the list, performing a given oper-


ation on each element.

List (P.1).9
List (P.1)
List ADT
Dept. Computer
Science

Extended operations:

• Determine whether the list is empty or not. Linear list concepts


List ADT

• Determine whether the list is full or not. Array implementation


How to store?


Implementation in C++

Find the size of the list. Singly linked list


Conceptual idea

• Clear the list to make it empty. Implementation in C++


Operations

• Replace an element with another element. Comparison of


implementations

• Merge two ordered list.


• Append an unordered list to another.

List (P.1).10
List (P.1)
List ADT: Implementation in C++
Dept. Computer
Science

1 template < class T >


2 class IList
3 {
Linear list concepts
4 public : List ADT
5 virtual void add ( T e ) = 0;
Array implementation
6 virtual void add ( int index , T e ) = 0;
How to store?
7 virtual T removeAt ( int index ) = 0; Implementation in C++
8 virtual bool removeItem ( T item ) = 0;
Singly linked list
9 virtual bool empty () = 0; Conceptual idea
10 virtual int size () = 0; Implementation in C++

11 virtual void clear () = 0; Operations

12 virtual T get ( int index ) = 0; Comparison of


13 virtual void set ( int index , T e ) = 0; implementations
14 virtual int indexOf ( T item ) = 0;
15 virtual bool contains ( T item ) = 0;
16 virtual string toString () = 0;
17 };

List (P.1).11
List (P.1)

Dept. Computer
Science

Linear list concepts


List ADT

Array implementation

Array implementation How to store?


Implementation in C++

Singly linked list


Conceptual idea
Implementation in C++
Operations

Comparison of
implementations

List (P.1).12
List (P.1)
Dynamically Allocated Array
Dept. Computer
Science

Linear list concepts


List ADT

Array implementation
How to store?
Implementation in C++

Singly linked list


Conceptual idea
Implementation in C++
1 List // C o n t i g u o u s I m p l e m e n t a t i o n of List Operations

2 // number of used e l e m e n t s ( m a n d a t o r y ) Comparison of


3 count < integer > implementations
4
5 // ( D y n a m i c a l l y A l l o c a t e d Array )
6 data < Array List of < DataType > >
7
8 capacity < integer >
9 End List

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++

13 virtual bool empty (); Operations

14 virtual int size (); Comparison of


15 virtual void clear (); implementations
16 virtual int get ( int index );
17 virtual void set ( int index , int element );
18 virtual int indexOf ( int item );
19 virtual bool contains ( int item );
20 virtual string toString ();
21 virtual void dump ();
22 };

List (P.1).14
List (P.1)
Array List: Implementation in C++ with Integer
Dept. Computer
Science

Linear list concepts


List ADT
1 class IntArrayList : public IList < int >
2 { Array implementation
3 // ... How to store?
Implementation in C++
4
5 private : Singly linked list
Conceptual idea
6 void checkIndex ( int index );
Implementation in C++
7 void ensureCa pacity ( int capacity ); Operations
8 };
Comparison of
implementations

List (P.1).15
List (P.1)
Array List: Implementation in C++ with Integer
Dept. Computer
Science

1 IntArrayList :: IntArrayList () Linear list concepts


2 { List ADT

3 this - > capacity = 10; Array implementation


4 this - > data = new int [ this - > capacity ]; How to store?

5 this - > count = 0; Implementation in C++

6 } Singly linked list


7 Conceptual idea
Implementation in C++
8 IntArrayList ::~ IntArrayList ()
Operations
9 {
10 delete [] this - > data ; Comparison of
implementations
11 }

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

14 int moveCount = this - > count - index ; Comparison of


15 if ( moveCount > 0) implementations
16 memmove ( this - > data + index + 1 ,
17 this - > data + index ,
18 moveCount * sizeof ( int ));
19
20 this - > data [ index ] = element ;
21 this - > count ++;
22 }

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

8 this - > data + index + 1 , Array implementation


9 sizeof ( int ) * moveCount ); How to store?
10 Implementation in C++

11 this - > count - -; Singly linked list


12 return e l em en tT o Re mo ve ; Conceptual idea

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

1 bool IntArrayList :: empty ()


2 {
3 return this - > count == 0;
4 } Linear list concepts
List ADT
5
6 int IntArrayList :: size () Array implementation
7 { How to store?
Implementation in C++
8 return this - > count ;
9 } Singly linked list
Conceptual idea
10
Implementation in C++
11 void IntArrayList :: clear () Operations
12 {
Comparison of
13 delete [] this - > data ; implementations
14
15 this - > capacity = 10;
16 this - > data = new int [ this - > capacity ];
17 this - > count = 0;
18 }

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++

13 int IntArrayList :: indexOf ( int element ) { Operations

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

1 string IntArrayList :: toString () {


2 stringstream ss ;
3 ss << " [ " ;
4 for ( int index = 0; index < count - 1; index ++)
5 ss << data [ index ] << " , " ; Linear list concepts
6 List ADT

7 if ( count > 0) ss << data [ count - 1] << " ] " ; Array implementation
8 else ss << " ] " ; How to store?

9 Implementation in C++

10 return ss . str (); Singly linked list


11 } Conceptual idea
Implementation in C++
12
Operations
13 void IntArrayList :: dump () {
14 string line (50 , ’= ’ ); Comparison of
implementations
15 cout << line << endl ;
16 cout << " Integer list ’s information : " << endl ;
17 cout << " - Capacity : " << this - > capacity << endl ;
18 cout << " - Size : " << this - > count << endl ;
19 cout << " - Data : " << this - > toString () << endl ;
20 cout << line << endl ;
21 }

List (P.1).21
List (P.1)
Array List: Implementation in C++ with Integer
Dept. Computer
Science

1 void IntArrayList :: checkIndex ( int index ) {


2 if ( index < 0 || index >= this - > count )
3 throw std :: out_of_range (
4 " Index is out of range " ); Linear list concepts
List ADT
5 }
6 Array implementation
7 void IntArrayList :: ensureCa pacity ( int capacity ) { How to store?
Implementation in C++
8 if ( capacity > this - > capacity ) {
9 int newCapacity = this - > capacity * 3 / 2; Singly linked list
Conceptual idea
10 int * newData = new int [ newCapacity ];
Implementation in C++
11 memmove ( newData , this - > data , Operations
12 this - > count * sizeof ( int ));
Comparison of
13 this - > capacity = newCapacity ; implementations
14 delete [] this - > data ;
15
16 this - > data = newData ;
17 }
18 }

List (P.1).22
List (P.1)
Contiguous Implementation of List
Dept. Computer
Science

In processing a contiguous list with n elements:


Linear list concepts
List ADT

• Insert and Remove operate in time approx- Array implementation


How to store?

imately proportional to n (require physical Implementation in C++

Singly linked list

shifting). Conceptual idea


Implementation in C++
Operations

Comparison of
implementations

• Clear, Empty, Full, Size, Replace, and Re-


trieve in constant time.

List (P.1).23
List (P.1)

Dept. Computer
Science

Linear list concepts


List ADT

Array implementation

Singly linked list How to store?


Implementation in C++

Singly linked list


Conceptual idea
Implementation in C++
Operations

Comparison of
implementations

List (P.1).24
List (P.1)
Linked List
Dept. Computer
Science

Linear list concepts


List ADT

Array implementation
Figure: Singly Linked List How to store?
Implementation in C++

Singly linked list


Conceptual idea

Structure Implementation in C++


Operations

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++

Singly linked list


Conceptual idea
Implementation in C++
Operations

Comparison of
implementations

List (P.1).26
List (P.1)
Nodes
Dept. Computer
Science

Linear list concepts


List ADT

Figure: Linked list node structure Array implementation


How to store?
Implementation in C++

Singly linked list


Conceptual idea
Implementation in C++

node // General dataType: Operations

data <dataType> dataType Comparison of


implementations
link <pointer> key <keyType>
end node field1 <...>
field2 <...>
...
fieldn <...>
end dataType

List (P.1).27
List (P.1)
Example
Dept. Computer
Science

Linear list concepts


List ADT

Array implementation
How to store?
Implementation in C++

Singly linked list


Conceptual idea
Implementation in C++
Operations

Comparison of
implementations

List (P.1).28
List (P.1)
Example
Dept. Computer
Science

Linear list concepts


List ADT

Array implementation
How to store?
Implementation in C++

Singly linked list


Conceptual idea
Implementation in C++
Operations

Comparison of
implementations

Figure: List representing polynomial

List (P.1).29
List (P.1)
Implementation in C++
Dept. Computer
Science

Linear list concepts


Example List ADT

Array implementation
How to store?
Implementation in C++

node 1 struct Node { Singly linked list


data <dataType> 2 int data ; Conceptual idea

next <pointer> 3 Node * linknext ; Implementation in C++


Operations
end node 4 };
Comparison of
implementations

List (P.1).30
List (P.1)
Implementation in C++ with struct
Dept. Computer
Science

Example

Linear list concepts


1 struct Node { 1 struct Node { List ADT

2 int data ; 2 float data ; Array implementation


3 Node * next ; 3 Node * next ; How to store?
4 }; 4 }; Implementation in C++

Singly linked list


Conceptual idea

In general, with template: Implementation in C++


Operations

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

15 class Node { Comparison of


16 protected : implementations
17 int data ;
18 Node * next ;
19
20 public :
21 Node ( int data = 0) {
22 int data = 0;
23 this - > next = NULL ;
24 }
25 }
List (P.1).32
26 };
List (P.1)
Linked list operations
Dept. Computer
Science

• Create an empty linked list Linear list concepts


List ADT

• Insert an item into a linked list Array implementation


How to store?

• Remove an item from a linked list


Implementation in C++

Singly linked list


Conceptual idea

• Traverse a linked list Implementation in C++


Operations

Comparison of
• Destroy a linked list implementations

• ...

List (P.1).33
List (P.1)
Insertion: Implementation in C++
Dept. Computer
Science

Linear list concepts


List ADT
1 class Int SLinked List : public IList < int > {
2 // D e c l a r a t i o n of a t t r i b u t e s . Array implementation
3 // D e c l a r a t i o n of constructor , d e s t r u c t o r . How to store?
Implementation in C++
4 // D e c l a r a t i o n of nested classes .
5 public : Singly linked list
Conceptual idea
6 virtual void add ( int element );
Implementation in C++
7 virtual void add ( int index , int element ); Operations
8 };
Comparison of
implementations

List (P.1).34
List (P.1)
Insert an item into a linked list
Dept. Computer
Science

Linear list concepts


Cases with insertion: List ADT

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

• Append an item into a list Implementation in C++


Operations
• Insert an item at a specific position in list Comparison of
implementations

List (P.1).35
List (P.1)
Insertion: Prepend to an empty list
Dept. Computer
Science

Linear list concepts


head tail List ADT

Array implementation
How to store?
Implementation in C++

Singly linked list


Conceptual idea
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

Linear list concepts


head tail List ADT

Array implementation
How to store?
Implementation in C++

Singly linked list


Conceptual idea
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

Linear list concepts


head tail List ADT

Array implementation
How to store?
Implementation in C++

Singly linked list


Conceptual idea
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++

Singly linked list


25 6 Conceptual idea
Implementation in C++
Operations

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++

Singly linked list


25 6 Conceptual idea
Implementation in C++
Operations

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++

Singly linked list


25 6 Conceptual idea
Implementation in C++
Operations

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++

Singly linked list


25 6 Conceptual idea
Implementation in C++
Operations

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++

Singly linked list


25 6 Conceptual idea
Implementation in C++
Operations

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++

Singly linked list


25 6 Conceptual idea
Implementation in C++
Operations

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++

Singly linked list


25 6 47 Conceptual idea
Implementation in C++
Operations

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++

Singly linked list


25 6 47 Conceptual idea
Implementation in C++
Operations

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++

Singly linked list


25 6 47 Conceptual idea
Implementation in C++
Operations

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++

Singly linked list


25 6 47 Conceptual idea
Implementation in C++
Operations

Comparison of
implementations

18

pNew

List (P.1).39
List (P.1)
Removal: Implementation in C++
Dept. Computer
Science

Linear list concepts


List ADT
1 class Int SLinked List : public IList < int > {
2 // D e c l a r a t i o n of attributes , constructor , d e s t r u c t o r .Array implementation
How to store?
3 // D e c l a r a t i o n of nested classes . Implementation in C++
4 public :
Singly linked list
5 virtual int removeAt ( int index ); Conceptual idea
6 virtual bool removeItem ( int item ); Implementation in C++

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++

Singly linked list


25 6 Conceptual idea
Implementation in C++
Operations

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++

Singly linked list


25 6 Conceptual idea
Implementation in C++
Operations

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++

Singly linked list


25 6 Conceptual idea
Implementation in C++
Operations

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++

Singly linked list


25 6 Conceptual idea
Implementation in C++
Operations

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++

Singly linked list


6 Conceptual idea
Implementation in C++
Operations

Comparison of
implementations

List (P.1).41
List (P.1)
Removal: Delete the last element
Dept. Computer
Science

Linear list concepts


head tail List ADT

Array implementation
How to store?
Implementation in C++

Singly linked list


Conceptual idea
Implementation in C++
Operations

25 6 47 Comparison of
implementations

List (P.1).42
List (P.1)
Removal: Delete the last element
Dept. Computer
Science

Linear list concepts


head pre tail List ADT

Array implementation
How to store?
Implementation in C++

Singly linked list


Conceptual idea
Implementation in C++
Operations

25 6 47 Comparison of
implementations

List (P.1).42
List (P.1)
Removal: Delete the last element
Dept. Computer
Science

Linear list concepts


head pre tail List ADT

Array implementation
How to store?
Implementation in C++

Singly linked list


Conceptual idea
Implementation in C++
Operations

25 6 47 Comparison of
implementations

List (P.1).42
List (P.1)
Removal: Delete the last element
Dept. Computer
Science

Linear list concepts


head tail List ADT

Array implementation
How to store?
Implementation in C++

Singly linked list


Conceptual idea
Implementation in C++
Operations

25 6 Comparison of
implementations

List (P.1).42
List (P.1)
Removal: Delete at index i
Dept. Computer
Science

Linear list concepts


head tail List ADT

Array implementation
How to store?
Implementation in C++

Singly linked list


Conceptual idea
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

head pre temp tail Linear list concepts


List ADT

Array implementation
How to store?
Implementation in C++

Singly linked list


Conceptual idea
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

head pre temp tail Linear list concepts


List ADT

Array implementation
How to store?
Implementation in C++

Singly linked list


Conceptual idea
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

head pre temp tail Linear list concepts


List ADT

Array implementation
How to store?
Implementation in C++

Singly linked list


Conceptual idea
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

Linear list concepts


head tail List ADT

Array implementation
How to store?
Implementation in C++

Singly linked list


Conceptual idea
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

9 virtual bool contains ( int item ); Comparison of


10 }; implementations

List (P.1).44
List (P.1)
Searching
Dept. Computer
Science

Search element with index 1.


Linear list concepts
List ADT

Array implementation
head tail How to store?
Implementation in C++

Singly linked list


Conceptual idea
Implementation in C++
Operations

Comparison of
implementations
25 6 47

List (P.1).45
List (P.1)
Searching
Dept. Computer
Science

Search element with index 1.


Linear list concepts
index = 0 List ADT

head temp tail Array implementation


How to store?
Implementation in C++

Singly linked list


Conceptual idea
Implementation in C++
Operations

Comparison of
implementations
25 6 47

List (P.1).45
List (P.1)
Searching
Dept. Computer
Science

Search element with index 1.


Linear list concepts
index = 1 List ADT

head temp tail Array implementation


How to store?
Implementation in C++

Singly linked list


Conceptual idea
Implementation in C++
Operations

Comparison of
implementations
25 6 47

List (P.1).45
List (P.1)
Searching
Dept. Computer
Science

Search element with index 1.


Linear list concepts
index = 1 List ADT

head temp tail Array implementation


STOP! How to store?
Implementation in C++

Singly linked list


Conceptual idea
Implementation in C++
Operations

Comparison of
implementations
25 6 47

List (P.1).45
List (P.1)

Dept. Computer
Science

Linear list concepts


List ADT

Comparison of Array implementation


How to store?
Implementation in C++

implementations of list Singly linked list


Conceptual idea
Implementation in C++
Operations

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

• Cons: How to store?


Implementation in C++

• If we want to insert or delete an ele- Singly linked list


Conceptual idea
Implementation in C++

ment, we have to shift subsequent el- Operations

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

quire us to move/shift subsequent data List ADT

Array implementation
elements. How to store?
Implementation in C++

Singly linked list


• Cons: Conceptual idea
Implementation in C++

• If we want to access a specific element, Operations

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

• Contiguous storage is generally preferable when:


• the entries are individually very small;
• the size of the list is known when the program is writ- Linear list concepts
List ADT

ten; Array implementation


• few insertions or deletions need to be made except at How to store?
Implementation in C++
the end of the list; and
Singly linked list
• random access is important. Conceptual idea
Implementation in C++
Operations
• Linked storage proves superior when:
Comparison of
• the entries are large; implementations

• the size of the list is not known in advance; and


• flexibility is needed in inserting, deleting, and rearrang-
ing the entries.

List (P.1).49
List (P.1)

Dept. Computer
Science

Linear list concepts


List ADT

Array implementation

THANK YOU. How to store?


Implementation in C++

Singly linked list


Conceptual idea
Implementation in C++
Operations

Comparison of
implementations

List (P.1).50

You might also like