Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 21 Data Structures, Algorithms and Complexity Stacks and Queues GRIFFITH COLLEGE DUBLIN.

Similar presentations


Presentation on theme: "Lecture 21 Data Structures, Algorithms and Complexity Stacks and Queues GRIFFITH COLLEGE DUBLIN."— Presentation transcript:

1 Lecture 21 Data Structures, Algorithms and Complexity Stacks and Queues GRIFFITH COLLEGE DUBLIN

2 Lecture 22 Introduction Stacks and queues are dynamic sets in which the element removed from the set by the Delete operation is prespecified In a stack, the element deleted from the set is the one most recently inserted The stack implements a last-in, first-out, or LIFO policy In a queue the element deleted is the one that has been in the set the longest time The queue implements a first-in, first-out, or FIFO policy

3 Lecture 23 Abstract Data Type ADT In computer science it makes sense to discuss data structures in conjunction with the operations that apply to them An abstract data type (ADT) is a set of data items and fundamental operations on this set Encapsulation is combining the data and operations that manipulate the data in one place. With an ADT the user is only interested in the operations, and not on how the data is stored

4 Lecture 24 The Stack ADT Stacks are widely used in computer science They can be used for maintaining flow of control with nested functions and loops They are also used for calculating arithmetic

5 Lecture 25 Implementing a Stack The insert operation on a stack is called Push and the delete operation is called Pop These names are allusions to physical stacks, such as the spring-loaded stacks of plates used in cafeterias. The order in which plates are popped from the stack is the reverse of the order in which they were pushed onto the stack, since only the top plate is accessible We can implement a stack in a number of ways. Here we will use an array

6 Lecture 26 The Stack ADT We can implement a stack of at most n elements with an array S[0.. n-1] The array has an attribute top[S] that indexes the most recently inserted element The stack consists of elements S[0.. Top[S]], where S[0] is the element at the bottom of the stack and S[top[S]] is the next available position When top[S] = 0, the stack contains no elements and is empty

7 Lecture 27 The Stack ADT In (a) the stack has 4 elements (b) is the stack after the calls Push(S, 17) and Push(S, 3) (c) is the stack after the call Pop(S) has returned the element 3 92615 6543210 (a) Top[S] = 4 31792615 6543210 (b) Top[S] = 6 31792615 6543210 (c) Top[S] = 5

8 Lecture 28 Stack Operations Stack-Empty(S)Stack-Full(S) if top[S] = 0 then if top[S] = length(S) then return truereturn true else return false endif endalg Pop(S)Push(S, x) if Stack-Empty(S) then if Stack-Full(S) then error “underflow”error “overflow” else top[S] = top[S] – 1S[top[S]]= x return S[top[S]]top[S] = top[S] + 1 endif endalg

9 Lecture 29 The Queue ADT Queues are also widely used in computer science For example in printing across networks or in a multi-tasking environment

10 Lecture 210 Implementing a Queue The insert operation on a stack is called Enqueue and the delete operation is called Dequeue Like a queue at a bank counter items are enqueued at the tail of the queue Items are dequeued from the head of the queue Again we can implement a queue in a number of ways. Here we will use an array

11 Lecture 211 Simple Queue Implementation We can implement a queue using an array as follows Let the head of the queue always be at position zero Enqueue items after that. Each time an item is dequeued, move the other items up a space 415 543210 Q Tail[Q] = 2 44 543210 Q Tail[Q] = 1

12 Lecture 212 Queue Operations Queue-Empty(Q)Queue-Full(Q) if tail[Q] = 0 then if tail[Q] = length(Q) thenreturn true else return false endif endalg

13 Lecture 213 Queue Operations Enqueue(Q, x)Dequeue(Q) if Queue-Full(Q) then if Queue-Empty(Q) then error “overflow”error “underflow” else Q[tail[Q]] = xtemp = Q[0] tail[Q] = tail[Q] + 1for i = 0 to tail[Q] - 1 endif Q[i] = Q[i+1] endalgendfor tail[Q] = tail[Q] –1 return temp endif endalg

14 Lecture 214 Circular Queue Implementation A problem with the above is that we have to close the gap each time an item is dequeued How do we manage when to avoid this when there is limited memory available? Another implementation is to have both a head[Q] and a tail[Q] Again we can have a queue of at most n-1 items Both move up the array and ‘wrap round’ when they reach the end

15 Lecture 215 Circular Array Head and tail can ‘wrap around’ in the array 489615 11109876543210 Head[Q] = 6 Tail[Q] = 11 1748961553 11109876543210 Head[Q] = 7 Tail[Q] = 2

16 Lecture 216 Circular Queue Operations Queue-Empty(Q)Queue-Full(Q) if head[Q] = tail[Q] then if head[Q] = tail[Q] + 1 or return true ( head[Q] = 0 and else tail[Q] = length[Q] - 1) return false return true endif else endalgreturn false endif endalg

17 Lecture 217 Circular Queue Operations Enqueue(Q, x)Dequeue(Q) if Queue-Full(Q) then if Queue-Empty(Q) then error “overflow” error “underflow” else Q[tail[Q]] = x x = Q[head[Q]] if tail[Q] = length[Q] - 1 then if head[Q] = length[Q]–1 then tail[q] = 0head[Q] = 0 else else tail[Q] = tail[Q] + 1head[Q] = head[Q] + 1 endif endif endif return xendalg

18 Lecture 218 Summary An abstract data type ADT is the encapsulation of the data and the operations to model some structure Stacks and Queues are dynamic data sets allowing the insertion and deletion of items in a prespecified way Stacks follow a LIFO policy Queues follow a FIFO policy Both can be implemented using arrays The Queue can have a simple or circular implementation


Download ppt "Lecture 21 Data Structures, Algorithms and Complexity Stacks and Queues GRIFFITH COLLEGE DUBLIN."

Similar presentations


Ads by Google