Sorting, Stacks, Queues Bryce Boe 2013/11/06 CS24, Fall 2013.

Slides:



Advertisements
Similar presentations
Stacks and Queues. Not really data structures – More of an enforcement of policy – Can be implemented using an array or linked list – Can store just about.
Advertisements

Stacks, Queues, and Linked Lists
Stack & Queues COP 3502.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
More on Stacks and Queues. As we mentioned before, two common introductory Abstract Data Type (ADT) that worth studying are Stack and Queue Many problems.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
What is a Queue? A queue is a FIFO “first in, first out” structure.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Data Structure Dr. Mohamed Khafagy.
Queue Overview Queue ADT Basic operations of queue
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort.
1 Queues CPS212 Gordon College. 2 Introduction to Queues A queue is a waiting line – seen in daily life –Real world examples – toll booths, bank, food.
Queues.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
TCSS 342, Winter 2005 Lecture Notes
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 8 Ming Li Department of.
Priority Queues and Heaps Bryce Boe 2013/11/20 CS24, Fall 2013.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
CS 1031 Queues Definition of a Queue Examples of Queues Design of a Queue Class Different Implementations of the Queue Class.
Stacks, Queues Bryce Boe 2013/07/30 CS24, Summer 2013 C.
Arrays, Lists, Stacks, Queues Static and Dynamic Implementation Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Stacks and Queues Introduction to Computing Science and Programming I.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
Week 3 - Friday.  What did we talk about last time?  Stacks  Array implementation of a stack.
Chapter Objectives  Learn how to represent a waiting line (queue)  Become proficient using the methods in the Queue  Understand how to implement the.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
The Abstract Data Type Queue A queue New items enter at the back, or rear, of the queue Items leave from the front of the queue First-in, first-out (FIFO)
UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –
Queues Chapter 5 Queue Definition A queue is an ordered collection of data items such that: –Items can be removed only at one end (the front of the queue)
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
April 27, 2017 COSC Data Structures I Review & Final Exam
Linear Data Structures
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Lecture 10 b Stacks b Queues. 2 Stacks b A stack ADT is linear b Items are added and removed from only one end of a stack b It is therefore LIFO: Last-In,
Queues CS 367 – Introduction to Data Structures. Queue A queue is a data structure that stores data in such a way that the last piece of data stored,
Implementing Queues Eric Roberts CS 106B February 13, 2013.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Review Array Array Elements Accessing array elements
Understanding Algorithms and Data Structures
COSC160: Data Structures: Lists and Queues
Queues Queues Queues.
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
Queues 11/16/2018 4:18 AM Queues 11/16/2018 4:18 AM Queues.
Lesson Objectives Aims
CSC 143 Queues [Chapter 7].
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Queues Jyh-Shing Roger Jang (張智星)
Lecture 2: Stacks and Queues
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
Queues Definition of a Queue Examples of Queues
Data Structures & Programming
Presentation transcript:

Sorting, Stacks, Queues Bryce Boe 2013/11/06 CS24, Fall 2013

Outline Finish Lab 5 Part 2 Review Lab 6 Review / Sorting / C++ Templates Stacks and Queues

O(n 2 ) Sorting Algorithms Bubble sort – Bubble the largest element to the end in each pass Insertion sort – Insert the next element into the sorted portion of the list Selection sort – Find the smallest (or largest) item and put it in its proper location

Templates Before templates: – Need a version of the data structure for each type it contains OR store (void*) pointers in the structure With templates: – Declare functions / classes that can operate on arbitrary types

STACKS

Stack A First-in Last-out data structure (FILO) – Operates like a stack of papers Operations – void push(T item) Add an item to the stack – T pop() Remove and return the most recently added item from the stack

Linked-List Implementation push(item) – Use insert_at(0, item) for a O(1) pop(item) – Use remove_at(0) for a O(1)

Array-based implementation push(item) – Use insert_at(-1, item) for an O(1) insertion – O(n) when the array must expand pop() – Use remove_at(-1) for an O(1) removal

QUEUES

Queues What is a queue? – A data structure that allows access to items in a first in, first out manor (FIFO) What are its operations? – enqueue (add to the queue) – dequeue (remove the oldest item in the queue) What are some example queues? – Waiting in line, task scheduling, data buffering

Linked List Implementation Stacks add and remove from the same side, thus for queues it makes sense to add and remove from opposite sides BUT, adding and removing from the end of the list is O(n)

Make the linked list smarter Add a tail pointer – Gives us immediate access to the end of the list – Can we improve these functions’ efficiency? insert_at(-1, item)? remove_at(-1)? YES NO

Linked-List Implementation enqueue(item) – Use insert_at(-1, item) for a O(1) Assumes we have a working tail pointer in the list dequeue(item) – Use remove_at(0) for a O(1)

Array-based implementation To implement an unbounded queue on top of the array-based implementation of a list requires treating the array as circular Rather than using 0 as a base index, the queue needs to keep track of which index should be the base, and use modular arithmetic to wrap around When the array needs to grow, the values must be manually “reset” such that the base index is at the zero position

Array-based implementation enqueue(item) – Use insert_at((BASE + size) % allocated, item) for an O(1) operation dequeue(item) – Use remove_at(BASE) for an O(1) operation and make sure to increment the BASE

Problems we can now solve Write a program to determine if a given text is a palindrome: – racecar, stats – poordanisinadroop Take a few minutes to solve it with your neighbor

Palindrome Solution bool is_palindrome(char *word) { Queue queue; Stack stack; int index = 0; //iterate through the word adding to the queue while(word[index] != ‘\0’) { stack.push(word[index]); queue.enqueue(word[index++]); } while(!queue.is_empty()) if (stack.pop() != queue.dequeue() return false; return true; }