Lists and Sequences. 1/22/ :32 PM Sequences 1

Similar documents
Lists and Sequences. 1/18/2005 4:12 AM Sequences 1

Vectors. 9/14/2007 2:55 PM Vectors 1

Data Structures and Algorithms " Arrays and Linked Lists!

Elementary Data Structures. Stacks, Queues, & Lists Amortized analysis Trees

Iterators and Sequences

Sequences Outline and Required Reading: Sequences ( 5.3) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

CSED233: Data Structures (2018F) Lecture3: Arrays and Linked Lists

This lecture. Iterators ( 5.4) Maps. Maps. The Map ADT ( 8.1) Comparison to java.util.map

Data structure is an organization of information, usually in memory, for better algorithm efficiency.

CH 6 : VECTORS, LISTS AND SEQUENCES

Arrays & Linked Lists

Arrays. Array Definition

1/18/12. Chapter 5: Stacks, Queues and Deques. Stacks. Outline and Reading. Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University

CS2210 Data Structures and Algorithms

CH 6. VECTORS, LISTS, AND SEQUENCES

CH 6 : VECTORS, LISTS AND SEQUENCES

Queues COL 106. Slides by Amit Kumar, Shweta Agrawal

Lecture 3 Linear Data Structures: Arrays, Array Lists, Stacks, Queues and Linked Lists

Linked Lists. Singly Linked Lists (3.2) CSE 2011 Winter /01/2011

Chapter. Basic Data Structures. Contents. 2.1 StacksandQueues Lists Trees Exercises... 84

Lecture 3 Linear Data Structures

CH7. LIST AND ITERATOR ADTS

Algorithms and Data Structures

Lecture Notes CPSC 122 (Fall 2014) Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S.

January 24, Abstract Data Types (ADTs) An ADT is an abstraction of a data structure.

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

Data structure and algorithm in Python

Welcome to CSL 201 Data Structures

Dynamic Data Structures

Positional Sequences Section 4.2

Fall, 2015 Prof. Jungkeun Park

Lists. CITS2200 Data Structures and Algorithms. Topic 9

Linear Data Structures

CH7. LIST AND ITERATOR ADTS

HOWDY! WELCOME TO CSCE 221 DATA STRUCTURES AND ALGORITHMS

1 Deletion in singly linked lists (cont d) 1 Other Functions. 1 Doubly Linked Lists. 1 Circular lists. 1 Linked lists vs. arrays

Information Science 2

Ashish Gupta, Data JUET, Guna

CMSC 341 Lecture 7 Lists

Data Structure: Lists and Iterators. Instructor: Prof. Young-guk Ha Dept. of Computer Science & Engineering

Linked Lists. Chapter 4

ITI Introduction to Computing II

Linked Lists in C and C++

Lecture 3 Linear Data Structures

Lists. Outline. COMP9024: Data Structures and Algorithms. Lists. Array List ADT. Array-Based Implementation. Array lists Node lists Iterators

CMPT 225. Lecture 6 linked list

DS L9: Queues

CSCE 110 PROGRAMMING FUNDAMENTALS

Unit 4 Basic Collections

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

CSCA48 Term Test 1 Seminar

Data Structures. Alice E. Fischer. Lecture 4, Fall Alice E. Fischer Data Structures L4... 1/19 Lecture 4, Fall / 19

CS32 Discussion Week 3

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

1 P age DS & OOPS / UNIT II

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

Linked lists. Comp Sci 1575 Data Structures. Definitions. Memory structure. Implementation. Operations. Comparison

1/27/12. Vectors: Outline and Reading. Chapter 6: Vectors, Lists and Sequences. The Vector ADT. Applications of Vectors. Array based Vector: Insertion

Lists ADT and Iterators

8. Fundamental Data Structures

Data Structures and Algorithms. Chapter 5. Dynamic Data Structures and Abstract Data Types

DATA STRUCTURES AND ALGORITHMS

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Data Structures and Algorithms. Roberto Sebastiani

CSE 143. Complexity Analysis. Program Efficiency. Constant Time Statements. Big Oh notation. Analyzing Loops. Constant Time Statements (2) CSE 143 1

Lists, Stacks, and Queues. (Lists, Stacks, and Queues ) Data Structures and Programming Spring / 50

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011

DATA STRUCTURE UNIT I

Dynamic Memory Management. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Programming. Lists, Stacks, Queues

10/1/2018 Data Structure & Algorithm. Circularly Linked List Doubly Linked List Priority queues Heaps

Linked List. April 2, 2007 Programming and Data Structure 1

CSC 172 Data Structures and Algorithms. Lecture #9 Spring 2018

Queues. Lesson 4. CS 32: Data Structures Dept. of Computer Science

Linked Lists and Abstract Data Structures A brief comparison

Sets and Sequences vs. Linked Lists

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

Topics on the Midterm

[2:3] Linked Lists, Stacks, Queues

Introduction. Reference

Lecture 3 Linear Data Structures

CSC 172 Data Structures and Algorithms. Lecture #8 Spring 2018

Linked List Nodes (reminder)

Data Structures. Jia-Liang Lu. Slides based on the course notes of C.A. Shaffer

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

Top of the Stack. Stack ADT

Top of the Stack. Stack ADT

Programming II (CS300)

ADTs, Arrays, Linked Lists

CSCA48 Winter 2018 Week 3: Priority Queue, Linked Lists. Marzieh Ahmadzadeh, Nick Cheng University of Toronto Scarborough

Java Review: Objects

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Mon, Feb 22, 2016

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

CS350 - Exam 1 (100 Points)

Linked Lists

The java.util.list ADT

Transcription:

Lists and Sequences 1/22/2018 10:32 PM Sequences 1

Outline Singly linked list Position ADT and List ADT Doubly linked list Sequence ADT Implementations of the sequence ADT Iterators 1/22/2018 10:32 PM Sequences 2

Singly Linked List A singly linked list is a concrete data structure consisting of a sequence of nodes Each node stores element link to the next node elem next node A B C D 1/22/2018 10:32 PM Sequences 3

Stack with a Singly Linked List We can implement a stack with a singly linked list The top element is stored at the first node of the list The space used is O(n) and each operation of the Stack ADT takes O(1) time nodes t top bottom elements 1/22/2018 10:32 PM Sequences 4

Queue with a Singly Linked List We can implement a queue with a singly linked list The front element is stored at the first node The rear element is stored at the last node The space used is O(n) and each operation of the Queue ADT takes O(1) time r nodes f elements 1/22/2018 10:32 PM Sequences 5

Inserting at the Head 1. Allocate a new node 2. Insert new element 3. Have new node point to old head 4. Update head to point to new node Linked Lists 6

Removing at the Head 1. Update head to point to next node in the list 2. Allow garbage collector to reclaim the former first node Linked Lists 7

Inserting at the Tail 1. Allocate a new node 2. Insert new element 3. Have new node point to null 4. Have old last node point to new node 5. Update tail to point to new node Linked Lists 8

Removing at the Tail Removing at the tail of a singly linked list is not efficient! There is no constant-time way to update the tail to point to the previous node Linked Lists 9

Position ADT The Position ADT models the notion of place within a data structure where a single object is stored A special null position refers to no object. Positions provide a unified view of diverse ways of storing data, such as a cell of an array a node of a linked list Member functions: Object& element(): returns the element stored at this position bool isnull(): returns true if this is a null position 1/22/2018 10:32 PM Sequences 10

List ADT The List ADT models a sequence of positions storing arbitrary objects It establishes a before/after relation between positions Generic methods: size(), isempty() Query methods: isfirst(p), islast(p) Accessor methods: first(), last() before(p), after(p) Update methods: replaceelement(p, o), swapelements(p, q) insertbefore(p, o), insertafter(p, o), insertfirst(o), insertlast(o) remove(p) 1/22/2018 10:32 PM Sequences 11

Doubly Linked List A doubly linked list provides a natural implementation of the List ADT Nodes implement Position and store: element link to the previous node link to the next node Optional - special trailer and header nodes prev elem next node header nodes/positions trailer elements 1/22/2018 10:32 PM Sequences 12

Doubly Linked List header Very important interjection: Correctly handling double (and single) linked lists is very tricky do not underestimate it! Hint: dangling pointers nodes/positions trailer elements 1/22/2018 10:32 PM Sequences 13

1/22/2018 10:32 PM Sequences 14

Insertion We visualize operation insertafter(p, X), which returns position q p A B C p A B q C p X q A B X C 1/22/2018 10:32 PM Sequences 15

Deletion We visualize remove(p), where p = last() p A B C D A B C p D A B C 1/22/2018 10:32 PM Sequences 16

Performance In the implementation of the List ADT by means of a doubly linked list The space used by a list with n elements is O(n) The space used by each position of the list is O(1) All the operations of the List ADT run in O(1) time Operation element() of the Position ADT runs in O(1) time 1/22/2018 10:32 PM Sequences 17

Sequence ADT The Sequence ADT is the union of the Vector and List ADTs Elements accessed by Rank, or Position Generic methods: size(), isempty() Vector-based methods: elematrank(r), replaceatrank(r, o), insertatrank(r, o), removeatrank(r) List-based methods: first(), last(), before(p), after(p), replaceelement(p, o), swapelements(p, q), insertbefore(p, o), insertafter(p, o), insertfirst(o), insertlast(o), remove(p) Bridge methods: atrank(r), rankof(p) 1/22/2018 10:32 PM Sequences 18

Applications of Sequences The Sequence ADT is a basic, generalpurpose, data structure for storing an ordered collection of elements Direct applications: Generic replacement for stack, queue, vector, or list small database (e.g., address book) Indirect applications: Building block of more complex data structures 1/22/2018 10:32 PM Sequences 19

Array-based Implementation We use a circular array storing positions A position object stores: Element Rank Indices f and l keep track of first and last positions S 0 1 2 3 f l elements positions 1/22/2018 10:32 PM Sequences 20

Sequence Implementations Operation Array List size, isempty 1 1 atrank, rankof, elematrank 1 n first, last, before, after 1 1 replaceelement, swapelements 1 1 replaceatrank 1 n insertatrank, removeatrank n n insertfirst, insertlast 1 1 insertafter, insertbefore n 1 remove n 1 1/22/2018 10:32 PM Sequences 21

Iterators An iterator abstracts the process of scanning through a collection of elements Methods of the ObjectIterator ADT: boolean hasnext() object next() reset() Extends the concept of position by adding a traversal capability May be implemented with an array or singly linked list An iterator is typically associated with an another data structure We can augment the Stack, Queue, Vector, List and Sequence ADTs with method: ObjectIterator elements() Two notions of iterator: snapshot: freezes the contents of the data structure at a given time dynamic: follows changes to the data structure 1/22/2018 10:32 PM Sequences 22

Comparison of Position, List, and Sequence ADTs Position ADT Keeps track of the position of an element within the data structure E.g., element at X, array-like indexing List ADT Models a sequence of positions storing elements E.g., first, last, isbefore, linked list Sequence ADT Contains both position and list data E.g., vector+linked-list 1/22/2018 10:32 PM Sequences 23

Linked List Operations 4 nodes in all cases 1. Swap in singly linked list Swap node 2 and 3 2. Swap in double linked list Swap node 2 and 3 3. Swap 1 st and last in singly linked list 4. Swap 1 st and last in doubly linked list 1/22/2018 10:32 PM Sequences 24

Linked List Operations Swap You have the pointer H Node fields are data and next How do you most efficiently swap B and C using only one auxiliary variable space? H A B C D 1/22/2018 10:32 PM Sequences 25

Linked List Operations Swap B H->next H->next B->next B->next B->next->next H->next->next B H A B C D 1/22/2018 10:32 PM Sequences 26

Linked List Operations Swap B H->next H->next B->next B->next B->next->next H->next->next B H A C B D 1/22/2018 10:32 PM Sequences 27

Linked List Operations Swap(Node *curr, Node *prev) prev->next curr->next curr->next curr->next->next prev->next->next curr What if curr = head? H A C B D 1/22/2018 10:32 PM Sequences 28

Doubly Linked List Operations Swap How do you most efficiently swap C and D using only one auxiliary variable space? H T A B C D 1/22/2018 10:32 PM Sequences 29

Doubly Linked List Operations Swap(Node *curr, Node *next) curr->prev->next next curr->next next->next next->next curr next->prev curr->prev curr->next->prev curr curr->prev next H T A B C D 1/22/2018 10:32 PM Sequences 30

Linked List Operations Reverse How do I efficiently reverse the order of the linked list? H A B C D 1/22/2018 10:32 PM Sequences 31

Linked List Operations Reverse(Node *curr, Node *prev) if (curr->next = NULL) { curr->next prev; newhead curr; } else { newhead Reverse(curr->next, curr); curr->next prev; } return (newhead); H A B C D 1/22/2018 10:32 PM Sequences 32

Doubly Linked List Operations Reverse How do I efficiently reverse the order of a doubly linked list? H T 1/22/2018 10:32 PM Sequences 33

Doubly Linked List Operations Can use the previous Reverse() method or if the semantics of next/prev are flexible then Reverse(Node *head, Node *tail) tmp head head tail tail tmp H T 1/22/2018 10:32 PM Sequences 34

Doubly Linked List Operations Can use the previous Reverse() method or if the semantics of next/prev are flexible then Reverse(Node *head, Node *tail) tmp head head tail tail tmp T H 1/22/2018 10:32 PM Sequences 35