linked list chapter 3 1. 2 data abstraction separates the logical properties of a data type from its...

47
Linked List Chapter 3 1

Upload: ralph-edwards

Post on 13-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

1

Linked List

Chapter 3

Page 2: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

2

Page 3: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

3

Data Abstraction

• separates the logical properties of a data type from its implementation

• LOGICAL PROPERTIES– What are the possible values? – What operations will be needed?

• IMPLEMENTATION– How can this be done in C++?– How can data types be used?

Page 4: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

4

Data structures• A data structure is a group of data elements grouped

together under one name.• These data elements, known as members, can have

different types and different lengths. • Data structures are declared in C++ using the

following: syntax:struct structure_name {member_type1 member_name1;member_type2 member_name2;member_type3 member_name3;.} object_names;

struct product { int weight; float price;

} apple, banana, melon;

Page 5: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

5http://www.cplusplus.com/doc/tutorial/structures/

Page 6: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

6http://www.cplusplus.com/doc/tutorial/structures/

Page 7: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

7

Templates - Class templates• A template parameter is a special kind of parameter

that can be used to pass a type as argument:

http://www.cplusplus.com/doc/tutorial/

Page 8: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

8http://www.cplusplus.com/doc/tutorial/

Page 9: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

9

Linked List• a linked list is a list in which the order of the

components is determined by an explicit link member in each node

• the nodes are structs--each node contains a component member and also a link member that gives the location of the next node in the list

• an external pointer (or head pointer) points to the first node in the list

Page 10: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

10

Nodes can be located anywhere inmemory

• the link member holds the memory address of the next node in the list

Page 11: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

11

Traversing a Linked List (single)

Page 12: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

12

Page 13: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

13

Page 14: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

14

Page 15: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

15

Page 16: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

16

Page 17: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

17

Page 18: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

18

Page 19: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

19

Page 20: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

20

Page 21: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

21

Page 22: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

22

Figure 3.1 (text book)

Page 23: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

23

Page 24: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

24

Algorithm from e to h

Page 25: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

25

Insertion - Algorithm (beginning)

Page 26: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

26

Inserting a new node at the beginning of a singly linked list

Page 27: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

27

Insertion - Algorithm (end)

Page 28: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

28

Inserting a new node at the end of a singly linked list

Page 29: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

29

Deleting a node at the beginning of a singly linked list

Page 30: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

30

Deleting a node at the end of a singly linked list

Page 31: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

31

Deleting a node from a singly linked list

Page 32: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

32

Page 33: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

33

Doubly Linked Lists

• An extension of a Singly Linked List• Each node has two pointer

– One pointing to the successor– One pointing to the predecessor

• They are used because they ease certain operations like the delete Element

• They are interesting for traversal as you can move in either directions

Page 34: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

34

Doubly Linked Lists (Cont.)

• Most of our structures and algorithms will be implemented with the help of Doubly Linked Lists

• Although some operations are made easier to understand they also become a bit slower due to the overhead of extra pointers

• In addition to the head a pointer called tail is also maintained

Page 35: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

35

Inserting in the Front

Page 36: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

36

Inserting in the Front (cont.)

Page 37: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

37

Inserting in the Front (cont.)

Page 38: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

38

Inserting in the Front (cont.)

Page 39: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

39

Inserting in the Front (cont.)

Page 40: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

40

Deleting element

Try your own solution

Page 41: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

41

Circular Lists

• Nodes form a ring• Each node has a successor

Page 42: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

42

Inserting a Node at the Tail of aList Circular Lists

Page 43: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

43

Inserting a Node at the Tail of aList Circular Lists (cont.)

Page 44: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

44

Inserting a Node at the Tail of aList Circular Lists (cont.)

Page 45: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

45

Inserting a Node at the Tail of aList Circular Lists (cont.)

Page 46: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

46

Skip Lists

• Linked lists require sequential scanning for a search operation

• Skip lists allow for skipping certain nodes• A skip list is an ordered linked list• A Skip List with Evenly spaced nodes of

different levels

Page 47: Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the

47

Figure 3.17 (self study)Note: (no need) 3.5 to 3.8