Queue Implementations



Similar documents
Cpt S 223. School of EECS, WSU

CSI33 Data Structures

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

Algorithms and Data Structures

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

Data Structures and Algorithms

Unordered Linked Lists

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Analysis of a Search Algorithm

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Linked Lists Linked Lists, Queues, and Stacks

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

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

Common Data Structures

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

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

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

Comp151. Definitions & Declarations

Chapter Objectives. Chapter 7. Stacks. Various Types of Stacks LIFO. Empty Stack. Stacks

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.

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

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

Pointers and Linked Lists

DATA STRUCTURES USING C

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

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

An Incomplete C++ Primer. University of Wyoming MA 5310

Lecture 12 Doubly Linked Lists (with Recursion)

Abstract Data Types. Chapter 2

Sequences in the C++ STL

Linked Lists: Implementation Sequences in the C++ STL

Data Structures and Algorithms Lists

Appendix M: Introduction to Microsoft Visual C Express Edition

EP241 Computer Programming

Answers to Review Questions Chapter 7

C++ INTERVIEW QUESTIONS

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

For the next three questions, consider the class declaration: Member function implementations put inline to save space.

Data Structures, Practice Homework 2, with Solutions (not to be handed in)

CmpSci 187: Programming with Data Structures Spring 2015

Programming with Data Structures

! " # $ %& %' ( ) ) *%%+, -..*/ *%%+ - 0 ) 1 2 1

Lecture 2 Notes: Flow of Control

Module 2 Stacks and Queues: Abstract Data Types

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

10CS35: Data Structures Using C

Sample Questions Csci 1112 A. Bellaachia

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

STACKS,QUEUES, AND LINKED LISTS

MS Visual C++ Introduction. Quick Introduction. A1 Visual C++

Visual Studio 2008 Express Editions

Integrating the C++ Standard Template Library Into the Undergraduate Computer Science Curriculum

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

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

Lecture Notes on Binary Search Trees

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

Creating a Simple Visual C++ Program

Basics of I/O Streams and File I/O

CS107L Handout 04 Autumn 2007 October 19, 2007 Custom STL-Like Containers and Iterators

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

Note: Syntactically, a ; is needed at the end of a struct definition.

Outline. The Stack ADT Applications of Stacks Array-based implementation Growable array-based stack. Stacks 2

Chapter 5 Functions. Introducing Functions

Data Structures Using C++

Calling the Function. Two Function Declarations Here is a function declared as pass by value. Why use Pass By Reference?

Universidad Carlos III de Madrid

Node-Based Structures Linked Lists: Implementation

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

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

1 Abstract Data Types Information Hiding

DATA STRUCTURE - STACK


Binary storage of graphs and related data

Basics of C++ and object orientation in OpenFOAM

Bevezetés a C++ Standard Template Library-be

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

C++FA 5.1 PRACTICE MID-TERM EXAM

LINKED DATA STRUCTURES

recursion here it is in C++ power function cis15 advanced programming techniques, using c++ fall 2007 lecture # VI.1

Cours de C++ Utilisations des conteneurs

Data Structures and Algorithms

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Algorithms and Data Structures

Data Types. Abstract Data Types. ADTs as Design Tool. Abstract Data Types. Integer ADT. Principle of Abstraction

Passing 1D arrays to functions.

Algorithms and Data Structures Written Exam Proposed SOLUTION

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

Introduction to Data Structures and Algorithms

DATA STRUCTURE - QUEUE

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

Stack & Queue. Darshan Institute of Engineering & Technology. Explain Array in detail. Row major matrix No of Columns = m = u2 b2 + 1

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

Fondamenti di C++ - Cay Horstmann 1

Copyright 2001, Bill Trudell. Permission is granted to copy for the PLoP 2001 conference. All other rights reserved.

Transcription:

Queue Implementations as double 1 2 as double MCS 360 Lecture 17 Introduction to Data Structures Jan Verschelde, 1 October 2010

Queue Implementations as double 1 2 as double

a circular as double A can be linear or circular. Applications for circular s: waiting room with fixed #seats, buffer to process data. We first use an array to implement a 1 two indices, to front and back element, 2 update: (index + 1) modulo array size. modulo, indices only increase

as double private data members #ifndef MCS360_CIRCULAR_FIXED_BUFFER_H #define MCS360_CIRCULAR_FIXED_BUFFER_H namespace mcs360_circular_fixed_buffer class Queue private: T *data; size_t ; // of buffer int current; // index to front of int back; // index to end of size_t number; // number of elements

as double public methods public: Queue( int c ); // creates an empty void push( T item ); // pushes the item at the end bool empty(); // return true if is empty T front(); // returns the front element void pop(); // removes the front element ; #include "mcs360_circular_fixed_buffer.tc" #endif

as double constructor and push #include "mcs360_circular_fixed_buffer.h" #include <iostream> namespace mcs360_circular_fixed_buffer Queue<T>::Queue( int c ) = c; data = new T[c]; current = -1; back = -1; number = 0; void Queue<T>::push( T item ) this->back = (this->back + 1) % this->; this->data[this->back] = item; this->number = this->number + 1; if(this->current < 0) this->current = 0;

as double rest of methods bool Queue<T>::empty() return (this->number == 0); T Queue<T>::front() return data[this->current]; void Queue<T>::pop() this->current = (this->current + 1) % this->; this->number = this->number - 1;

as double testing the buffer #include <iostream> #include "mcs360_circular_fixed_buffer.h" using namespace mcs360_circular_fixed_buffer; using namespace std; int main() Queue<int> q(10); for(int i=1; i<6; i++) q.push(i); for(;!q.empty(); q.pop()) cout << q.front() << endl; for(int i=6; i<12; i++) q.push(i); for(;!q.empty(); q.pop()) cout << q.front() << endl; for(int i=12; i<18; i++) q.push(i); for(;!q.empty(); q.pop()) cout << q.front() << endl;

Queue Implementations as double 1 2 as double

improving... as double The implementation is simple and efficient: management of indices straightforward, efficient if size. All operations have cost O(1). Suggestions for improvement: throw exceptions for when pop empty or push to full ; enlarge when full.

Queue Implementations as double 1 2 as double

the Deque as double A deque is a double ended : we can pop from the front or the end, and push to front or to the end. Methods of STL deque, on deque<t> q: q.push_back(t): append to at end q.push_front(t): insert to at front t = q.back(): return last element of t = q.front(): return first element of q.pop_back(): remove last element of q.pop_front(): remove first element of

Queue Implementations as double 1 2 as double

implementing a deque as double We adapt our mcs360_double_list::list, using the Node definition of mcs360_double_node.h. The goal is to implement a deque: circular: we can circulate doubly linked: move forward & backward. If we allow to push and pop to the front and end of the, the is double ended, or a deque.

the data members as double #ifndef MCS360_CIRCULAR_DOUBLE_RING_H #define MCS360_CIRCULAR_DOUBLE_RING_H #define NULL 0 namespace mcs360_circular_double_ring class Queue private: #include "mcs360_double_node.h" Node *current; // pointer to current node int number; // number of elements

as double the Node definition From lecture 12: mcs360_double_node.h: #ifndef DNODE_H #define DNODE_H struct Node T data; // T is template parameter Node *next; // pointer to next node Node *prev; // pointer to previous node ; Node(const T& item, Node* next_ptr = NULL, Node* prev_ptr = NULL) : data(item),next(next_ptr),prev(prev_ptr) #endif

public methods as double public: Queue(); // returns an empty bool empty(); // true if empty, false otherwise int size(); // returns the size of the T front(); // returns the item at front of void move_front_forward(); // makes next element front of void move_front_backward(); // makes previous item front of

as double pushing and popping void push_front(t item); // inserts item in front of void push_back(t item); // appends item to the end void pop_front(); // removes the element at front void pop_back(); // removes the element at end ; #include "mcs360_circular_double_ring.tc" #endif Notice the grouping of the methods.

as double first group of methods namespace mcs360_circular_double_ring Queue<T>::Queue() current = NULL; number = 0; bool Queue<T>::empty() return (current == NULL); int Queue<T>::size() return number;

first group continued as double T Queue<T>::front() return current->data; void Queue<T>::move_front_forward() current = current->next; void Queue<T>::move_front_backward() current = current->prev;

appending to the Appending d to a that stores a, b, c : as double last current Node Node Node next: * prev: * data: a next: * prev: * data: b next: * prev: * data: c

appending to the Appending d to a that stores a, b, c : as double last current Node Node Node next: * prev: * data: a next: * prev: * data: b next: * prev: * data: c

appending to the Appending d to a that stores a, b, c : as double last current Node Node Node next: * prev: * data: a next: * prev: * data: b next: * prev: * data: c

as double appending to the void Queue<T>::push_back(T item) if(current == NULL) current = new Node(item); current->next = current; current->prev = current; else Node *last = current->prev; current->prev = new Node(item,current,current->prev); last->next = current->prev; number = number + 1;

as double removing the front void Queue<T>::pop_front() if(current!= NULL) if(current->prev == current->next) delete current; current = NULL; number = 0; else Node *last = current->prev; last->next = current->next; current->next->prev = last; delete current; current = last->next; number = number - 1;

as double Summary + Assignments More on Chapter 6 on implementations. Assignments: 1 Use the STL list in a templated class to implement a. Define an exception class Queue_Empty and illustrate how to operate your implementation with a test program. 2 Give code for push_front on our circular doubly linked list implementation for a deque. Make a drawing to illustrate the logic of the code. 3 Give code for pop_back on our circular doubly linked list implementation for a deque. Make a drawing to illustrate the logic of the code.