Algorithms and data structures Protected by 4.5.2015.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

CSCE 3110 Data Structures & Algorithm Analysis
Algorithms and data structures Protected by
418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Ceng-112 Data Structures I Chapter 5 Queues.
Algorithms and data structures
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.
Module R2 Overview. Process queues As processes enter the system and transition from state to state, they are stored queues. There may be many different.
EngageNY.org ©2012 Core Knowledge Foundation. This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
Data Structures (Second Part) Lecture 3 : Array, Linked List, Stack & Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Queue Overview Queue ADT Basic operations of queue
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
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.
LUNG HEALTH IN EUROPE FACTS AND FIGURES. KEY AIMS ‘Lung Health in Europe – Facts and Figures’ is a concise version of the European Respiratory Society.
Algorithms and data structures Protected by
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
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++
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
Stacks And Queues Chapter 18.
Algorithms and data structures Protected by
Queue 09/10/081. Queue (Linear Queue) It is a linear data structure consisting of list of items. In queue, data elements are added at one end, called.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
Data Structures – Week #4 Queues. January 12, 2016Borahan Tümer, Ph.D.2 Outline Queues Operations on Queues Array Implementation of Queues Linked List.
CE 221 Data Structures and Algorithms
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Evaluating individuals Evaluating classes Noting unusual patterns or trends Identifying gaps in learning USE OF SCORES.
Sven Koerber-Abe, 2013 Begrüßen und Verabschieden Begrüßen und Verabschieden.
Give Eg:? Queues. Introduction DEFINITION: A Queue is an ordered collection of element in which insertions are made at one end and deletions are made.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 15 1.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
الطابور QUEUE (abstract data type) واحد من هياكل البيانات الخطية الشائعة الاستخدام داخل البرامج. يحتوي علي عناصر من نفس النوع. من أنواع البيانات الخطية.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
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.
Grammatik: sollen Sven Koerber-Abe, 2015.
Data Structures Using C, 2e
CS505 Data Structures and Algorithms
Using Queues: Coded Messages
Creative Commons Introduction webinar for librarians
Grammatik: nehmen, essen, möchten
Grammatik: wohnen mögen
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
CSCE 3110 Data Structures & Algorithm Analysis
Training of Trainers Workshop
Writing the Methods Section
Einkaufen in Deutschland
DATA STRUCTURE QUEUE.
Writing the Results Section
Training of Trainers Workshop
Preparing Tables and Figures: Some Basics
The Structure of Journal Articles
Barbara Gastel INASP Associate
Grammatik: Das ist ein …
Barbara Gastel INASP Associate
Review & Lab assignments
Ja / Doch Sven Koerber-Abe, 2013.
Queues FIFO Enqueue Dequeue Peek.
Grammatik: wohnen mögen
Writing the Introduction
Grammatik: ich, mich Sven Koerber-Abe, 2014.
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Grammatik: nehmen, essen, möchten
Presentation transcript:

Algorithms and data structures Protected by

Creative Commons n You are free to: share — copy and redistribute the material in any medium or format share — copy and redistribute the material in any medium or format adapt — remix, transform, and build upon the material adapt — remix, transform, and build upon the material n Under the following terms: Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use. Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use. NonCommercial — You may not use the material for commercial purposes. NonCommercial — You may not use the material for commercial purposes. ShareAlike — If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original. ShareAlike — If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original. No additional restrictions — You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits. Text copied from Algorithms and data structures, FER Notices: You do not have to comply with the license for elements of the material in the public domain or where your use is permitted by an applicable exception or limitation. No warranties are given. The license may not give you all of the permissions necessary for your intended use. For example, other rights such as publicity, privacy, or moral rights may limit how you use the material. 2 / 17

Queues

Algorithms and data structures, FER Queue n Queue is a linear list where insertion ( enqueueing ) is performed on one end and removal ( dequeueing ) on the other. Principle FirstInFirstOut ( FIFO ) Principle FirstInFirstOut ( FIFO ) Two indices are used ( front & rear ) Two indices are used ( front & rear ) – The end of the queue where insertion is performed is rear – The other end where removal is performed is at the beginning of the queue, front functions: enqueue, dequeue functions: enqueue, dequeue 4 / rear 2 front

Algorithms and data structures, FER Circularity n An efficient way of queue implementation using static structure is a one dimensional array of a given data type, used circularly Circularity is achieved using the operator modulo ( % ) Circularity is achieved using the operator modulo ( % ) n One element of the array remains empty In this way we can differ between an empty and a full queue In this way we can differ between an empty and a full queue – Empty queue: rear == front – Full queue: (rear + 1) % maxelements == front If the whole array were filled with data, an additional element counter would be required If the whole array were filled with data, an additional element counter would be required  RedPoljem (QueueByArray) typedef struct { type array[MAXQUE]; int rear, front; } Queue; 5 / 17

Algorithms and data structures, FER Circular list 6 / 17 queue->front queue->rear

Algorithms and data structures, FER An empty queue - circular array implementation 7 / 17 queue->front queue->rear 0

Algorithms and data structures, FER A full queue - circular array implementation queue->rear queue->front 8 / 17

Algoritmi i strukture podataka, FER9 / queue->rear queue->frontEnqueueing int enqueue (type element, Queue *queue) { if ((queue->rear+1) % MAXQUE == queue->front) return 0; queue->rear++; queue->rear %= MAXQUE; queue->array[queue->rear] = element; return 1; } Calling program: #define MAXQUE 16 enqueue(5, &queue); enqueue(2, &queue); enqueue(7, &queue); enqueue(-4, &queue); enqueue(1, &queue); 16

Algorithms and data structures, FER Dequeueing from a circular array - I int dequeue (type *element, Queue *queue) { if (queue->rear == queue->front) return 0; queue->front++; queue->front %= MAXQUE; *element = queue->array[queue->front]; return 1; } queue->rear queue->front Calling program: #define MAXQUE 16 dequeue(&number, &queue); / 17

Algorithms and data structures, FER queue->rear queue->front Dequeueing from a circular array - II int dequeue (type *element, Queue *queue) { if (queue->rear == queue->front) return 0; queue->front ++; queue->front %= n; *element = queue->array[queue->front]; return 1; } Calling program: #define MAXQUE 16 dequeue(&number, &queue); 11 / 17

Algorithms and data structures, FER Queue – list implementation I n A disadvantage while using array is the fixed memory allocation Engagement of not used memory (most often) Engagement of not used memory (most often) Insufficient memory (occasionally) Insufficient memory (occasionally) n List implementation with dynamic memory allocation Only memory in use is engaged Only memory in use is engaged The size of a queue is limited only due to the total available memory The size of a queue is limited only due to the total available memory typedef struct at atom; struct at { int element; struct at *next; }; typedef struct { atom *rear, *front; } Queue; next element atom 12 / 17

Algorithms and data structures, FER Queue – list implementation II Empty queue 5242 Non empty queue  RedListom (QueueByList) queue->front queue->rear queue->front queue->rear void init_queue(Queue *queue){ queue->rear = NULL; queue->front = NULL; } 13 / 17

Algorithms and data structures, FER Calling program: int enqueue (int element, Queue *queue) { atom *new; if (new = (atom*) malloc (sizeof (atom))) { new->element = element; new->next = NULL; if (queue->front == NULL) queue->front = new;// if queue is empty else (queue->rear)->next = new;// else, put at the end queue->rear = new;// remember the last return 1; } return 0; } Equeueing – empty queue Queue queue; init_queue(&queue); enqueue(52, &queue); 52 queue->front queue->rear new 14 / 17

Algorithms and data structures, FER Enqueueing – non empty queue Calling program: int enqueue (int element, Queue *queue) { atom *new; if (new = (atom*) malloc (sizeof (atom))) { new->element = element; new->next = NULL; if (queue->front == NULL) queue->front = new;// if queue is empty else (queue->rear)->next = new;// else, put at the end queue->rear = new;// remember the last return 1; } return 0; } Queue queue; init_queue(&queue); enqueue (52, &queue); enqueue (42, &queue); 52 queue->front queue->rear new / 17

Algorithms and data structures, FER Dequeueing int dequeue (int *element, Queue *queue) { atom *old; if (queue->front) { // if queue is not empty *element = queue->front->element; // element to be removed old = queue->front; // remember the current front queue->front = queue->front->next; // new front free (old); // release the memory of the removed element if (queue->front == NULL) queue->rear = NULL; // empty queue return 1; } return 0; } 52 queue->front queue->rear old 42 Calling program: dequeue(&number,&queue); 16 / 17

Algorithms and data structures, FER Dequeueing of the last element int dequeue (int *element, Queue *queue) { atom *old; if (queue->front) { // if queue is not empty *element = queue->front->element; // element to be removed old = queue->front; // remember the current front queue->front = queue->front->next;// new front free (old);// release the memory of the removed element if (queue->front == NULL) queue->rear = NULL; // empty queue return 1; } return 0; } queue->front queue->rear old 42 Calling program: dequeue(&number,&queue); 17 / 17