Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 17: Linked Lists.

Similar presentations


Presentation on theme: "Chapter 17: Linked Lists."— Presentation transcript:

1 Chapter 17: Linked Lists

2 Introduction to Linked List
17.1 Introduction to Linked List

3 Introduction to Linked List
A linked list is a series of connected nodes, where each node is a data structure. A linked list can grow or shrink in size as the program runs Nodes are dynamically allocated

4 Node Organization A node contains:
data: one or more data fields – may be organized as structure, object, etc. a pointer that can point to another node pointer data

5 Declaring a Node Declare a node: struct ListNode { };

6 Declaring a Node Declare a node:
struct ListNode { int data; ListNode *next; }; The pointer can hold the address of any object that is a ListNode structure. Self-referential data structure

7 Linked List Organization
Linked list contains 0 or more nodes: Has a list head to point to first node Last node points to NULL NULL list head

8 NULL NULL is a named constant, defined in the iostream file, that stands for address 0. The address 0 is considered an unusable address. Most computers store special operating system data structures in the lower areas of memory.

9 Empty List If a list currently contains 0 nodes, it is the empty list

10 Empty List If a list currently contains 0 nodes, it is the empty list
In this case the list head points to NULL NULL list head

11 Linked List Operations
17.2 Linked List Operations

12 Linked List Operations
Basic operations: append a node to the end of the list insert a node within the list traverse the linked list delete a node delete/destroy the list

13 Advantage of Linked Lists
Advantage of linked lists over arrays It is faster to insert a node into a linked list or delete one from the list

14 Linked Lists vs. Arrays Linked lists can insert a node between other nodes easily NULL list head

15 Variations of the Linked List
Other linked list organizations: doubly-linked list: each node contains two pointers: one to the next node in the list, one to the previous node in the list NULL list head 5 13 19

16 Variations of the Linked List
Other linked list organizations: circular linked list: the last node in the list points back to the first node in the list, not to NULL list head 5 13 19

17 The STL list Container Template for a doubly linked list
Member functions for adding elements to the list: insert, push_back, push_front removing elements from the list: erase, pop_back, pop_front


Download ppt "Chapter 17: Linked Lists."

Similar presentations


Ads by Google