stack and queue
play

Stack and Queue ADT Stack Queue 2 ADT Example All main programs - PDF document

Advanced Programming Stack - Queue Summary Stack and Queue ADT Stack Queue 2 ADT Example All main programs rely on concept of Abstract It is possible to define an ADT corresponding Data Type (ADT). to a generic set of elements,


  1. Advanced Programming Stack - Queue Summary Stack and Queue  ADT  Stack  Queue 2 ADT Example All main programs rely on concept of Abstract It is possible to define an ADT corresponding Data Type (ADT). to a generic set of elements, on which some operations are defined, like union , intersection ADT are useful to define algorithms in a more and difference : general way.  union (X, Y)  intersect (X, Y) An ADT is a mathematical model on top of which a set of operations is defined.  diff (X, Y) These 3 operations have operands 3 4 Example of SET operations ADT extends primitive types X Y  ADT concept combines and generalizes concepts of primitive types and procedures. Y  Primitive types are the ones supported by a X language (e.g. int, float, char ).  UNION (X, Y)  Using ADT it is possible to extend the set of types of supported data.  An ADT is defined independently from:  INTERSECT(X,Y)  The programming language  Implementation choices about data structures  DIFF (X, Y) 5 6 1

  2. Advanced Programming Stack - Queue Implementation in C Example When implementing an ADT in C: Actual Type Definition:  Define a separate file containing the typedef int SET_ELEM; functions corresponding to the operations ADT operations: defined by the ADT SET_ELEM *union(SET_ELEM *, SET_ELEM *);  Define a data type corresponding to the SET_ELEM *intersect(SET_ELEM *, SET_ELEM *); ADT SET_ELEM *diff (SET_ELEM *, SET_ELEM *);  Access the ADT only by means of its SET_ELEM *make_null( void); operations. int size (SET_ELEM *); void dump (SET_ELEM *); 7 8 Stack Example Suppose we execute these operations on a stack: A STACK is an ADT with these operations: M = init()  Push: insert a new element in the ADT Push (M, K1)  Pop: extract from ADT the last element Push (M, K2) inserted (LIFO = Last In First Out policy) Push (M, K3)  Empty: retrun true if the stack has no If Pop(M) is called it returns K3. Then another elements call to Pop(M) will return ritorna K2.  Init: create an empty ADT. Calling then Empty (M) it returns FALSE. 9 10 Implementing stack with array Pseudo-code  Define an array of N elements  Add a variable top containing the index of last inserted element. Array memory is allocated in advance, whatever is the number of elements stored. All operations on a stack have complexity O(1). 11 12 2

  3. Advanced Programming Stack - Queue C Stack Example (n=6) int pop( void) Empty stack #define MAX 100 Init { int buff[MAX]; return( buff[--index]); int index; } void push( int val); int empty( void) 0 1 2 3 4 5 int pop( void); { int empty( void); if( index == 0) void push( int val) return (1); { else buff[index++] = val; index return (0); return; } } 13 14 Example (2) Example (3) Push( S, A) Push( S, B) 0 1 2 3 4 5 0 1 2 3 4 5 A A B index index 15 16 Example (4) Queue A queue is an ADT with these operations: Pop( S ) == B  Insert (or enqueue): insert a new element in ADT  Extract (or dequeue): extract from ADT the 0 1 2 3 4 5 “oldest” element; it realizes a FIFO ( First In First Out ) strategy. A  Empty: retrun true if ADT has 0 elements  Init: create an empty ADT. index 17 18 3

  4. Advanced Programming Stack - Queue Example Implementing queue with array Suppose we have executed these operations:  Definition of an array of N+1 elements M = init()  Introduciton of 2 variables head e tail: Enqueue (M, K1)  Head contains the index of the oldest Enqueue (M, K2) element, the one inserted for more time  Tail contains index in which insert a new Enqueue(M, K3) element. If Dequeue(M) is executed this returns K1.  A mechanism for transofrming an array Executing again Dequeue(M) , this returns K2. in a circular buffer. Calling Empty(M) at this point, it returns FALSE. 19 20 Circular Buffer Example (n=5) Head and tail are incremented whenever dequeue ed enqueue are called, respectively. Whenever head or tail become equals to n+1, they have to be reset at 1. At the beginning, head = 1, tail = 1. 1 2 3 4 5 6 When the queue is full (overflow) then head = tail + 1 (or head=1 and tail=n). When the queue is empty (underflow) head = tail. 21 22 Example (1) Example (2) Empty queue Init Enqueue( Q, A) 1 2 3 4 5 6 1 2 3 4 5 6 A head head tail tail 23 24 4

  5. Advanced Programming Stack - Queue Example (3) Example (4) Enqueue( Q, B) Enqueue( Q, C) 1 2 3 4 5 6 1 2 3 4 5 6 A B A B C head head tail tail 25 26 Example (5) Example (6) Enqueue( Q, D) Dequeue( Q)==A 1 2 3 4 5 6 1 2 3 4 5 6 A B C D B C D head head tail tail 27 28 Example (7) Example (8) Full queue Enqueue( Q, E) Enqueue( Q, F) 1 2 3 4 5 6 1 2 3 4 5 6 B C D E B C D E F head head tail tail 29 30 5

  6. Advanced Programming Stack - Queue Example (9) Example (10) Full queue Dequeue( Q) ==B Enqueue( Q, G) 1 2 3 4 5 6 1 2 3 4 5 6 C D E F G C D E F head head tail tail 31 32 Example (11) Example (12) Dequeue( Q) == C Dequeue( Q) 1 2 3 4 5 6 1 2 3 4 5 6 G D E F G E F head head tail tail 33 34 Example (13) Example (14) Enqueue( Q, H) Dequeue( Q) 1 2 3 4 5 6 1 2 3 4 5 6 G H E F G H F head head tail tail 35 36 6

  7. Advanced Programming Stack - Queue Example (15) Example (16) Dequeue( Q) Dequeue( Q) 1 2 3 4 5 6 1 2 3 4 5 6 G H H head head tail tail 37 38 Example (17) Pseudo-code Empty queue Dequeue( Q) 1 2 3 4 5 6 head tail 39 40 C C int insert( int elem) { int extract( void) if( (tail+1) < head || #define DIM 10 { int ret; int empty( void) (tail==DIM && head==0) ) int buffer[DIM+1]; { return( -1); int tail=0, if( head == tail) if( head == tail) head=0; return( -1); return( 1); buffer[tail++] = elem; int insert( int elem); ret = buffer[head++]; else if( tail == (DIM+1)) int extract( void); if( head == (DIM+1)) return( 0); tail = 0; int empty( void); head = 0; } return( 0); return( ret); } } 41 42 7

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend