Tải bản đầy đủ (.pptx) (85 trang)

C++ programming program design including data structure 7th ch16

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.15 MB, 85 trang )

Chapter 16:
Linked Lists


Objectives



In this chapter, you will:







Learn about linked lists
Become familiar with the basic properties of linked lists
Explore the insertion and deletion operations on linked lists
Discover how to build and manipulate a linked list
Learn how to implement linked lists as ADTs

C++ Programming: Program Design Including Data Structures, Seventh Edition

2


Objectives (cont’d)









Learn how to create linked list iterators
Learn how to implement the basic operations on a linked list
Learn how to create unordered linked lists
Learn how to create ordered linked lists
Learn how to construct a doubly linked list
Become familiar with circular linked lists

C++ Programming: Program Design Including Data Structures, Seventh Edition

3


Introduction




Data can be organized and processed sequentially using an array, called a sequential list
Problems with an array





Array size is fixed

Unsorted array: searching for an item is slow
Sorted array: insertion and deletion is slow because it requires data movement

C++ Programming: Program Design Including Data Structures, Seventh Edition

4


Linked Lists



Linked list: a collection of items (nodes) containing two components:




Data
Address (link) of the next node in the list

C++ Programming: Program Design Including Data Structures, Seventh Edition

5


Linked Lists (cont’d.)



Example:


– Link field in the last node is nullptr

C++ Programming: Program Design Including Data Structures, Seventh Edition

6


Linked Lists (cont’d.)



A node is declared as a class or struct






Data type of a node depends on the specific application
Link component of each node is a pointer

Variable declaration:

C++ Programming: Program Design Including Data Structures, Seventh Edition

7


Linked Lists: Some Properties




Example: linked list with four nodes (Figure 16-4)

C++ Programming: Program Design Including Data Structures, Seventh Edition

8


Linked Lists: Some Properties (cont’d.)



current = head;



Copies value of head into current

C++ Programming: Program Design Including Data Structures, Seventh Edition

9


Linked Lists: Some Properties (cont’d.)



current = current->link;


C++ Programming: Program Design Including Data Structures, Seventh Edition

10


Traversing a Linked List



Basic operations of a linked list:







Search for an item in the list
Insert an item in the list
Delete an item from the list

Traversal: given a pointer to the first node of the list, step through the nodes of the list

C++ Programming: Program Design Including Data Structures, Seventh Edition

11


Traversing a Linked List (cont’d.)




To traverse a linked list:



Example:

C++ Programming: Program Design Including Data Structures, Seventh Edition

12


Item Insertion and Deletion



Definition of a node:



Variable declaration:

C++ Programming: Program Design Including Data Structures, Seventh Edition

13


Insertion




To insert a new node with info 50 after p in this list:

C++ Programming: Program Design Including Data Structures, Seventh Edition

14


Insertion (cont’d.)

C++ Programming: Program Design Including Data Structures, Seventh Edition

15


Insertion (cont’d.)



Can use two pointers to simplify the insertion code somewhat:



To insert newNode between p and q:

C++ Programming: Program Design Including Data Structures, Seventh Edition

16



Insertion (cont’d.)

C++ Programming: Program Design Including Data Structures, Seventh Edition

17


Deletion



Node with info 34 is to be deleted:

C++ Programming: Program Design Including Data Structures, Seventh Edition

18


Deletion (cont’d.)



Node with info 34 is removed from the list, but memory is still occupied




Node is dangling

Must keep a pointer to the node to be able to deallocate its memory

C++ Programming: Program Design Including Data Structures, Seventh Edition

19


Deletion (cont’d.)

C++ Programming: Program Design Including Data Structures, Seventh Edition

20


Building a Linked List




If data is unsorted, the list will be unsorted
Can build a linked list forward or backward




Forward: a new node is always inserted at the end of the linked list
Backward: a new node is always inserted at the beginning of the list

C++ Programming: Program Design Including Data Structures, Seventh Edition


21


Building a Linked List Forward



Need three pointers to build the list:







One to point to the first node in the list, which cannot be moved
One to point to the last node in the list
One to create the new node

Example:



Data: 2 15 8 24 34

C++ Programming: Program Design Including Data Structures, Seventh Edition

22



Building a Linked List Forward (cont’d.)

C++ Programming: Program Design Including Data Structures, Seventh Edition

23


Building a Linked List Forward (cont’d.)

C++ Programming: Program Design Including Data Structures, Seventh Edition

24


Building a Linked List Forward (cont’d.)



Now repeat this process three more times:

C++ Programming: Program Design Including Data Structures, Seventh Edition

25


×