CMSC202 Computer Science II for Majors Lecture 12 – Linked Lists

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Lists CS 3358.
Linked Lists.
Linked Lists Linked Lists Representation Traversing a Linked List
CSE Lecture 12 – Linked Lists …
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
Comparison summary Array based (dynamic) Keeps place for up to 4N elements Each element takes 1 memory places Fast accession time Slow removals and insertion.
Main Index Contents 11 Main Index Contents Abstract Model of a List Obj. Abstract Model of a List Obj. Insertion into a List Insertion into a List Linked.
Linked Lists part II. Linked Lists A linked list is a dynamic data structure consisting of nodes and links: 627 start 8 This symbol indicates a null reference.
Chapter 3: Arrays, Linked Lists, and Recursion
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Chapter 5 Linked Lists II
CS 206 Introduction to Computer Science II 10 / 02 / 2009 Instructor: Michael Eckmann.
CS2006- Data Structures I Chapter 5 Linked Lists III.
List Interface and Linked List Mrs. Furman March 25, 2010.
Data Structure & Algorithms
1 Data Structures and Algorithms Outline This topic will describe: –The concrete data structures that can be used to store information –The basic forms.
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
UNIT-II Topics to be covered Singly linked list Circular linked list
Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists DeSiaMorewww.desiamore.com/ifm1.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
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.
Linked Data Structures
Lecture 6 of Computer Science II
Unit – I Lists.
Cpt S 122 – Data Structures Abstract Data Types
4. Linked Lists.
CSCI-255 LinkedList.
Program based on queue & their operations for an application
UNIT – I Linked Lists.
Prepared by, Jesmin Akhter, Lecturer, IIT, JU
Lists CS 3358.
Programming Abstractions
CSCI 3333 Data Structures Linked Lists.
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
CMSC 341 Lecture 10 B-Trees Based on slides from Dr. Katherine Gibson.
Topic 11 Linked Lists -Joel Spolsky
Circularly Linked Lists
Arrays and Linked Lists
Lesson Objectives Aims
Lecture 20 Linked Lists Richard Gesick.
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.
Programming Abstractions
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Doubly Linked List Implementation
Problem Understanding
Linked List and Selection Sort
Chapter 17: Linked Lists.
Introduction to C++ Linear Linked Lists
Lists.
Data Structures & Algorithms
CS148 Introduction to Programming II
CMSC202 Computer Science II for Majors Lecture 10 and 11 – Inheritance
General List.
Programming II (CS300) Chapter 07: Linked Lists
Doubly Linked List Implementation
Chapter 9 Linked Lists.
Topic 11 Linked Lists -Joel Spolsky
Problem Understanding
Presentation transcript:

CMSC202 Computer Science II for Majors Lecture 12 – Linked Lists Dr. Katherine Gibson

Last Class We Covered Inheritance Object relationships is-a (Inheritance) has-a (Composition and Aggregation)

Any Questions from Last Time?

Today’s Objectives To cover linked lists in detail Traversal Creation Insertion Deletion

Linked Lists vs Vectors

What is a Linked List? Data structure Uses nodes that contain Dynamic Allow easy insertion and deletion Uses nodes that contain Data Pointer to next node in the list

In these diagrams, a doubly-outlined box indicates a pointer. Example Linked List In these diagrams, a doubly-outlined box indicates a pointer. head tail data link data link data link data link NULL

Why Use Linked Lists? We already have vectors! What are some disadvantages of a vectors? Inserting in the middle of a vector takes time Deletion as well Sorting Requires a contiguous block of memory

Representation in Memory Vector location in memory Each cell is a block of memory First node of Linked List NULL

(Dis)Advantages of Linked Lists Change size easily and constantly Insertion and deletion can easily happen anywhere in the Linked List Only one node needs to be contiguously stored Disadvantages: Can’t access by index value Requires management of memory Pointer to next node takes up more memory

Nodes

Nodes A node is one element of a Linked List Nodes consist of two main parts: Data stored in the node Pointer to next node in list Often represented as classes data link

link can point to other nodes Code for Node Class class Node { String name; int testGrade; Node *link; // constructor // accessors // mutators }; link testGrade name NULL link can point to other nodes two options: another Node NULL

Linked List Overview

Example Linked List m_head NULL link link DUMMY link testGrade name

Important Points to Remember Last node in the Linked List points to NULL Each node points to either another node in the Linked List, or to NULL Only one link per node

Managing Memory with LLs Hard part of using Linked Lists is ensuring that none of the nodes go “missing” Think of Linked List as a train (Or as a conga line of Kindergarteners) Must keep track of where links point to If you’re not careful, nodes can get lost in memory (and you have no way to find them)

Linked List Functions What functions does a Linked List class implementation require? Linked_List constructor insert() remove() printList() isEmpty()

Linked Lists’ “Special” Cases Linked Lists often need to be handled differently under specific circumstances Linked List is empty Linked List has only one element Linked List has multiple elements Changing something with the first or last node Keep this in mind when you are coding Dummy nodes alleviate some of these concerns

Creating a Linked List On the Board

Traversing the List To control our traversal, we’ll use a loop Initialization, Termination Condition, Modification Set CURR to the first node in the list Continue until we hit the end of the list (NULL) Move from one node to another (using m_next)

Demonstration of Traversal FRONT CURR NULL link DUMMY link 91 Bob link 94 Eve NULL for (CURR = FRONT; CURR != NULL; CURR = CURR->link) {

Demonstration of Traversal FRONT CURR link DUMMY link 91 Bob link 94 Eve NULL for (CURR = FRONT; CURR != NULL; CURR = CURR->link) {

Demonstration of Traversal FRONT CURR link DUMMY link 91 Bob link 94 Eve NULL for (CURR = FRONT; CURR != NULL; CURR = CURR->link) {  // ignore, dummy node

Demonstration of Traversal FRONT CURR link DUMMY link 91 Bob link 94 Eve NULL for (CURR = FRONT; CURR != NULL; CURR = CURR->link) {

Demonstration of Traversal FRONT CURR link DUMMY link 91 Bob link 94 Eve NULL for (CURR = FRONT; CURR != NULL; CURR = CURR->link) {

Demonstration of Traversal FRONT CURR link DUMMY link 91 Bob link 94 Eve NULL for (CURR = FRONT; CURR != NULL; CURR = CURR->link) {  // print information (Bob)

Demonstration of Traversal FRONT CURR link DUMMY link 91 Bob link 94 Eve NULL for (CURR = FRONT; CURR != NULL; CURR = CURR->link) {

Demonstration of Traversal FRONT CURR link DUMMY link 91 Bob link 94 Eve NULL for (CURR = FRONT; CURR != NULL; CURR = CURR->link) {

Demonstration of Traversal FRONT CURR link DUMMY link 91 Bob link 94 Eve NULL for (CURR = FRONT; CURR != NULL; CURR = CURR->link) {  // print information (Eve)

Demonstration of Traversal FRONT CURR link DUMMY link 91 Bob link 94 Eve NULL for (CURR = FRONT; CURR != NULL; CURR = CURR->link) {

Demonstration of Traversal FRONT CURR NULL link DUMMY link 91 Bob link 94 Eve NULL for (CURR = FRONT; CURR != NULL; CURR = CURR->link) {

Demonstration of Traversal FRONT CURR NULL link DUMMY link 91 Bob link 94 Eve NULL for (CURR = FRONT; CURR != NULL; CURR = CURR->link) {  } // exit the loop

Insertion and Deletion On the Board

Announcements Project 3 is out – get started now! It is due Thursday, March 31st