Lecture 6 List s2021
Lecture 6 List s2021
Spring 2021
LIST
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.
What it is (conceptual)?
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)
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
19
Singly Linked List[3]
SLList SLNode
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 …
31
Circular Lists [1]
• In a linear list, the traversing order must be from
head -> tail.
First
Data Data Data … Data
head
• 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
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.
elements
35
Doubly circular LinkedList with Dummy
HeaderNode
Dummy …
Lnext
Header Rnext Lnext Data Rnext Lnext Data 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