Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMPT 225 Lecture 5 – linked list.

Similar presentations


Presentation on theme: "CMPT 225 Lecture 5 – linked list."— Presentation transcript:

1 CMPT 225 Lecture 5 – linked list

2 Last Lectures Overview of data collections
Introduce our first data collection: List Design the List as an ADT Design its public section (the gaps in the wall) 2. Design its private section (what is hidden behind the wall) Different types of List: Position-oriented versus value- oriented Lists Look at arrays (one of the concrete data structures) Implement a List ADT using an array Differentiate between stack-allocated and heap- allocated arrays Test the List ADT Introduce test cases and a test driver

3 Learning Outcomes At the end of this lecture, a student will be able to: define one of the concrete data types, namely linked list, and demonstrate, simulate, and trace its operations write C++ code manipulate pointers manage memory in C++

4 Today’s Menu Introduce 2nd data structure (concrete data type)
Pointers and linked lists Link-based implementation of List ADT class

5 Introducing 2nd data structure: linked list
Node Node Node Made of pointers and nodes Characteristics: Adv: Flexible/unbounded size: it grows or shrinks as needed Disadv: Sequential access Elements must be accessed in some specific order dictated by the links Head

6 What can we do with a linked list?
Insert element into it Remove element from it No shifting required Traverse (iterate through) a linked list for various purposes such as displaying each element Search for a particular element Concatenate linked lists

7 Insert an element into a linked list
@ front ... insert(int newElement) // insert of List ADT class Node *newNode = new Node(newElement); if (newNode != NULL){ newNode->next = head; // Head NULL or not head = newNode; elementCount++; }

8 Insert an element into a linked list
Will our code work in all situations? Here, will our code work when linked list is empty: ... insert(int newElement) // insert of List ADT class Node *newNode = new Node(newElement); if (newNode != NULL){ newNode->next = head; // Head NULL or not head = newNode; elementCount++; }

9 Traverse a linked list // Anchor head of linked list
Node* current = head; while (current -> next != NULL) current = current -> next;

10 Insert an element into a linked list
@ end ... insert(int newElement) Node *newNode = new Node(newElement); if (newNode != NULL){ if (head == NULL) head = newNode; else { // Move to the end of the list Node* current = head; // Anchor while (current -> next != NULL) current = current -> next; current -> next = newNode; } elementCount++; }

11 Insert an element into a linked list
@ specific location When linked list is used as a data structure for a position-oriented data collection ADT like a List ADT OR When linked list is used as a data structure for a value-oriented data collection ADT like a List ADT, kept in sorted order

12 Remove an element from a linked list
@ front ... remove( ) if (head != NULL) { Node* nodeToRemove = head; head = head -> next; // Return node to the system nodeToRemove -> next = NULL; delete nodeToRemove; nodeToRemove = NULL; elementCount--; }

13 Remove an element from a linked list
@ end ... remove( ) if (head != NULL) { // Move to the end of the list Node* current = head; // Anchor while (current -> next != NULL) current = current -> next; // Then what??? }

14 Issue with Traverse Using “current -> next”?
Using “current -> next -> next”? -> called “Look Ahead” Advantage: Disadvantage:

15 Removal – with previous
@ end

16 Insertion - Improvement
@ end

17 doubly headed singly linked list
Advantage: Disadvantage:

18 Removal – Improvement @ end

19 singly headed doubly linked list
Advantage: Disadvantage:

20 doubly headed doubly linked list
Advantage: Disadvantage:

21 Removal @ specific location
When linked list is used as a data structure for a position-oriented data collection ADT like our List ADT OR When linked list is used as a data structure for a value-oriented data collection ADT like our List ADT (kept in sorted or unsorted order)

22 linked lists are very flexible
singly headed singly linked circular list singly headed doubly linked circular list

23 Be Careful Do not confuse data members of List ADT class (components of a linked list) Such as head and tail with local variables of various List ADT methods manipulating the linked list Such as current and previous and with next and back data members of the Node class

24 Activity - Problem Statement
We are asked to develop a software application to manage customer accounts These accounts are to be printed very often either in … ascending alphabetical sort order of customer last name, OR ascending numerical sort order of customer SIN Let’s design the underlying data structure of our ADT class Let’s create a linked list that would allow us to perform these operations in O(n)

25 Implementation - Node Class

26

27 List ADT class { List.h List position-oriented List class ADT.
January 2019 {

28 Construct a List object (containing a linked list)
CODE:

29 √ Learning Check We can now … Create linked list
Perform operations on a linked list Create a Node class Implement a List ADT class: Using an array Using a linked-list

30 Next Lecture Review of the Big-O notation
Compare the two implementations of our List ADT class: Array-based implementation Link-based implementation


Download ppt "CMPT 225 Lecture 5 – linked list."

Similar presentations


Ads by Google