CMPT 225 Lecture 5 – linked list.

Slides:



Advertisements
Similar presentations
Lists CS 3358.
Advertisements

Linked Lists.
Linear Lists – Linked List Representation
DATA STRUCTURES USING C++ Chapter 5
Data Structures Using C++
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Data Structures Using C++ 2E
Data Structures Using Java1 Chapter 4 Linked Lists.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
Data Structures Using C++1 Chapter 5 Linked Lists.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
CS32 Discussion Section 1B Week 3 TA: Hao Yu (Cody)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
UNIT-II Topics to be covered Singly linked list Circular linked list
LINKED LISTS.
Unit 1 - Introducing Abstract Data Type (ADT) Part 3 – Slides 24 to end.
1 Linked List. List vs Arrays Two built-in data structures that can be used to organize data, or to create other data structures: Lists Arrays.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Chapter 3: Fundamental Data Structures: The Array and Linked Structures Data Structures in Java: From Abstract Data Types to the Java Collections Framework.
Chapter 16: Linked Lists.
CSE 1342 Programming Concepts
Linked List ADT used to store information in a list
Unit – I Lists.
C++ Programming:. Program Design Including
Week 4 - Monday CS221.
Data Structure By Amee Trivedi.
Anatomy of a linked list
Linked List :: Basic Concepts
Lectures linked lists Chapter 6 of textbook
Program based on queue & their operations for an application
UNIT – I Linked Lists.
Review Deleting an Element from a Linked List Deletion involves:
Lists CS 3358.
UNIT-3 LINKED LIST.
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
CSCI 3333 Data Structures Linked Lists.
Linked List Sudeshna Sarkar.
Programmazione I a.a. 2017/2018.
LINKED LISTS CSCD Linked Lists.
Top Ten Words that Almost Rhyme with “Peas”
Prof. Neary Adapted from slides by Dr. Katherine Gibson
CMSC 341 Lecture 5 Stacks, Queues
Linked Lists.
Linked Lists.
Arrays and Linked Lists
Chapter 18: Linked Lists.
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Linked Lists.
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
11-3 LINKED LISTS A linked list is a collection of data in which each element contains the location of the next element—that is, each element contains.
Chapter 17: Linked Lists.
Lecture 16 Section 6.2 Thu, Mar 1, 2007
Lecture 14 Linked Lists CSE /26/2018.
Linked Lists.
Linked Lists.
CS148 Introduction to Programming II
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
BY PROF. IRSHAD AHMAD LONE.
CMPT 225 Lecture 6 – Review of Complexity Analysis using the Big O notation + Comparing List ADT class implementations.
Lecture 3 – Data collection List ADT
Lecture 4 – Data collection List ADT
CMPT 225 Lecture 7 – Stack.
CMPT 225 Lecture 8 – Queue.
CMPT 225 Lecture 16 – Heap Sort.
Presentation transcript:

CMPT 225 Lecture 5 – linked list

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

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++

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

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

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

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++; }

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++; }

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

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++; }

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

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--; }

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??? }

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

Removal – with previous @ end

Insertion - Improvement @ end

doubly headed singly linked list Advantage: Disadvantage:

Removal – Improvement @ end

singly headed doubly linked list Advantage: Disadvantage:

doubly headed doubly linked list Advantage: Disadvantage:

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)

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

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

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)

Implementation - Node Class

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

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

√ 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

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