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

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

Dynamic Data Structures

1 P age DS & OOPS / UNIT II

Abstract Data Types 1

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

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

Abstract Data Types. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Abstract Data Types 1

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

811312A Data Structures and Algorithms, , Exercise 1 Solutions

STACKS AND QUEUES. Problem Solving with Computers-II

Lecture 7: Data Structures. EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

csci 210: Data Structures Stacks and Queues

Name CPTR246 Spring '17 (100 total points) Exam 3

List, Stack, and Queues

1 Short Answer (7 Points Each)

.:: UNIT 4 ::. STACK AND QUEUE

Abstract vs concrete data structures HEAPS AND PRIORITY QUEUES. Abstract vs concrete data structures. Concrete Data Types. Concrete data structures

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

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

Chapter 9 STACK, QUEUE

DATA STRUCTURE UNIT I

Programming. Lists, Stacks, Queues


CS24 Week 4 Lecture 2

Linked Lists in C and C++

LECTURE 11 TREE TRAVERSALS

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

Containers: Queue and List. Jordi Cortadella and Jordi Petit Department of Computer Science

Course Review. Cpt S 223 Fall 2009

Solution for Data Structure

Data Abstractions. National Chiao Tung University Chun-Jen Tsai 05/23/2012

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures

Chapter 18: Stacks And Queues

cs Java: lecture #6

CMSC 341. Linked Lists, Stacks and Queues. CMSC 341 Lists, Stacks &Queues 1

Algorithms, Data Structures, and Problem Solving

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

Ashish Gupta, Data JUET, Guna

Basic Data Structures (Version 7) Name:

Trees. Carlos Moreno uwaterloo.ca EIT

Chapter 18: Stacks And Queues. Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Stacks, Queues and Hierarchical Collections

Queues. A queue is a special type of structure that can be used to maintain data in an organized way.

Course Review for Finals. Cpt S 223 Fall 2008

Data Structure Advanced

Chapter 18: Stacks And Queues

HOWDY! WELCOME TO CSCE 221 DATA STRUCTURES AND ALGORITHMS

Name: 1 stack<int> s; 2 s.push(9) 3 s.push(5) 4 s.push(10) 5 s.push(1) 6 s.pop() 7 s.pop() 8 s.push(0) 9 s.pop() 10 s.pop() 11 s.

Sequential Containers Cont'd

Information Science 2

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

3. Fundamental Data Structures

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

CS 216 Exam 1 Fall SOLUTION

Lecture 10 Notes Linked Lists

CS350 - Exam 1 (100 Points)

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

Sequential Containers Cont'd

CH 6 : VECTORS, LISTS AND SEQUENCES

CSE 100: GRAPH ALGORITHMS

12 Abstract Data Types

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Unit 4 Basic Collections

UNIVERSITY OF MASSACHUSETTS LOWELL Department of Electrical and Computer Engineering EXAM November 2016

INSTITUTE OF AERONAUTICAL ENGINEERING

Binary Trees. Directed, Rooted Tree. Terminology. Trees. Binary Trees. Possible Implementation 4/18/2013

Containers: Stack. The Stack ADT. The Stack ADT. The Stack ADT

Containers: Stack. Jordi Cortadella and Jordi Petit Department of Computer Science

CH 6 : VECTORS, LISTS AND SEQUENCES

CMSC 341 Lecture 6 STL, Stacks, & Queues. Based on slides by Lupoli, Dixon & Gibson at UMBC

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

CP2 Revision. theme: dynamic datatypes & data structures

PA3 Design Specification

Object Oriented Programming COP3330 / CGS5409

Come to the PASS workshop with your mock exam complete. During the workshop you can work with other students to review your work.

Agenda. Inner classes and implementation of ArrayList Nested classes and inner classes The AbstractCollection class Implementation of ArrayList!

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

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

CMSC Introduction to Algorithms Spring 2012 Lecture 7

Chapter 17: Linked Lists

5 Phases of Software Life Cycle

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 9. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University

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

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

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

DATA STRUCUTRES. A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

Data Structures Question Bank Multiple Choice

16. Dynamic Data Structures

Stack. Data structure with Last-In First-Out (LIFO) behavior. Out

Lecture Notes on Queues

CPSC 260 Data Structures and Algorithms for Computer Engineers Linked Lists!

Final Exam Data Structure course. No. of Branches (5)

CS32 Discussion Week 3

Stack and Queue. Stack:

THE UNIVERSITY OF WESTERN AUSTRALIA

Data Structure. Recitation VII

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

Transcription:

Linear Structure Linear Structures Chapter 4 CPTR 318 Every non-empty linear structure has A unique element called first A unique element called last Every element except last has a unique successor Every element except first has a unique predecessor 1 2 Implementations Array Contiguous memory Space needed only for data Insertions and deletions may require data movement Linked list Non-contiguous memory Data and links both require space Insertions and deletions require no data movement Array details For an array A of size n, where n > 0: First element: A[0] Last element: A[n 1] Predecessor of A[i], for all i > 0: A[i 1] Successor of A[i], for all i < n 1: A[i + 1] n A 0 i 1 i i + 1 n 1 List details struct Node T data; Node *prev, *next; class List Node<T> *head, *tail; // Methods omitted For a list L, where L.head is not null, and p is a Node<T> pointer to an element in the list: First element: L.head Last element: L.tail Predecessor of p, for all p L.head: p->prev Successor of p, for all p L.tail: p->next Searching Ordered Search O(log n) Insertion O(1) Deletion O(1) Random Access O(1) 1

Searching Ordered Search O(log n) Insertion O(1) Deletion O(1) Random Access O(1) Searching Ordered Search O(log n) Insertion O(1) Deletion O(1) Random Access O(1) Searching Ordered Search O(log n) Insertion O(1) Deletion O(1) Random Access O(1) Searching Ordered Search O(log n) Insertion O(1) Deletion O(1) Random Access O(1) Searching Ordered Search O(log n) Insertion O(1) Deletion O(1) Random Access O(1) Searching Ordered Search O(log n) Insertion O(1) Deletion O(1) Random Access O(1) 2

Specialized Linear Structures Stack LIFO : push, pop, top Queue FIFO : enqueue, dequeue, first Deque Access at both ends : insert_front, insert_back, delete_front, delete_back, front, back Stack LIFO push pop top Applications Depth-first search Recursive evaluation Queue FIFO enqueue dequeue front Applications Breadth-first search Scheduling Simulations Circular Queue Page 131 Encapsulation Good! Data structure encapsulation is also known as data hiding Client need not worry about the internal details Client is concerned with what, not how The author of the encapsulating class is free to change the internals of the encapsulated class without breaking client code Wonderful for software maintenance issues: fixes, optimizations, etc. Encapsulation Bad! The client is insulated from accessing the individual elements of the data structure The client is at the mercy of the access operations provided by the encapsulating class What if I want to print only the items in a list of integers that are even? 17 18 3

Encapsulation Dilemma Iterator How can we provide access to individual elements of an encapsulated data set without exposing the implementation of the data structure? This problem has been encountered many times in the past Fortunately a standard solution has been devised An iterator is an object that provides access to individual elements of a collection in a standard way without exposing the implementation details of that collection The collection can be real or virtual Traversing a list vs. generating a random number 19 20 Design Patterns Iterator is one example of a design pattern A design pattern in software is a design that solves a programming problem The design has application beyond the particular problem at hand Concept originally proposed by Christopher Alexander for architectural design GoF book The iterator concept is one example of a design pattern Structure of a Design Pattern Name Context for its use Problem description Solution 22 23 Iterator Design Pattern Name: Iterator Context for its use: An encapsulated collection of data elements Problem description: Need to provide client-controlled access to individual elements of the collection without exposing the underlying data structure of the collection holding those elements Solution: Provide a separate object that has data structure-specific knowledge of the collection, but that presents to the client generic operations that access elements in an implementation-independent manner The Standard Template Library. The STL provides a collection of data structures and provides some generic algorithms, such as sorting. As its name suggests, the STL makes heavy use of templates. All compiler that implement the standard has the library available. 24 4

Basic Data Structures Data Structure is a representation of data and the operations allowed on that data. Basic Data Structures List Stack Queue Sets Maps Priority Queues STL basic components Containers Iterators Algorithms STL Containers STL Iterators A collection of objects (elements) Must support: bool empty() iterator begin() iterator end() int size() Iterators maintain a notion of a current position in the container and provides basic operations such as the ability to advance to the next position and access the item in the current position Must support: itr++ itr-- (optional) *itr ==!= Each container could define several iterators const_iterator must be used to traverse const containers C++11 Smart Pointers // Implements individual nodes within a singly-linked list struct ListNode // The value of interest in a list element T data; // A link to the next element in the list; null, if this node is the last element shared_ptr<listnode<t>> next; ListNode<T>(const T& elem): data(elem), next(nullptr) } ListNode<T>(const T& elem, shared_ptr<listnode<t>> n): data(elem), next(n) } Singly-linked List // Implements singly-linked list objects. class LinkedList // Points to the first element of this list; null if the list is empty shared_ptr<listnode<t>> head; // Points to the last element of this list; null if the list is empty shared_ptr<listnode<t>> tail; public: // Creates a new, empty list object LinkedList<T>(): head(nullptr), tail(nullptr) } // Inserts new element elem onto the back of this list object void insert(const T& elem) } 5

Reference Counting Insert 12 Insert 4 Insert 22 Insert 7 6

7

Doubly-linked Lists 2 2 2 2 Doubly-linked Lists 2 2 2 2 1 2 2 1 8

Doubly-linked Lists 1 2 2 1 Now what? Weak Pointers (weak_ptr<t>) // Implements individual nodes within a singly-linked list struct ListNode // The value of interest in a list element T data; // A link to the next element in the list; null, if this node is the last element shared_ptr<listnode<t>> next; // A link to the previous element in the list; null, if this node is the first element weak_ptr<listnode<t>> prev; ListNode<T>(const T& elem): data(elem), next(nullptr) } ListNode<T>(const T& elem, shared_ptr<listnode<t>> n): data(elem), next(n) } Weak Pointers (weak_ptr<t>) // Implements individual nodes within a singly-linked list struct ListNode // The value of interest in a list element T data; // A link to the next element in the list; null, if this node is the last element shared_ptr<listnode<t>> next; // A link to the previous element in the list; null, if this node is the first element weak_ptr<listnode<t>> prev; ListNode<T>(const T& elem): data(elem), next(nullptr) } Weak pointers 1 1 1 2 ListNode<T>(const T& elem, shared_ptr<listnode<t>> n): data(elem), next(n) } Weak pointers Weak pointers shared_ptr<listnode<t>> 1 1 1 2 weak_ptr<listnode<t>> 1 1 1 2 Create a strong pointer from a weak pointer using lock 9

Weak pointers Weak pointers 1 1 2 2 p Create a strong pointer from a weak pointer using lock: shared_ptr<listnode<t>> p = tail->prev.lock() 1 2 1 2 p Create a strong pointer from a weak pointer using lock: p = p->prev.lock() 1 1 1 2 1 1 1 2 0 1 1 1 1 1 1 10

0 1 1 1 1 0 1 1 0 11