datos, 2018-2019 grado en ciencia e ingeniería de linear abstract data type...

151
Unit 2 - Linear Abstract Data Type Data Structures and Algorithms (DSA) Grado en Ciencia e Ingeniería de Datos, 2018-2019 Author: Isabel Segura-Bedmar

Upload: others

Post on 01-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

Unit 2 - Linear Abstract Data Type

Data Structures and Algorithms (DSA)

Grado en Ciencia e Ingeniería de Datos, 2018-2019

Author: Isabel Segura-Bedmar

Page 2: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

Linear ADT

2

● Represents a sequence of elements of some type.● Examples:

○ Person names (strings): Mary, John, Paul, …○ integers: 5,6,1,-3,0,2,1,…○ Objects (instances) of the Point class.○ Objects (instances) of the Employee class.

● All the elements must belong to the same data type.

Page 3: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

Index

3

2.1. Stack ADT2.2. Queue ADT2.3. List ADT

2.3.1. Singly Linked List 2.3.2. Doubly Linked List

Page 4: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

2.1. Index of Stack ADT.

4

2.1.1. Stack Abstract Data Type.2.1.2. Implementing a stack using a Python list 2.1.3. Using Stacks to solve problems

Page 5: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

2.1.1. Stack Abstract Data Type● Linear data structure based on LIFO (last-in, first-out)

principle. ● Insertion (push) and removing (pop) operations are

always performed at one single position, the top of the stack.

5

top (last added element) ->

bottom (first added element) ->

Page 6: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

Undo operations

Back Navigation

2.1.1. Stack Abstract Data Type

Page 7: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

● top(): returns the top element from the stack (without removing it). The stack is not modified. It throws an error if the stack is empty.

● size(): returns the number of elements stored on the stack.

● isEmpty(): returns true if the stack is empty, and false otherwise.

7

2.1.1. Stack Abstract Data Type

Page 8: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

8

Operations Stack Output

s.isEmpty() [] True

s.push(‘W’) [‘W’] --

s.push(‘O’) ['W',’0’] --

s.top() ['W',’0’] ‘O’

s.push(‘D’) ['W',’0’,’D’] --

s.pop() ['W',’0’] ‘D’

s.size() ['W',’0’] 2

s.push(‘R’) ['W',’0’,’R’] --

s.push(‘D) ['W',’0’,’R’,’D’]

2.1.1. Stack Abstract Data Type

Page 9: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

2.1.1. Stack Abstract Data Type. Quizzes

9

1. Given the following code, what is the top item on the stack?.

Answers:a) 5,b) 1,c) 3,d) The stack is empty.

m = Stack()m.push(5)m.push(1)m.pop()m.push(3)m.top()

Page 10: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

2.1.1. Stack Abstract Data Type. Quizzes

10

1. Given the following code, what is the top item on the stack?.

Answers:a) 5,b) 1,c) 3,d) The stack is empty.

m = Stack()m.push(5)m.push(1)m.pop()m.push(3)m.top()

Page 11: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

11

2. Given the following code, what is the output?.

Answers:a) 3,5b) an error will occurc) 1,5

m = Stack()m.push(3)m.push(5)m.push(1)while not m.isEmpty():

print(m.pop())print(m.pop())

2.1.1. Stack Abstract Data Type. Quizzes

Page 12: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

12

2. Given the following code, what is the output?.

Answers:a) 3,5b) an error will occurc) 1,5

m = Stack()m.push(3)m.push(5)m.push(1)while not m.isEmpty():

print(m.pop())print(m.pop())

2.1.1. Stack Abstract Data Type. Quizzes

Page 13: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

2.1. Index of Stack ADT.

13

2.1.1. Stack Abstract Data Type.2.1.2. Implementing a stack using a Python list2.1.3. Using Stacks to solve problems

Page 14: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

● Python list already contains methods (append, pop, len, etc) to manipulate the data structure.

● Array-based implementation.

14

2.1.2. Implementation a stack using a Python list

Page 15: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

2.1.2. Implementing a stack using a Python list● Which end of the list will be considered the top of the

stack and which will be the bottom? Two options:

15

W O R D

0 1 2index 3

bottom top

D R O W

0 1 2index 3

top bottom

Option 1: Option 2:

Page 16: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

● Option 1: Top is stored at the end of the list

16

2.1.2. Implementing a stack using a Python list

Page 17: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

● Option 1: Top is stored at the end of the list

17

2.1.2. Implementing a stack using a Python list

Page 18: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

Option 2: Top is stored at the beginning of the list

18

2.1.2. Implementing a stack using a Python list

Page 19: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

● What implementation is better?○ Option 1 (top at the end of the list)○ Option 2 (top at the beginning of the list)

19

push pop

Option 1 O(1) O(1)

Option 2 O(n) O(n)

The first implementation requires less time complexity!!!

2.1.2. Implementing a stack using a Python list

Page 20: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

2.1. Index of Stack ADT.

20

2.1.1. Stack Abstract Data Type.2.1.2. Implementing a stack using a Python list2.1.3. Using Stacks to solve problems

Page 21: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

21

● Stacks are very useful for reversing data

2.1.3. Using stacks to solve problems

Page 22: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

22

2.1.3. Using stacks to solve problems

Page 23: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

23

Evaluation of arithmetic and logical expressions requires checking if their parentheses are balanced:

Expression Balanced parenthesis?

x= (((y+2)*5)/2 - 5) * 10

(()())

((()())

2.1.3. Using stacks to solve problems

Page 24: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

24

● Parentheses must appear in a balanced way:○ each opening symbol has a corresponding closing

symbol ○ the pairs of parentheses are properly nested.

● A stack is a good data structure to solve this problem because closing symbols match opening symbols in the reverse order of their appearance.

2.1.3. Using stacks to solve problems

Page 25: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

25

Steps to solve the problem:1. Create an empty stack to store the opening

symbols.

2.1.3. Using stacks to solve problems

Page 26: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

26

Steps to solve the problem:2. Read the expression from left to right. For each symbol:

i) If the symbol is an opening symbol, push it on the stack.

2.1.3. Using stacks to solve problems

Page 27: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

27

Steps to solve the problem:2. Read the expression from left to right. For each symbol:

i) If the symbol is an opening symbol, push it on the stack.

ii) If the symbol is a closing symbol:1. If the stack is empty, there is no opening

symbol for it!!!. Return false.2. Otherwise, remove the top of the stack (with

pop) and continue.

2.1.3. Using stacks to solve problems

Page 28: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

28

Steps to solve the problem:3. When all characters have been readed, there is

two options:a. If the stack is empty, this means that the

expression is balanced.b. Otherwise, the expression is not balanced

(there are still opening symbols in the stack)

2.1.3. Using stacks to solve problems

Page 29: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

29

2.1.3. Using stacks to solve problems

Page 30: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

30

● The previous function only works for parenthesis. ● In the next lab class, we will extend it in order to deal

also with:■ Brace: ‘{‘ and ‘}■ Brackets: ‘[‘ and ‘]’

2.1.3. Using stacks to solve problems

Page 31: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

Index

31

2.1. Stack ADT2.2. Queue ADT2.3. List ADT

2.3.1. Singly Linked List 2.3.2. Doubly Linked List

Page 32: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

2.2. Index of Queue ADT.

32

2.2.1. Queue Abstract Data Type.2.2.2. Implementing a queue using a Python list 2.2.3. Using queues to solve problems

Page 33: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

● Linear data structure based on FIFO (first-in, first-out) principle. ○ We insert (enqueue) a new element at the end (tail)

of the queue. ○ We remove (dequeue) the first element (head) of

the queue.

2.2.1. Queue Abstract Data Type

Page 34: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

Applications of queues

2.2.1. Queue Abstract Data Type

Page 35: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

35

● Collection of items which are added to the tail of the queue and removed from the beginning (front) of the queue.

● The queue operations are:○ Queue(): creates an empty queue.○ enqueue (Object e): adds the element e at the tail

of the queue. ○ dequeue(): removes and returns the first element

of the queue. The queue is modified. If the queue is empty, an error is thrown.

2.2.1. Queue Abstract Data Type

Page 36: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

36

● front(): returns the first element of the queue. The queue is not modified. If the queue is empty, it throws an error.

● isEmpty(): returns true if the queue is empty; otherwise false.

● size(): returns the numbers of elements in the queue.

2.2.1. Queue Abstract Data Type

Page 37: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

37

Operations Queue Output

q.isEmpty() [] True

q.enqueue(1) [1] --

q.enqueue(2) [1,2] --

q.enqueue(3) [1,2,3] --

q.front() [1,2,3] 1

q.dequeue() [2,3] 2

d.size() [2,3] 2

2.2.1. Queue Abstract Data Type

Page 38: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

38

Given the following code, what items are left in the queue?

Answers:a) 3,5,1b) 5,1c) 3,5d) 3,1

m = Queue()m.enqueue(3)m.enqueue(5)m.enqueue(1)m.dequeue()

2.2.1. Queue Abstract Data Type - Quizz

Page 39: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

39

Given the following code, what items are left in the queue?

Answers:a) 3,5,1b) 5,1c) 3,5d) 3,1

m = Queue()m.enqueue(3)m.enqueue(5)m.enqueue(1)m.dequeue()

2.2.1. Queue Abstract Data Type - Quizz

Page 40: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

2.2. Index of Queue ADT.

40

2.2.1. Queue Abstract Data Type.2.2.2. Implementing a queue using a Python list 2.2.3. Using queues to solve problems

Page 41: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

41

2.2.2. Implementing a queue using a Python List

Page 42: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

42

2.2.2. Implementing a queue using a Python List

Page 43: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

43

Stack Queue

push/enqueue O(1) O(1)

pop / dequeue O(1) O(n)

The Queue array-based implementation is worse than the array-based implementation for stacks!!!

2.2.2. Implementing a queue using a Python List

Page 44: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

2.2. Index of Queue ADT.

44

2.2.1. Queue Abstract Data Type.2.2.2. Implementing a queue using a Python list 2.2.3. Using queues to solve problems

Page 45: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

Josephus Problem

2.2.3. Using queues to solve problemas

Page 46: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

Josephus Problem

2.2.3. Using queues to solve problemas

Page 47: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

Index

47

2.1. Stack ADT2.2. Queue ADT2.3. List ADT

2.3.1. Singly Linked List 2.3.2. Doubly Linked List

Page 48: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

48

● Definition of List ADT● How Python lists are implemented internally 2.3.1. Implementing a list ADT using a singly linked list 2.3.2. Implementing a list ADT using a doubly linked list

2.3. Index of List ADT.

Page 49: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

49

● Informal Specification:○ Sequence of elements {a1,a2,…,an} where each

element has one only predecessor (except the first one that does not have predecessor) and one only successor (except the last one that does not have successor).

○ The basic operations of a list ADT are:■ insert an element to the list. ■ remove an element from the list■ read an element in the list

2.3. List Abstract Data Type

Page 50: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

2.3. List Abstract Data Type

50

● A collection of items where each item holds a relative position with respect to the others. Some possible operation are: ○ List() creates a new list. ○ addFirst(L,e) add the element e at the beginning of

the list L.○ addLast(L,e) add the element e at the tail of the list

L.○ removeFirst(L) removes the first element of the list

L. It returns the element. ○ removeLast(L) removes the last element of the list

L. It returns the element.

Page 51: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

51

● More operations:○ isEmpty(L): returns True if the list L is empty,

False otherwise.○ size(L): returns the number of items of the list.○ contains(L,e): returns the first position of the

element e in the list. If the element doesn’t exist return -1.

○ insertAt(L,index,e): inserts the element e at the position index of the list L.

○ removeAt(L,index): removes the element at the position index of the list L. It returns the element.

2.3. List Abstract Data Type

Page 52: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

52

● Definition of List ADT● How Python lists are implemented internally 2.3.1. Implementing a list ADT using a singly linked list 2.3.2. Implementing a list ADT using a doubly linked list

2.3. Index of List ADT.

Page 53: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

How are Python lists implemented internally?

53

MaríaPepaJuanArturoMartínJoséDaniel

To save a list of n elements, we would need n consecutive cells in memory

2.3. List Abstract Data Type

Page 54: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

54

MaríaPepaJuanArturoMartínJoséDaniel

Easy and fast access to all its elements: A[i]

2.3. List Abstract Data Type

Page 55: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

55

MaríaPepaJuanArturoMartínJoséDaniel

A.pop(0)

PepaJuanArturoMarínJoséDaniel

Move all the elements one spot to the left in the list

0

1

2

3

4

5

6

0

1

2

3

4

5

6

7

This is not efficient when your program has to perform a lot of remove operations

2.3. List Abstract Data Type

Page 56: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

56

MaríaPepaJuanArturoMartínJoséDaniel

A.insert(3,”Isabel”)

MaríaPepaJuanIsabelArturoMartínJoséDaniel

Move all the elements (index>=3) one position to the right

0

1

2

3

4

5

6

0

1

2

3

4

5

6

7

This is not efficient when your program has to perform a lot of insertion operations

2.3. List Abstract Data Type

Page 57: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

57

MaríaPepaJuanArturoMartínJoséDaniel

A.append(”Isabel”)

0

1

2

3

4

5

6 If the next location after the last element is not free => search a new place for the whole list.

2.3. List Abstract Data Type

Page 58: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

58

● Definition of List ADT● How Python lists are implemented internally 2.3.1. Implementing a list ADT using a singly linked list 2.3.2. Implementing a list ADT using a doubly linked list

2.3. Index of List ADT.

Page 59: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

59

María

Pepa

Juan

Arturo

Martín None

mem

ory

head

2.3.1. Implementing a List using a singly linked list

Page 60: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

60

Lucas NoneMary

Pepa

JuanArturo

Martin None

head

A.addLast(“Lucas”)

2.3.1. Implementing a List using a singly linked list

The gaps in memory allow that the physical and logical orders can be different.

Page 61: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

61

1 10 115 None

● Each node stores an element of the sequence and a reference to the next node of the list.

head

2.3.1. Implementing a List using a singly linked list

Page 62: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

62

2.3.1. Implementing a List using a singly linked list(pseudo-code for singly node constructor)

Algorithm Node(node,e):node.element=enode.next=None

Page 63: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

63

2.3.1. Implementing a List using a singly linked list(pseudo-code for Singly Linked List constructor)

Algorithm SList(list):list.head=Nonelist.size=0

● The constructor creates an empty list. ● head must be None.

Page 64: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

64

5 101 None

head

2

newNode

5 101 None

head

2

newNode

5 101 None

head

2.3.1. Implementing a List using a singly linked list(addFirst method)

Page 65: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

65

Algorithm addFirst(L,e):newNode=Node(e) #1newNode.next=L.head #2L.head=newNode #3L.size=L.size+1 #4

2.3.1. Implementing a List using a singly linked list(addFirst method, pseudo-code)

Page 66: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

66

Algorithm addFirst(L,e):newNode=Node(e) #1L.head=newNode #3newNode.next=L.head #2L.size=L.size+1 #4

CN

newNode

ISM ISADS None

head

Is the order of instructions important?

2.3.1. Implementing a List using a singly linked list(addFirst method, pseudo-code)

Page 67: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

67

1

head

10 35 None

1

head

10 35 None

head

5 31 None

2.3.1. Implementing a List using a singly linked list(removeFirst method)

Page 68: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

68

Algorithm removeFirst(L):e=L.head.element #1L.head=L.head.next #2L.size=L.size-1 #3return e #4

It does not work when L is empty!!!

2.3.1. Implementing a List using a singly linked list(removeFirst method, pseudo-code)

Page 69: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

69

Algorithm removeFirst(L):If L.head is None then

Show an error: list is empty

e=L.head.element #1L.head=L.head.next #2L.size=L.size-1 #3return e #4

2.3.1. Implementing a List using a singly linked list(removeFirst method, pseudo-code)

Page 70: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

5 61 None

head

8

newNode

None

2.3.1. Implementing a List using a singly linked list(addLast method)

Page 71: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

5 61 None

head

8

newNode

None

current

current = first

2.3.1. Implementing a List using a singly linked list(addLast method)

Page 72: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

5 61 None

head

8

newNode

None

current

current = current.next

2.3.1. Implementing a List using a singly linked list(addLast method)

Page 73: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

5 61 None

head

8

newNode

None

current

current = current.next

The last node is reached when current.next=None

2.3.1. Implementing a List using a singly linked list(addLast method)

Page 74: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

5 61

head

8

newNode

None

current

current.next= newNode

The last node must point to the new node

2.3.1. Implementing a List using a singly linked list(addLast method)

Page 75: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

75

Algorithm addLast(L,e):newNode=Node(e) current=L.headwhile current.next is not None:

current=current.nextcurrent.next=newNodeL.size=L.size+1

Does this code work if L is empty?

2.3.1. Implementing a List using a singly linked list(addLast method, pseudo-code)

Page 76: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

76

Algorithm addLast(L,e):if L.head==None:

addFirst(L,e)

newNode=Node(e) current=L.headwhile current.next is not None:

current=current.nextcurrent.next=newNodeL.size=L.size+1

2.3.1. Implementing a List using a singly linked list(addLast method, pseudo-code)

Page 77: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

77

1

head

10 35 None

1

head

10 35 None

The penultimate node must point to None with its next attribute.

None

2.3.1. Implementing a List using a singly linked list(removeLast method)

Page 78: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

78

Algorithm removeLast(L): if L.head is None: show an error return None

previous=None current=self.head while current.next is not None: previous=current current=current.next e=current.element previous.next=None self.size=self.size-1 return e

2.3.1. Implementing a List using a singly linked list(removeLast method, pseudo-code)

Page 79: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

79

Algorithm getAt(L,index): if index<0 or index>=L.size: show “index out of index” return None

i=0 current=self.head while i<index: current=current.next i = i+1

return current.element

2.3.1. Implementing a List using a singly linked list(getAt method, pseudo-code)

Page 80: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

80

● Implement the following methods:○ contains(L,e):returns the first position of the element

e at the list. If e does not exist, then it returns -1.○ insertAt(L,index,e): inserts the element e at the

position index of the list L.○ removeAt(L,index): removes the element at the

position index from the list L.● Implement a stack with a singly linked list.● Implement a queue with a singly linked list.

2.3.1. Implementing a List using a singly linked list(Exercises)

Page 81: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

81

5 3 610 None

● Implement a singly linked list that also includes a reference to the last node (tail).

head tail

2.3.1. Implementing a List using a singly linked list(Exercises)

Page 82: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

82

51230

5

1

2

3

0 None

mem

ory

head

mem

ory

Python List class Singly Linked List

0

1

2

3

4

2.3.1. Implementing a List using a singly linked list(Python List versus Singly Linked List)

Page 83: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

83

Python List Implementation Singly Linked List

(+) Fast and Easy access to the elements.(-) Insertion and remove operations are slow (as shifting elements is required)

(-) Requires more space(-) Sequential access ( you must visit all the previous elements)(+) Insertion and remove operations are more efficient.

2.3.1. Implementing a List using a singly linked list(Python List versus Singly Linked List)

Page 84: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

84

● Definition of List ADT● How Python lists are implemented internally 2.3.1. Implementing a list ADT using a singly linked list 2.3.2. Implementing a list ADT using a doubly linked list

2.3. Index of List ADT.

Page 85: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

How improve the access to the nodes?

85

● In addition to the reference next, a node has an additional reference to the previous node (prev).

● It allows visiting the list from left to right, and also in reverse.

element nextprev element nextprevelement nextprev

2.3.2. Implementing a List using a doubly linked list

Page 86: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

86

element nextprev

Algorithm Node(node,e):node.element=e node.next=Nonenode.prev=None

node

2.3.2. Implementing a List using a doubly linked list (pseudocode for doubly node constructor)

Page 87: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

87

● Use two references:○ head: points to the first node of the list.○ tail: points to the last node of the list.

1 3 2 10

head tail

None None

2.3.2. Implementing a List using a doubly linked list

Page 88: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

88

● The constructor creates an empty list. ● head and tail must be None.

Algorithm DList(list):list.head=None list.tail=Nonelist.size=0

2.3.2. Implementing a List using a doubly linked list (pseudocode for Doubly Linked List constructor)

Page 89: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

89

● addFirst(L,e) adds the element e at the front of the list L.● addLast(L,e) adds the element e at the tail of the list L.● removeFirst(L) removes the first element of the list L. It

returns the element. ● removeLast(L) removes the last element of the list L. It

returns the element.● insertAt(L,index,e) inserts the element e at the index

position of the list.

2.3.2. Implementing a List using a doubly linked list

Page 90: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

for example, l.addFirst(5)90

1) Create a new node.2) Link the new node to the list3) Update the head reference 4) Increase the size

5 NoneNone

1 3 2 10

head tail

None None

newNode=Node(e)newNode

2.3.2. Implementing a List using a doubly linked list (addFirst method)

Page 91: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

91

1) Create a new node.2) Link the new node to the list3) Update the head reference 4) Increase the size

5 1 3 2 10

head tail

newNode.next = L.head

NoneNone

NonenewNode

2.3.2. Implementing a List using a doubly linked list (addFirst method)

Page 92: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

92

1) Create a new node.2) Link the new node to the list3) Update the head reference 4) Increase the size

5 1 3 2 10

head tail

newNode.next = L.headL.head.prev=newNode

NoneNonenewNode

2.3.2. Implementing a List using a doubly linked list (addFirst method)

Page 93: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

93

1) Create a new node.2) Link the new node to the list3) Update the head reference 4) Increase the size

5 1 3 2 10

tail

L.head=newNode

NoneNone

head head

newNode

2.3.2. Implementing a List using a doubly linked list (addFirst method)

Page 94: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

94

1) Create a new node.2) Link the new node to the list3) Update the head reference 4) Increase the size

5 1 3 2 10

head tail

L.size=L.size+1

NoneNone

2.3.2. Implementing a List using a doubly linked list (addFirst method)

Page 95: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

95

Algorithm addFirst(L,e):newNode=Node(e) newNode.next=L.headL.head.prev=newNodeL.head=newNodeL.size=L.size+1

2.3.2. Implementing a List using a doubly linked list (addFirst method, pseudo-code)

Page 96: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

96

Algorithm addFirst(L,e):newNode=Node(e) newNode.next=L.headL.head.prev=newNodeL.head=newNodeL.size=L.size+1

Does it work when the list is empty?

2.3.2. Implementing a List using a doubly linked list (addFirst method, pseudo-code)

Page 97: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

97

● If the list is empty:1) Create a new node.2) Update the references head and tail for

pointing to the new node.3) Increase the size

5

head tail

NoneNone

size=1

2.3.2. Implementing a List using a doubly linked list (addFirst method, pseudo-code)

Page 98: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

98

Algorithm addFirst(L,e):if L.head=None:newNode=Node(e) L.tail=newNodeL.head=newNodeL.size=L.size+1

else:…

2.3.2. Implementing a List using a doubly linked list (addFirst method, pseudo-code)

Page 99: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

99

Algorithm addFirst(L,e):newNode=Node(e)

if L.head=None:L.tail=newNode

else:newNode.next=L.headL.head.prev=newNode

L.head=newNodeL.size=L.size+1

2.3.2. Implementing a List using a doubly linked list (addFirst method, pseudo-code)

Page 100: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

100

● addFirst(L,e) adds the element e at the front of the list L.● addLast(L,e) adds the element e at the tail of the list L.● removeFirst(L) removes the first element of the list L. It

returns the element. ● removeLast(L) removes the last element of the list L. It

returns the element.● insertAt(L,index,e) inserts the element e at the index

position of the list.

2.3.2. Implementing a List using a doubly linked list

Page 101: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

for example, l.addLast(4)

101

1) Create a new node.2) Link the new node to the list3) Update the tail reference 4) Increase the size

4

1 3 2 10

head tail

NoneNone

NoneNone

newNode=Node(e)newNode

2.3.2. Implementing a List using a doubly linked list (addLast method)

Page 102: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

102

1) Create a new node.2) Link the new node to the list3) Update the tail reference 4) Increase the size

newNode.prev=L.tail

1 3 2 10

head tail

None 4 None

newNodeNone

2.3.2. Implementing a List using a doubly linked list (addLast method)

Page 103: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

103

1) Create a new node.2) Link the new node to the list3) Update the tail reference4) Increase the size

newNode.prev=L.tailL.tail.next = newNode

1 3 2 10

head tail

None 4 None

newNode

2.3.2. Implementing a List using a doubly linked list (addLast method)

Page 104: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

104

1) Create a new node.2) Link the new node to the list3) Update the tail reference 4) Increase the size

L.tail = newNode

1 3 2 10

headtail

None 4 None

newNode

2.3.2. Implementing a List using a doubly linked list (addLast method)

Page 105: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

105

1) Create a new node.2) Link the new node to the list3) Update the tail reference 4) Increase the size

L.size = L.size + 1

1 3 2 10

head tailNone 4 None

2.3.2. Implementing a List using a doubly linked list (addLast method)

Page 106: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

106

Algorithm addLast(L,e):newNode=Node(e) newNode.prev=L.tailL.tail.next=newNodeL.tail=newNodeL.size=L.size+1

2.3.2. Implementing a List using a doubly linked list (addLast method, pseudo-code)

Page 107: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

107

Algorithm addLast(L,e):newNode=Node(e) newNode.prev=L.tailL.tail.next=newNodeL.tail=newNodeL.size=L.size+1

Does it work when the list is empty?

2.3.2. Implementing a List using a doubly linked list (addLast method, pseudo-code)

Page 108: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

108

● If the list is empty:1) Create a new node.2) Head and tail references must point to the

new node.3) Increase the size

4

head tail

NoneNone

2.3.2. Implementing a List using a doubly linked list (addLast method, pseudo-code)

Page 109: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

109

Algorithm addLast(L,e):if L.head=None:newNode=Node(e) L.tail=newNodeL.head=newNodeL.size=L.size+1

else:…

2.3.2. Implementing a List using a doubly linked list (addLast method, pseudo-code)

Page 110: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

110

Algorithm addLast(L,e):newNode=Node(e)

if L.head=None:L.head=newNode

else:newNode.prev=L.tailL.tail.next=newNode

L.tail=newNodeL.size=L.size+1

2.3.2. Implementing a List using a doubly linked list (addLast method, pseudo-code)

Page 111: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

111

● addFirst(L,e) adds the element e at the front of the list L.● addLast(L,e) adds the element e at the tail of the list L.● removeFirst(L) removes the first element of the list L. It

returns the element. ● removeLast(L) removes the last element of the list L. It

returns the element.● insertAt(L,index,e) inserts the element e at the index

position of the list.

2.3.2. Implementing a List using a doubly linked list

Page 112: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

112

1) Store the element into a variable2) Update the head reference 3) Update the head.prev reference to None4) Decrease the size5) Return the result.

5 1 3 2 10

head tail

NoneNone

result=L.head.element

2.3.2. Implementing a List using a doubly linked list (removeFirst method)

Page 113: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

113

1) Store the element into a variable2) Update the head reference 3) Update the head.prev reference to None4) Decrease the size5) Return the result

5 1 3 2 10

head tail

L.head=L.head.next

NoneNone

head

2.3.2. Implementing a List using a doubly linked list (removeFirst method)

Page 114: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

114

1) Store the element into a variable2) Update the head reference 3) Update the head.prev reference to None4) Decrease the size5) Return the result

5 1 3 2 10

head tail

L.head.prev=None

NoneNone

head

None

2.3.2. Implementing a List using a doubly linked list (removeFirst method)

Page 115: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

115

1) Store the element into a variable2) Update the head reference 3) Update the head.prev reference to None4) Decrease the size5) Return the result

5 1 3 2 10

head tail

L.size = L.size - 1

None

head

None

2.3.2. Implementing a List using a doubly linked list (removeFirst method)

Page 116: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

116

1) Store the element into a variable2) Update the head reference 3) Update the head.prev reference to

None4) Decrease the size5) Return the result

1 3 2 10

tail

return result

None

head

None

2.3.2. Implementing a List using a doubly linked list (removeFirst method)

Page 117: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

117

Algorithm removeFirst(L):result=L.head.elementL.head= L.head.nextL.head.prev = NoneL.size=L.size-1return result

2.3.2. Implementing a List using a doubly linked list (removeFirst method, pseudo-code)

Page 118: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

118

Algorithm removeFirst(L):If L.isEmpty():“show an error”return None

result=L.head.elementL.head= L.head.nextL.head.prev = NoneL.size=L.size-1return result

2.3.2. Implementing a List using a doubly linked list (removeFirst method, pseudo-code)

Page 119: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

119

Algorithm removeFirst(L):If L.isEmpty():“show an error”return None

result=L.head.elementL.head= L.head.nextL.head.prev = NoneL.size=L.size-1return result

For what case does this algorithm does not work?

2.3.2. Implementing a List using a doubly linked list (removeFirst method, pseudo-code)

Page 120: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

Special case: the list only has one node

120

4

head tailNoneNone

Algorithm removeFirst(L):If L.isEmpty():

“show an error”return None

result=L.head.elementL.head= L.head.nextL.head.prev = NoneL.size=L.size-1return result

2.3.2. Implementing a List using a doubly linked list (removeFirst method, pseudo-code)

Page 121: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

121

4

head

tailNoneNone

Algorithm removeFirst(L):If L.isEmpty():

“show an error”return None

result=L.head.elementL.head= L.head.nextL.head.prev = NoneL.size=L.size-1return result

None

2.3.2. Implementing a List using a doubly linked list (removeFirst method, pseudo-code)

Special case: the list only has one node.The tail reference is not updated!!!

Page 122: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

122

4

head

tailNoneNone

Algorithm removeFirst(L):If L.isEmpty():

“show an error”return None

result=L.head.elementL.head= L.head.nextif L.head is None:

L.tail=Noneelse:

L.head.prev = NoneL.size=L.size-1return result

None

tail None

2.3.2. Implementing a List using a doubly linked list (removeFirst method, pseudo-code)

Page 123: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

123

Algorithm removeFirst(L):If L.isEmpty():

“show an error”return None

result=L.head.elementL.head= L.head.nextif L.head is None:

L.tail=Noneelse:

L.head.prev = NoneL.size=L.size-1return result

2.3.2. Implementing a List using a doubly linked list (removeFirst method, pseudo-code)

Page 124: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

124

● addFirst(L,e) adds the element e at the front of the list L.● addLast(L,e) adds the element e at the tail of the list L.● removeFirst(L) removes the first element of the list L. It

returns the element. ● removeLast(L) removes the last element of the list L. It

returns the element.● insertAt(L,index,e) inserts the element e at the index

position of the list.

2.3.2. Implementing a List using a doubly linked list

Page 125: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

125

1) Store the element into a variable2) Update the tail reference 3) Update the tail.next reference to None4) Decrease the size5) Return the result.

5 1 3 2 10

head tail

NoneNone

result=L.tail.element

2.3.2. Implementing a List using a doubly linked list (removeLast method)

Page 126: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

126

1) Store the element into a variable2) Update the tail reference 3) Update the tail.next reference to None4) Decrease the size5) Return the result.

5 1 3 2 10

head tail

NoneNone

L.tail = L.tail.prev

tail

2.3.2. Implementing a List using a doubly linked list (removeLast method)

Page 127: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

127

1) Store the element into a variable2) Update the tail reference 3) Update the tail.next reference to None4) Decrease the size5) Return the result.

5 1 3 2 10

head tailNoneNone

L.tail.next = None

tail

2.3.2. Implementing a List using a doubly linked list (removeLast method)

Page 128: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

128

1) Store the element into a variable2) Update the tail reference 3) Update the tail.next reference to None4) Decrease the size5) Return the result.

5 1 3 2

headNoneNone

L.size = L.size -1

tail

2.3.2. Implementing a List using a doubly linked list (removeLast method)

Page 129: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

129

1) Store the element into a variable2) Update the tail reference 3) Update the tail.next reference to None4) Decrease the size5) Return the result.

5 1 3 2

headNoneNone

return result

tail

2.3.2. Implementing a List using a doubly linked list (removeLast method)

Page 130: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

130

Algorithm removeLast(L):If L.isEmpty():“show an error”return None

result=L.tail.elementL.tail= L.tail.prevL.tail.next = NoneL.size=L.size-1return result

2.3.2. Implementing a List using a doubly linked list (removeLast method, pseudo-code)

Page 131: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

131

Algorithm removeLast(L):If L.isEmpty():“show an error”return None

result=L.tail.elementL.tail= L.tail.prevL.tail.next = NoneL.size=L.size-1return result

This doen’s work if the list only has one node

2.3.2. Implementing a List using a doubly linked list (removeLast method, pseudo-code)

Page 132: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

132

4

head

tail

NoneNone

Algorithm removeLast(L):If L.isEmpty():

“show an error”return None

result=L.tail.elementL.tail= L.tail.prevL.tail.next = NoneL.size=L.size-1return result

Special case: The list only has one nodeThe head reference is not updated!!!

None

2.3.2. Implementing a List using a doubly linked list (removeLast method, pseudo-code)

Page 133: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

133

Algorithm removeLast(L):If L.isEmpty():

“show an error”return None

result=L.tail.elementL.tail= L.tail.previf L.tail is None:

L.head = Noneelse:

L.tail.next = NoneL.size=L.size-1return result

2.3.2. Implementing a List using a doubly linked list (removeLast method, pseudo-code)

Page 134: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

134

● addFirst(L,e) adds the element e at the front of the list L.● addLast(L,e) adds the element e at the tail of the list L.● removeFirst(L) removes the first element of the list L. It

returns the element. ● removeLast(L) removes the last element of the list L. It

returns the element.● insertAt(L,index,e) inserts the element e at the index

position of the list.

2.3.2. Implementing a List using a doubly linked list

Page 135: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

135

1) Create a new node2) Traverse to the node at the given index3) Link the new node to the list4) Increase the size

A B C D E

head tail

NoneNone

0 1 2 3 4

P NoneNonenewNode=Node(e)newNode

2.3.2. Implementing a List using a doubly linked list (insertAt method)

for example, l.insertAt(3,’P’)

Page 136: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

136

1) Create a new node2) Traverse to the node at the given index3) Link the new node to the list4) Increase the size

A B C D E

head tail

NoneNone

0 1 2 3 4

P NoneNone

newNode

2.3.2. Implementing a List using a doubly linked list (insertAt method)

Page 137: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

137

1) Create a new node2) Traverse to the node at the given index3) Link the new node to the list4) Increase the size

A B C D E

head tail

NoneNone

0 1 2 3 4

P NoneNone

newNode

aux= L.head

aux, i=0 while i <index:

aux=aux.nexti=i+1

2.3.2. Implementing a List using a doubly linked list (insertAt method)

Page 138: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

138

1) Create a new node2) Traverse to the node at the given index3) Link the new node to the list4) Increase the size

A B C D E

head tail

NoneNone

0 1 2 3 4

P NoneNone

newNode

aux,i=1

while i <index:aux=aux.nexti=i+1

2.3.2. Implementing a List using a doubly linked list (insertAt method)

Page 139: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

139

A B C D E

head tail

NoneNone

0 1 2 3 4

P NoneNone

newNode

aux,i=2

while i <index:aux=aux.nexti=i+1

2.3.2. Implementing a List using a doubly linked list (insertAt method)

1) Create a new node2) Traverse to the node at the given index3) Link the new node to the list4) Increase the size

Page 140: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

140

1) Create a new node2) Traverse to the node at the given index3) Link the new node to the list4) Increase the size

A B C D E

head tail

NoneNone

0 1 2 3 4

P NoneNone

newNode

aux, i=3

while i <index:aux=aux.nexti=i+1

False

2.3.2. Implementing a List using a doubly linked list (insertAt method)

Page 141: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

141

1) Create a new node2) Traverse to the node at the given index3) Link the new node to the list4) Increase the size

A B C D E

head tail

NoneNone

0 1 2 3 4

P

newNode

auxprevious

2.3.2. Implementing a List using a doubly linked list (insertAt method)

Page 142: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

142

1) Create a new node2) Traverse to the node at the given index3) Link the new node to the list4) Increase the size

A B C D E

head tail

NoneNone

0 1 2 3 4

P newNode.next=aux

newNode

auxprevious

2.3.2. Implementing a List using a doubly linked list (insertAt method)

Page 143: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

143

1) Create a new node2) Traverse to the node at the given index3) Link the new node to the list4) Increase the size

A B C D E

head tail

NoneNone

0 1 2 3 4

P newNode.next=aux

newNode

aux

newNode.prev=previous

previous

2.3.2. Implementing a List using a doubly linked list (insertAt method)

Page 144: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

144

1) Create a new node2) Traverse to the node at the given index3) Link the new node to the list4) Increase the size

A B C D E

head tail

NoneNone

0 1 2 3 4

P aux.prev=newNode

newNode

auxprevious

2.3.2. Implementing a List using a doubly linked list (insertAt method)

Page 145: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

145

1) Create a new node2) Traverse to the node at the given index3) Link the new node to the list4) Increase the size

A B C D E

head tail

NoneNone

0 1 2 3 4

P aux.prev=newNode

newNode

auxprevious

previous.next=newNode

2.3.2. Implementing a List using a doubly linked list (insertAt method)

Page 146: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

146

1) Create a new node2) Traverse to the node at the given index3) Link the new node to the list4) Increase the size

A B C

tail

NoneNone0 1 2

L.size = L.size + 1

P3

D4

headE

5

2.3.2. Implementing a List using a doubly linked list (insertAt method)

Page 147: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

147

Algorithm insertAt(L,index,e):If index<0 or index>L.size:

“show an error”return

i=0aux=L.headwhile i<index:

aux=aux.nexti=i+1

#aux is the node at the index positionprevious=aux.prevnewNode=Node(e)newNode.next=auxnewNode.prev=previousaux.prev=newNodeprevious.next=newNodeL.size= L.size+1

2.3.2. Implementing a List using a doubly linked list (insertAt method, pseudo-code)

Page 148: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

148

Algorithm insertAt(L,index,e):If index<0 or index>L.size:

“show an error”return

i=0aux=L.headwhile i<index:

aux=aux.nexti=i+1

#aux is the node at the index positionprevious=aux.prevnewNode=Node(e)newNode.next=auxnewNode.prev=previousaux.prev=newNodeprevious.next=newNodeL.size= L.size+1

Warning!!!! If index=0, previous is None

Warning!!!! If index=size, aux is None

2.3.2. Implementing a List using a doubly linked list (insertAt method, pseudo-code)

Page 149: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

149

Algorithm insertAt(L,index,e):If index<0 or index>L.size:

“show an error”return

if index==0 L.addFirst(e)

else if index==L.size L.addLast(e)

elsei=0aux=L.headwhile i<index:

aux=aux.nexti=i+1

#aux is the node at the index positionprevious=aux.prevnewNode=Node(e)newNode.next=auxnewNode.prev=previousaux.prev=newNodeprevious.next=newNodeL.size= L.size+1

2.3.2. Implementing a List using a doubly linked list (insertAt method, pseudo-code)

Page 150: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

150

● Implement the rest of the methods:○ getAt(L,index) returns the element at the index

position○ contains(L,e) returns the first index of the element in

the list. If e does not exist, then it returns -1.○ removeAt(L,index) removes the element at the index

position. ○ show(L,opc):

■ If opc=0, it prints the elements of the list.■ if opc=1, it prints the elements of the list in reverse

order (from right to left).

2.3.2. Implementing a List using a doubly linked list (exercises)

Page 151: Datos, 2018-2019 Grado en Ciencia e Ingeniería de Linear Abstract Data Type …ocw.uc3m.es/ingenieria-informatica/data-structures-and... · 2019-10-28 · 2.3. List Abstract Data

151

● A palindrome word is one that reads the same backward as forward.

● Examples:○ Anna, Level, Civic, Madam, Noon.

● Implement a Python function that takes a word and returns true if it is palindrome, else false.

● In your solution, you have to use a doubly linked list where each node contains only one character of the input word.

2.3.2. Implementing a List using a doubly linked list (exercises)