Jerry Lebowitz.  Stacks  Queues 3 C++ Programming: From Problem Analysis to Program Design, Sixth Edition.

Slides:



Advertisements
Similar presentations
DATA STRUCTURES USING C++ Chapter 5
Advertisements

Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
Queue Definition Ordered list with property: –All insertions take place at one end (tail) –All deletions take place at other end (head) Queue: Q = (a 0,
Queues. Queue Definition Ordered list with property: All insertions take place at one end (tail) All insertions take place at one end (tail) All deletions.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
What is a Queue? n Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the.
Queues CS 3358 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: – Elements are added.
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.
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
1 A full binary tree A full binary tree is a binary tree in which all the leaves are on the same level and every non leaf node has two children. SHAPE.
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
DATA STRUCTURE & ALGORITHMS
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
The Template Class Chain Chain Linear list. Each element is stored in a node. Nodes are linked together using pointers.
Queues.
1 C++ Plus Data Structures Nell Dale Queues ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified.
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.
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.
Chapter 18: Stacks and Queues
Chapter 18: Stacks and Queues
Implementing a Queue as a Linked Structure CS 308 – Data Structures.
Chapter 17: Stacks and Queues
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Chapter 7 Stacks Dr. Youssef Harrath
Reference: Vinu V Das, Principles of Data Structures using C and C++
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues.
C++ Classes and Data Structures Jeffrey S. Childs
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R1. Elementary Data Structures.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other.
Data Structures Using C++ 2E Chapter 8 Queues. Data Structures Using C++ 2E2 Objectives Learn about queues Examine various queue operations Learn how.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
Chapter 18: Stacks and Queues
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,
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Data Structures Using Java1 Chapter 7 Queues. Data Structures Using Java2 Chapter Objectives Learn about queues Examine various queue operations Learn.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Data Structures Using C++
Queues CS 302 – Data Structures Sections 5.3 and 5.4.
1 Chapter 7 The Linked List as a Data Structure. 2 The List ADT A list is a list of elements. The list of elements consist of the data acted upon by list.
Data Structures: Advanced Damian Gordon. Advanced Data Structure We’ll look at: – Linked Lists – Trees – Stacks – Queues.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Scis.regis.edu ● CS-362: Data Structures Week 8 Dr. Jesús Borrego 1.
CHAPTER 17 LINKED LISTS. In this chapter, you will:  Learn about linked lists  Become aware of the basic properties of linked lists  Explore the insertion.
Data Structures – Week #4 Queues. January 12, 2016Borahan Tümer, Ph.D.2 Outline Queues Operations on Queues Array Implementation of Queues Linked List.
1 Chapter 17: Stacks and Queues Learn about stacks Examine various stack operations Learn how to implement a stack as an array Learn how to implement a.
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Chapter 17: Stacks and Queues. Objectives In this chapter, you will: – Learn about stacks – Examine various stack operations – Learn how to implement.
Queues 1. Introduction A sequential collection of data Changes occur at both ends, not just one Queue applications –files waiting to be printed –"jobs"
  A linked list is a collection of components called nodes  Every node (except the last one) contains the address of the next node  The address of.
1 Data Structures and Algorithms Queue. 2 The Queue ADT Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
CSCE 210 Data Structures and Algorithms
Data Structures Using C++ 2E
CS505 Data Structures and Algorithms
QUEUES.
Stack and Queue APURBO DATTA.
Queues Chapter 4.
DATA STRUCTURE QUEUE.
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
CSC 248 – fundamentals of Data structure
Data Structures Using C++ 2E
Getting queues right … finally (?)
Queues Dr. Anwar Ghani Lecture 06: Queue Data Structures
Data Structures & Programming
Presentation transcript:

Jerry Lebowitz

 Stacks  Queues

3 C++ Programming: From Problem Analysis to Program Design, Sixth Edition

 A queue is a data structure in which elements are added at one end, called the rear, and deleted from the other end, called the front ◦ A first in first out (FIFO) data structure ◦ The middle elements of the queue are inaccessible  The rear of the queue is accessed whenever a new element is added to the queue  The front of the queue is accessed whenever an element is deleted from the queue

 Consider a line of customers in a bank, where customers are waiting either to withdraw/deposit money or for some other business ◦ Each new customer gets in the line at the rear and whenever a teller is ready for a new customer, the customer at the front of the line is served

 Queue Operations ◦ initializeQueue ◦ destroyQueue ◦ isEmptyQueue ◦ isFullQueue ◦ addQueue  Operation that adds an element to the queue ◦ deQueue  Operation removes the front element from the queue and stores the front element

 Suppose that front gives the index of the first element of the queue, and rear gives the index of the last element of the queue ◦ To add an element (addQueue)to the queue, first advance rear to the next array position and then add the element to the position that rear is pointing to ◦ To delete (deQueue) an element from the queue, first retrieve the element front is pointing to and then advance front to the next initially, the queue is empty

template class queueType { public: const queueType & operator=(const queueType &); void initializeQueue(); void destroyQueue(); int isEmptyQueue(); int isFullQueue(); void addQueue(Type queueElement); void deQueue(Type& deqElement); queueType(int queueSize = 100); queueType(const queueType & otherQueue); ~queueType(); private: int maxQueueSize; int count; int front; int rear; Type *list; };

template void queueType ::addQueue(Type newElement) { rear = (rear + 1) % maxQueueSize;//use mod operator to advance rear because the array is circular count++; list[rear] = newElement; }

template void queueType ::deQueue(Type& deqElement) { deqElement = list[front]; count--; front = (front + 1) % maxQueueSize; //use mod operator to advance front because the array is circular }  See example queue1.cpp

 Two pointers, front and rear, are used to maintain a queue //Definition of the node template struct nodeType { Type info; nodeType *link; };

template class linkedQueueType { public: const linkedQueueType & operator (const linkedQueueType &); bool isEmptyQueue(); bool isFullQueue(); void destroyQueue(); void initializeQueue(); void addQueue(const Type& newElement); void deQueue(Type& deqElement); linkedQueueType (); //default constructor linkedQueueType(const linkedQueueType & otherQueue); ~linkedQueueType(); //destructor private: nodeType *front; nodeType *rear; };

template void linkedQueueType ::addQueue (const Type& newElement) { nodeType *newNode; newNode = new nodeType ; //create the node newNode->info = newElement; //store the info newNode->link = NULL; if(front == NULL) //if initially the queue is empty { front = newNode; rear = newNode; } else//add newNode at the end { rear->link = newNode; rear = rear->link; } }//end addQueue

template void linkedQueueType ::deQueue(Type& deqElement) { nodeType *temp; deqElement = front->info; temp = front; front = front->link; delete temp; //delete the first node if(front == NULL) //if after deletion the queue is empty rear = NULL; //set rear to NULL }//end deQueue  See example queue2.cpp