A stack is a data linear data structure in which addition of new element or deletion of an existing element always takes place at the same end. This.

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be instantiated.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
CSCE 3110 Data Structures & Algorithm Analysis Stacks and Queues Reading: Chap.3 Weiss.
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.
Data Structures (Second Part) Lecture 3 : Array, Linked List, Stack & Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering.
CSCE 3110 Data Structures & Algorithm Analysis Queues Reading: Chap. 3 Weiss.
A queue is an ADT which allows data values to be accessed only one at a time and only the first inserted. The rule imposed on a queue is: First In First.
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.
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.”
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Introduction to Stacks & Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Data Structure Dr. Mohamed Khafagy.
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Data Structures: Lists i206 Fall 2010 John Chuang Some slides adapted from Glenn Brookshear, Brian Hayes, or Marti Hearst.
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.
Implementing and Using Stacks
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
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.”
Stacks and queues Basic operations Implementation of stacks and queues Stack and Queue in java.util Data Structures and Algorithms in Java, Third EditionCh04.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R1. Elementary Data Structures.
Cosc237/data structures1 Data Types Every data type has two characteristics: 1.Domain - set of all possible values 2.set of allowable operations Built-in.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Welcome to CSCE 221 – Data Structures and Algorithms
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
Queues 1. Queue  a queue represents a sequence of elements where elements can be added at the back of the sequence and removed from the front of the.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.
CS 221 Analysis of Algorithms Data Structures. Portions of the following slides are from  Goodrich and Tamassia, Algorithm Design: Foundations, Analysis.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
CSCE 3110 Data Structures & Algorithm Analysis
Stacks.
Data Structures Using C, 2e
Queues.
UNIT II Queue.
Stacks.
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Queue data structure.
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
CSCE 3110 Data Structures & Algorithm Analysis
Queues Mohammad Asad Abbasi Lecture 5
CSCE 3110 Data Structures & Algorithm Analysis
Stack and Queue.
CMSC 341 Lecture 5 Stacks, Queues
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
CSCE 3110 Data Structures & Algorithm Analysis
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Instructor: Mr.Vahidipour
Queues.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues.
Queues: Implemented using Arrays
Queues 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
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.
Stacks and Queues.
Data Structures and Algorithms
DATA STRUCTURES IN PYTHON
Presentation transcript:

A stack is a data linear data structure in which addition of new element or deletion of an existing element always takes place at the same end. This end is called as top of the stack When an item is added to a stack, the operation is called as push when an item is removed from the stack, the operation is called as pop stack is also called as last–in-first-out (LIFO).

PUSH POP

PUSH (stack[MAX_SIZE],item) : This algorithm an item at the top of the stack[MAX_SIZE]. STEP-1: Initialize Set top = -1 STEP-2: Repeat step-3 to 5 until top<MAXSIZE-1 STEP-3: Read item STEP-4: Set top=top+1 STEP-5:Set stack[top]=item STEP-6:Print “stack overflow”

int stack[5], top = -1; void push( ) { int item; if (top<=4) { printf(“\n Enter the number”); scanf(“%d”, &item); top=top+1; stack [top] = item; } else { printf(“\n stack overflow”); }

 objects : a finite ordered list with zero or more elements.  functions: for all stack € Stack, item € element, maxStackSize € positive integer. Stack CreateS(maxStackSize)::= create an empty stack whose maximum size is maxStackSize Boolean IsFull( stack, maxStackSize )::= if(number of elements in stack==maxStackSize) return TRUE else return FALSE

Stack Push( stack, item)::= if(IsFull(stack))stack Full else insert item into top of stack and return. Boolean IsEmpty(stack)::= if(stack==CreateS( maxStackSize ) return TRUE else return FALSE Element Pop(stack)::= if(IsEmpty(stack))return else remove and return the element at the top of the stack.

 Direct applications  Page-visited history in a Web browser  Undo sequence in a text editor  Saving local variables when one function calls another, and this one calls another, and so on.  Indirect applications  Auxiliary data structure for algorithms  Component of other data structures

Queue is a linear data structure that permits the insertion of new element at one end and deletion of an element at the other end. The end at which the deletion of an element takes place is called as front and the end at Which insertion of new element can take place is called as rear. Queue is also called as first-in-first-out (FIFO).

ENQUEUE BACKFRONT DEQUEUE

QINSERT (Queue[MAXSIZE],item) : This algorithm inserts an item at the rear of queue [MAXSIZE] STEP-1: Initialisation. set front = -1 set rear = -1 STEP-2: Repeat steps 3 to 5 until rear<MAXSIZE -1 STEP-3: Read item STEP-4: If front = = -1 then

front = 0 rear = 0 Else rear = rear + 1 STEP-5: Set Queue [Rear] = item STEP-6: Print, Queue overflow

int queue [5], front = -1, rear = -1 ; Void queue ( ) { int item ; if (rear < 4) { printf(“\n Enter the number ”); scanf(“%d ” & item ); if (front = = -1 ) { front = 0 ; rear = 0; }

else { rear = rear + 1 ; } queue [rear] = item ; } else { printf (“\n Queue is full”); }

QDELETE (queue [MAXSIZE], item ) : This algorithm deletes an item at the front of the queue [MAXSIZE]. STEP-1: Repeat steps 2 to 4 until front >= 0 STEP-2: Set item = queue [front] STEP-3: If front = = rear set front = -1 set rear = -1 else front = front + 1

STEP-4: Print “ Number deleted is ”, item STEP-5: print “ Queue is empty ”

int queue [5], front, rear ; void delete ( ) { int item ; If ( front ! = -1) { item = queue [front]; if (front = = rear ) { front = -1; rear = -1 ; }

front = front +1 ; } printf (“\n Number deleted is = %d “, item ); } else { printf( “ Queue is empty”); }

ADT Queue is objects: a finite ordered list with zero or more elements. functions: for all queue € Queue,item € element,maxQueueSize € positive integer

Queue CreateQ(maxQueueSize)::= create an empty queue whose maximum size is maxQueueSize Boolean IsFullQ(queue,maxQueueSize)::= if(number of elements in queue==maxQueueSize) return TRUE else return FALSE

Queue AddQ(queue,item)::= if(IsFullQ(queue))queueFull else insert item at rear of queue and return queue Boolean IsEmptyQ(queue)::= if(queue==CreateQ(maxQueueSize)) return TRUE else return FALSE

Element DeleteQ(queue)::= if(IsEmptyQ(queue)) return else remove and return the item at front of queue.

1)Serving requests of a single shared resource (printer, disk, CPU),transferring data asynchronously (data not necessarily received at same rate as sent) between two processes (IO buffers), e.g., pipes, file IO, sockets. 2) Call center phone systems will use a queue to hold people in line until a service representative is free.

3) When a resource is shared among multiple consumers. Examples include CPU scheduling, Disk Scheduling. 4) Buffers on MP3 players and portable CD players, iPod playlist. Playlist for jukebox – add songs to the end, play from the front of the list.