Data Structures and Algorithms



Similar documents
Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

Chapter 3: Restricted Structures Page 1

Common Data Structures

Data Structures Using C++ 2E. Chapter 5 Linked Lists

QUEUES. Primitive Queue operations. enqueue (q, x): inserts item x at the rear of the queue q

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

Data Structures and Algorithms Stacks and Queues

Linked Lists Linked Lists, Queues, and Stacks

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

STACKS,QUEUES, AND LINKED LISTS

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Cpt S 223. School of EECS, WSU

Queues and Stacks. Atul Prakash Downey: Chapter 15 and 16

Application of Stacks: Postfix Expressions Calculator (cont d.)

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

Analysis of a Search Algorithm

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

Data Structures and Algorithm Analysis (CSC317) Intro/Review of Data Structures Focus on dynamic sets

Unordered Linked Lists

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Last not not Last Last Next! Next! Line Line Forms Forms Here Here Last In, First Out Last In, First Out not Last Next! Call stack: Worst line ever!

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

What is a Stack? Stacks and Queues. Stack Abstract Data Type. Java Interface for Stack ADT. Array-based Implementation

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Module 2 Stacks and Queues: Abstract Data Types

DATA STRUCTURES USING C

10CS35: Data Structures Using C

Introduction to Data Structures

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Cpt S 223. School of EECS, WSU

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Algorithms and Data Structures

Universidad Carlos III de Madrid

Course: Programming II - Abstract Data Types. The ADT Queue. (Bobby, Joe, Sue, Ellen) Add(Ellen) Delete( ) The ADT Queues Slide Number 1

PROGRAMMING CONCEPTS AND EMBEDDED PROGRAMMING IN C, C++ and JAVA: Lesson-4: Data Structures: Stacks

DATA STRUCTURE - STACK

Queue Implementations

DATA STRUCTURE - QUEUE

Sequences in the C++ STL

Introduction to Data Structures and Algorithms

St S a t ck a ck nd Qu Q eue 1

CmpSci 187: Programming with Data Structures Spring 2015

Computer Architecture

Data Structures and Data Manipulation

Linked Lists: Implementation Sequences in the C++ STL

Abstract Data Types. Chapter 2

PES Institute of Technology-BSC QUESTION BANK

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

1.00 Lecture 35. Data Structures: Introduction Stacks, Queues. Reading for next time: Big Java: Data Structures

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

Conditions for Strong Synchronization In Concurrent Data Types

C++ Programming Language

Linear ADTs. Restricted Lists. Stacks, Queues. ES 103: Data Structures and Algorithms 2012 Instructor Dr Atul Gupta

Advanced Computer Architecture-CS501. Computer Systems Design and Architecture 2.1, 2.2, 3.2

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Chapter 8: Bags and Sets

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

Glossary of Object Oriented Terms

Data Structures Using C++ 2E. Chapter 5 Linked Lists

OPERATING SYSTEM SERVICES

EECE 276 Embedded Systems

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department

The Design of the Inferno Virtual Machine. Introduction

Big O and Limits Abstract Data Types Data Structure Grand Tour.

Lecture 22: C Programming 4 Embedded Systems

First Bytes Programming Lab 2

Node-Based Structures Linked Lists: Implementation

JAWAA: Easy Web-Based Animation from CS 0 to Advanced CS Courses

Using these objects to view the process of the whole event from triggering waiting for processing until alarm stops. Define event content first.

EARTH PEOPLE TECHNOLOGY SERIAL GRAPH TOOL FOR THE ARDUINO UNO USER MANUAL

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

INTRODUCTION The collection of data that makes up a computerized database must be stored physically on some computer storage medium.

Algorithms and Data S tructures Structures Stack, Queues, and Applications Applications Ulf Leser

Programming with Data Structures

ò Paper reading assigned for next Thursday ò Lab 2 due next Friday ò What is cooperative multitasking? ò What is preemptive multitasking?

ASSEMBLY PROGRAMMING ON A VIRTUAL COMPUTER

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Record Storage and Primary File Organization

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Data Structures and Algorithms V Otávio Braga

Data Structures. Level 6 C Module Descriptor

Lecture 2: Data Structures Steven Skiena. skiena

Sequential Data Structures

\Mankinds's progress is measured by the number of. Elementary data structures such as stacks, queues,

COMPUTER SCIENCE. Paper 1 (THEORY)

Copyright 2007 Ramez Elmasri and Shamkant B. Navathe. Slide 13-1

Linked List as an ADT (cont d.)

Event Log. Chapter 7 Event Log Event Log Management Excel Editing Create a New Event Log... 9

Data Structures UNIT III. Model Question Answer

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

Transcription:

Data Structures and Algorithms Lecture 4 2016 Stacks and... 1/28

1 2 Circular Linked 3 Queue Syntax and Functions in C++ 4 Stacks and... 2/28

FIFO and LIFO Outline Data structures can be specialized versions of the general array, growing array, or linked list. A Stack is a Last-In-First-Out data structure: all insertions and deletions happen at one end of the array or list. A Queue is a First-In-First-Out data structure with both a front and a back. Data is added at the back of the queue and removed at the front. A Dequeue is a more general data structure with both a front and a back. Data can be either added or removed at both the front and the back of the dequeue. Stacks and queues have played an important role in the development of computer science. Stacks and... 3/28

Stack and Queue Implementations Implementation can be an array, a growing array, or a linked list. Each implementation has its own and advantages / disadvantages. Stack Queue Dequeue Array simple, fast, fast, not limited size. limited size. appropriate. Growing fast, fast, used by STL Array not complex, complex to program tricky to program unlimited size unlimited size unlimited size Linked easy not di cult not List slower runtime slower runtime appropriate. unlimited size unlimited size The nature of the application you are implementing may determine which implementation you should use. Stacks and... 4/28

Traditional Stack and Queue Functions There is a long tradition behind the names of the functions used with stacks and queues. Stack empty full top push pop Queue empty full front back enqueue dequeue C++ provides template class implementations that honor some of these names, change some, add alternative names and provide additional functions. Stacks and... 5/28

Circular Linked Circular Linked STL and their functions Stacks and... 6/28

are Familiar Circular Linked The first item inserted in a queue is the first to be processed, and every item is processed in order. We queue up for service in banks, grocery stores, airports, etc. FIFO order is consistent with our idea of fairness. Stacks and... 7/28

in Computing Circular Linked are central in computer simulations. Events that happen in the simulation are modeled by objects placed in a queue. When they get to the front of the queue, the computer executes the imaginary event. are used in GUI interfaces to manage events. Within a running computer, there are multiple processes and threads that all need to share one (or a few) processing units. The operating system handles these processes by putting them in queues, waiting for a turn at executing on a CPU. Every I/O request ends one turn. When the I/O is finished, the process goes back into the queue, waiting for another turn. Stacks and... 8/28

ACircularQueue-Empty Circular Linked Aqueueimplementedinanarrayiscalledacircular queue because the part of the array that is filled with date moves toward the end, then circles back to the beginning of the array. Here, we push 4 items onto the queue. Queue q; max count front back 8 0 0 0 data???????? [0] [1] [2] [3] [4] [5] [6] [7] Stacks and... 9/28

ACircularQueue-1 Circular Linked Here, we push 4 items onto the queue. q.push('a'); q.push('z'); q.push('m'); q.push('p'); max 8 count 4 front back 0 4 data a z m p???? [0] [1] [2] [3] [4] [5] [6] [7] Stacks and... 10/28

ACircularQueue-2 Circular Linked Now we pop two and push two. q.pop(); q.push('b'); q.push('c'); q.pop(); max 8 count 4 front 2 back 6 data?? m p b c?? [0] [1] [2] [3] [4] [5] [6] [7] Stacks and... 11/28

ACircularQueue-3 Circular Linked Here we see the back position wrap around. q.push('d'); q.push('e'); q.push('f'); max 8 count 7 front 2 back 1 data f? m p b c d e [0] [1] [2] [3] [4] [5] [6] [7] Stacks and... 12/28

ACircularQueue-4 Circular Linked Here we see the front position wrap around. q.pop(); q.pop(); q.pop(); q.pop(); q.pop(); q.push('g'); q.push('h'); q.pop(); q.pop(); max 8 count 2 front 1 back 3 data? g h? [0] [1] [2] [3]???? [4] [5] [6] [7] Stacks and... 13/28

ALinkedQueue Empty Circular Linked An empty linked queue has a dummy header cell. This eliminates special cases and makes programming easier. The dummy cell gives a place to anchor front and back pointers in an empty list. Queue q; front count back 0 ~ nullptr Stacks and... 14/28

ALinkedQueue-1 Outline Circular Linked A linked queue after four pushes. q.push('a'); q.push('z'); q.push('m'); q.push('p'); ~ front count back 4 a z m p nullptr Stacks and... 15/28

Pop One and Push One. Circular Linked q.pop(); pop() must free this cell: q.push('b'); a nullptr ~ front count back 4 z m p b nullptr Stacks and... 16/28

ALinkedQueue-3 Outline Circular Linked The queue grows and shrinks easily by relinking cells. q.push('c'); q.pop(); q.pop(); q.pop(); front count back 2 ~ b c nullptr Stacks and... 17/28

Queue Syntax and Functions in C++ STL Classes Overview of STL STL Queue Implementations Stacks and... 18/28

Stack and Queue Implementations Queue Syntax and Functions in C++ When using a data structure, there are two approaches: Build your own. Use one of the template classes (STL) provided by C++. The second choice is normally better. STL containers are debugged, ready to use, and very adaptable. However, because this is a data structures class, I will sometimes ask you to build your own. In this way, you will better understand how data structures work. Stacks and... 19/28

Stack and Queue Functions in C++ Queue Syntax and Functions in C++ C++ template classes exist for many data structures, including Stack and Queue. These functions are defined: Stack Queue (constructor) (constructor) empty empty size size top front back push push emplace emplace pop pop swap swap Stacks and... 20/28

queue<bt> Outline Queue Syntax and Functions in C++ queue is an adapter class; it is an interface wrapped around another STL class. The underlying class can be a vector<bt>, a list<bt> or a deque<bt>. A deque (double-ended queue) is the default. (First form of the constructor, below). To make a queue with a di erent underlying container, use the second form of the constructor. queue<int> qu1; // Empty queue using deque queue<int, list<int>> qu2; // Empty queue using list Stacks and... 21/28

queue<bt> Functions Queue Syntax and Functions in C++ Examples of use are given here. Prototypes are at cplusplus.com while (!qu2.empty())... // Result is type bool unsigned n = qu2.size(); // # of elements in container BT temp = qu2.front( ); // Refers to oldest item in qu2 BT temp = qu2.back( ); // Refers to newest item in qu2 qu2.push( item ); // Insert item after back qu2.emplace( BT( ) ); // Construct and push an item qu2.pop(); // Remove item at front. Return void swap( qu1, qu2); // Swap two entire queues Stacks and... 22/28

Program 4: Bucket Sort. Due March 1 A bucket sort 1 is the fastest sort for a vary large number of data items 2. It is an O(n) sort that works by using groups of bits from the sort keys rather than by comparing key values to each other. The bucket sort described here is applicable to any large data set, but the presentation is specific to the one you will be using. In this assignment, you will implement an array of queues. You will build your own queue class, not use the C++ STL queue. 1 This technique has been in use since the days of punched cards. 2 Google s map-reduce is based on a bucket sort. Stacks and... 23/28

The Data to Sort Outline We will be sorting objects with two data fields: (1) the time (type time t), a 4-byte unsigned integer. (2) the temperature, a 4-byte float. The data file will consist of a very large number of time/temperature measurements, recorded in January at an apartment in Tuckahoe, New York, arranged by time. There will be three files, one from each of three sensors on di erent sides of the building. You only need to process one of the files, but di erent people should choose di erent files. Handle file opening and EOF properly. First, all of the data must be read from a file, and each data item installed in a new cell. The cells must each be linked onto the end of a queue called the master queue. Stacks and... 24/28

Sorting by Temperature. Data cells will be distributed into buckets. An array of 256 buckets must be created. Each bucket is a linked queue, initially empty. When the data and buckets are ready, sorting begins. It will work in four passes (numbered 0, 1, 2, and 3), where each pass consists of a distribute operation (next slide) and a collect operation (slide 26). After distributing all the data into the buckets. the queues from the buckets must be collected and joined back into a single master queue. At the end of four passes, the data will again be in the master queue, and will be sorted. Output the sorted data to a file. Stacks and... 25/28

Distributing Data into Buckets Distribute each cell into a bucket as follows: Remove it from the front of the master queue. Access one byte of the temperature and use the result as a subscript to select a bucket. Isolate that byte using shifting and masking (next slide). In pass k, use the kth byte from the right. Attach the cell to the end of the queue in that bucket. Stacks and... 26/28

Isolating one byte. Outline Define mask = 0xff; to isolate one byte (8 bits). Start with the temperature value from the cell you are distributing. Compute subscript = mask & (temp >> n) where n is 8 k, andk is the pass number. The computed subscript is the number of the bucket to use for this data item. Detach the cell from the master queue and push it onto the selected bucket queue. In this process, the actual sort key must not be changed. Stacks and... 27/28

Collecting the Data into a Master List At the end of each distribution pass, your master queue is empty. Now collect all the cells, as follows: Find the first non-empty bucket, (k), in the bucket array. Start at bucket k and process buckets, in order, through number 255. If the bucket is not empty, Attach the last cell of the master queue to the first non-dummy cell in the bucket. Set the back pointer of the master list equal to the back pointer of the bucket. All the cells have now been removed from the bucket, so reset it to empty. Stacks and... 28/28