0% found this document useful (0 votes)
51 views37 pages

Lecture 6 List s2021

This document discusses data structures and algorithms for lists. It defines the list abstract data type (ADT) as a sequence of elements of the same type that can be accessed by position. Common list operations like adding, removing, and retrieving elements are described. Two common implementations of lists are presented: array-based lists and linked lists. Array-based lists store elements in a fixed-size array, while linked lists use dynamically allocated nodes connected via references. Examples of singly linked lists are provided to demonstrate how they implement list operations and represent data. Finally, potential applications of lists like data sequencing and memory management are briefly described.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views37 pages

Lecture 6 List s2021

This document discusses data structures and algorithms for lists. It defines the list abstract data type (ADT) as a sequence of elements of the same type that can be accessed by position. Common list operations like adding, removing, and retrieving elements are described. Two common implementations of lists are presented: array-based lists and linked lists. Array-based lists store elements in a fixed-size array, while linked lists use dynamically allocated nodes connected via references. Examples of singly linked lists are provided to demonstrate how they implement list operations and represent data. Finally, potential applications of lists like data sequencing and memory management are briefly described.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Data structure and algorithms

Spring 2021

LIST

Lecturer: Do Thuy Duong


Contents
• Abstract Data Type (ADT)
• List ADT
• List ADT implementation
• Array-based list
• Linked list
• Singly linked list
• Circular linked list
• Doubly linked list 2

2
Abstract Data Type (ADT)
• An ADT is a model of a data
type:
• Data properties
• A list of operations on that data
(What operations an ADT can
have, not how to implement
them)
• Example: The List ADT:
• Group of elements
• Operations: Add items, Remove
items, Find items, Display items…

3
Abstract Data Type (ADT)
• We use A Data Structure to implement
an ADT.

4
Abstract Data Type (ADT)
• We use A Data Structure to implement an ADT.

Abstract data types Data structures


List Array-based List
Linked list
Stack Array based stack
Linked list based stack
Queue Array based queue
Linked list based queue
Map Tree map
Hash map/ Hash table
Vehicle Bicycle
Car
Truck
5
List

What it is (conceptual)?

When we use it (applications)?

How we implement it (implementation)?

6
List ADT
• Definition
• A sequence of zero or more elementsof the same
type.
• Except for the first and last items, each item has a
unique predecessor and a unique successor.
• First item (head or front) do not havea
predecessor.
• Last item (tail or end) do not havea successor.
• Items are referenced by their position within the
list
7
List ADT
• List Operations
• Create an empty list.
• Determine whether a list is empty.
• Determine the number of items in a list.
• Add an item at a given position in the list.
• Remove the item at a given position in the list.
• Remove all the items from the list.
• Retrieve (get) item at a given position in the
list.
8
List ADT
• UML model

List ADT

+ isEmpty(): boolean
+ getLength(): int
+ add(int: pos, ItemType: newItem): void
+ remove(int: pos): void
+ removeAll(): void
+ get(int: Pos): ItemType

9
List implementations
• ADT implementation
• Choose a data structure to represent the
ADT’s data.
• How to choose? Depends on
• Details of the ADT’soperations.
• Context in which the operations will be used.

10
List implementations
A list can be implementedas:
• An array (statically allocated)

Data Data Data Data Data Data

• A linked list (dynamically allocated)


Data Data Data …

11
Array-Based List [1]
• A list’s items are stored in an array items
• Both an array and a list identify their items by index
number.
• A list’s kth item will be stored in items[k-1]
• length and maxSize are used to indicate the
current length and maximum length of the list

12
Array-Based List [2]
• isEmpty():
• The list is empty when length equals to 0
• getLength():
• Return length
• get(int Pos):
• Return indexs[Pos-1]
• removeAll():
• Assign length to 0
• add() and remove() ???

13
Array-Based List [3]
myList [0] [1] [2] [3] [4] [5] [6] [7] [8]
List

length 5

9 3 -2 5 18 6
maxSize

add(3,-4);

myList [0] [1] [2] [3] [4] [5] [6] [7] [8]
List

length 5
3 -2 -4 18 6
maxSize 9
We need to shift the elementsof the
array and then insert the new
14
element.
Array-Based List [3]
add(3,-4);

myList [0] [1] [2] [3] [4] [5] [6] [7] [8]
List

length 5

maxSize 5 3 -2 5 18 6

myList [0] [1] [2] [3] [4] [5] [6] [7] [8]
List

length 6

maxSize 9 3 -2 -4 5 18 6

15
Array-Based List [4]
• add(), remove()
• Time complexity for these operations is O(n), where
n is the current length of the list.
• Advantages
• Easy toimplement
• Quick search for a node
• Disadvantages
• Not the ideal solution for problems involving a lot
of insertions or deletions (data movement)
• Size is fixed at runtime

16
Linked List
• Also called reference-basedlist
• Differences with array-based list:
• Data storage
• Arrays store elements in consecutive memoryblocks
• Linked lists store elements in components called
nodes, not necessarily in consecutive memoryblocks
• Dynamic size
• Linked lists can grow at runtime

17
Singly Linked List[1]
• A list of nodes.
• Every node (except the last one) contains the
link to the next node.
• Every node has two components:
• data
data next
• next
• data can be a primitive data type or a reference to
an object.
• next is a reference variable (in Java) or a pointer
(in C).

18
Singly Linked List[2]
• Nodes (data, next) connected in a chain by links

• The head or the tail variables point to the first


and the last node of the list.

19
Singly Linked List[3]
SLList SLNode

head: SLNode data: DataType


next: SLNode
+ isEmpty(): boolean
+ getLength(): int +setNext(SLNode: node): void
+ add(SLNode: node): void +getNext(): SLNode
+ addAt(int: pos, SLNode: node): void +getData(): DataType
+ remove(int: pos): void +setData(DataType: data):void
+ removeAll(): void
+ get(int: pos): SLNode

data next data next


info ink
A B22
l
20
Singly Linked List[4]
• SLNode:
• setNext(), getNext(), getData(), setData()
• SLList:
• isEmpty():
• The list is empty when head equals to null.
• add(SLNode newNode):
• Insert a node at the beginning of a linked list.

newNode.setNext(head);
head=newNode;
21
Singly Linked List[5]
• addAt(int pos, SLNode newNode):
• Insert a new node at the pos position of a linked list:
• Travel to the pos-1 position
• Insert the new node
Pos -1

prevNode

newNode
prevNode=traversing(pos-1);
newNode.setNext(prevNode.getNext());
prevNode.setNext(newNode);
22
Singly Linked List[6]
• How to travel to the pos position?

current

int count=1;
SLNode current = this.head;
while (count < pos)
{
count++;
current=current.getNext();
}
return current;
23
Singly Linked List[7]
• remove(int pos): remove the node at the pos position
from a linked list:
• Travel to the pos position
• Remove the pos node

prevNode posNode

prevNode=traversing(pos-1);
posNode=traversing(pos); //posNode=prevNode.getNext();
prevNode.setNext(posNode.getNext());
24
Singly Linked List[8]
• remove(int pos):
• If pos equals 1
• Remove the first node

head=head.getNext();

• removeAll():
head=null;

25
Singly Linked List[9]
• get(int pos):
• Return the node at the pos position
return traversing(pos);
• getLength():
int count=0;
SLNode current=this.head;
while (current != null)
{
count++;
current=current.getNext();
}
return count;

26
List applications[1]
• Use list to solve problemrelated to order of
a collection:
• Data sequence management: mailing list,
scrolling list (GUI)
• Memory management: Hash table
• And mathematical problem…
• Linked lists are used as a building block for many
other data structures, such as stacks, queues
and their variations

27
List applications[2]
• Example : Representing Polynomials
• Addition/subtraction /multiplication.. . of two
polynomials.
P1(x) = 2x2+3x+7
P2 (x)= 3x3+5x+2
P1(x)+ P2(x)= 3x3+2x2+8x+9
• How can a list be used to represent that
polynomial?
28
List applications[3]
For the polynomial:
f(x) = 3.2x4 + 7x2 - 4x + 2
We can represent it using a linked-list:
• Node’s data contains a real number (coefficient) and
an integer (degree).

f 3.2 4 7 2 -4 1 2 0 NULL

29
Other Linked-lists
• List with Dummy Header Node
• Circular Linked Lists
• Doubly Linked Lists

30
LL with Dummy HeaderNode
• Sometimes it is desirable to keep an extra
node at the front of alist.

head Dummy
header Data Data … Data …

Dummy HeaderNode Data nodes

We don’t care about The value of these nodes


the value of this node. are our data.

31
Circular Lists [1]
• In a linear list, the traversing order must be from
head -> tail.

Linear list Data Data Data … Data NULL

Consider the following implementation: Circular list

Circular list Data Data Data … Data

• From any point in such a list we can reach any other


point in the list.
32
Circular Lists [2]
• To specify which node is the first one, we need a list head reference

First
Data Data Data … Data
head

• A null list head reference represents an empty circular head


list. NULL

• We can also use a dummy header node that allows insertion and
removal of an element conveniently from anywhere of the list.

head Dummy
header Data Data … Data

An empty circular list: head Dummy


header

33
Doubly Linked List[1]
Some problems of singly linked lists:
• We can traverse the list in one direction only.
• To delete a node p, we need to know the predecessor of p.
• We can perform insertion after a given node in the list. But it is
difficult to insert before a given node.

Doubly Linked List can solve these problems:


(Although it takes some extra memory space)

Linear, singly linked list

Data Data Data … Data NULL

Linear, doubly linked list


36
NULL Lnext Data Rnext Lnext Data Rnext … Lnext Data Rnext NULL
34
Doubly Linked List[2]
• A doubly linked list provides a natural implementation of the List
ADT
• Nodes implement Position and store: prev next
• data
• link to the previous node
• link to the next node
data node
• Special head and tail references
head nodes/positions tail

elements

35
Doubly circular LinkedList with Dummy
HeaderNode

Doubly circular Linked List:


• A dummy header node can be included for easier
manipulation:

Dummy …
Lnext
Header Rnext Lnext Data Rnext Lnext Data Rnext

• If the list is empty, both link fields of the header node


point to itself.
Dummy
Lnext Header Rnext

36
Tutorial & nexttopic
• Preparing for the tutorial:
• Practice with examples and exercises in
Tutorial 6
• Preparing for next topic:
• Read textbook chapter 3 (3.6 & 3.7) Stack and
Queue.

39

37

You might also like