Linked List Chapter 3 1. 2 Data Abstraction separates the logical properties of a data type from its...

Preview:

Citation preview

1

Linked List

Chapter 3

2

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?

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;

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

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

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/

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

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

10

Nodes can be located anywhere inmemory

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

11

Traversing a Linked List (single)

12

13

14

15

16

17

18

19

20

21

22

Figure 3.1 (text book)

23

24

Algorithm from e to h

25

Insertion - Algorithm (beginning)

26

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

27

Insertion - Algorithm (end)

28

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

29

Deleting a node at the beginning of a singly linked list

30

Deleting a node at the end of a singly linked list

31

Deleting a node from a singly linked list

32

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

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

35

Inserting in the Front

36

Inserting in the Front (cont.)

37

Inserting in the Front (cont.)

38

Inserting in the Front (cont.)

39

Inserting in the Front (cont.)

40

Deleting element

Try your own solution

41

Circular Lists

• Nodes form a ring• Each node has a successor

42

Inserting a Node at the Tail of aList Circular Lists

43

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

44

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

45

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

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

47

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

Recommended