41
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 1 Data Type Stack & Queue Lect 27 Goutam Biswas

Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 1'

&

$

%

Data Type Stack & Queue

Lect 27 Goutam Biswas

Page 2: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 2'

&

$

%

Stack and Queue

Both stack and queue are important data typesused in computing. They are essentially lists ofdata with restricted entry and exit orderings.

Lect 27 Goutam Biswas

Page 3: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 3'

&

$

%

Use of Stack

1. Most modern computer architecture

supports hardware stack to implement

recursive programming, exception handling,

system call implementation.

2. Compiler uses stack for syntax checking and

semantic action.

Lect 27 Goutam Biswas

Page 4: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 4'

&

$

%

Basic Operations on a Stack

init() −→ s:Stack, empty stack.

isEmpty(s) −→ b:Boolean

top(s) −→ v:Data, if s is not empty

error, otherwise

push(s, v) −→ t:Stack

pop(s) −→ t:Stack, if s is not empty

error, otherwise

Lect 27 Goutam Biswas

Page 5: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 5'

&

$

%

A Few Axioms of the Stack Operations

isEmpty(init()) = true

isEmpty(push(s, v)) = false

pop(init()) = error

pop(push(s, v)) = s

top(init()) = error

top(push(s, v)) = v

The axioms define the operations.

Lect 27 Goutam Biswas

Page 6: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 6'

&

$

%

Stack: an Example

init() → [], empty stack

push([], 5) → [5]

push([5], 7) → [5 7]

push([5 7], 3) → [5 7 3]

pop([5 7 3]) → [5 7]

push([5 7], 10) → [5 7 10]

top([5 7 10]) → 10

LIFO list.

Lect 27 Goutam Biswas

Page 7: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 7'

&

$

%

Basic Operations on a Queue

init() → q:Queue, an empty queue

isEmpty(q) → b:Boolean

front(q) → d:Data, if q is not empty

→ error, otherwise

add(q, d) → p:Queue

delete(q) → p:Queue, if q is notempty

→ error, otherwise

Lect 27 Goutam Biswas

Page 8: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 8'

&

$

%

Note

The operation add is also called insert,enqueue; similarly the operation delete is alsocalled dequeue.

Lect 27 Goutam Biswas

Page 9: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 9'

&

$

%

A Few Axioms of the Queue Operations

isEmpty(init()) = true

isEmpty(add(q, d)) = false

delete(init()) = error

delete(add(q, d)) = q, if q is empty

= add(delete(q), d), otherwise

front(init()) = error

front(add(q, v)) = v, if q is empty

= front(q), otherwise

Lect 27 Goutam Biswas

Page 10: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 10'

&

$

%

Queue: an Example

init() → [], empty queue

add([], 5) → [5]

add([5], 7) → [5 7]

add([5 7], 3) → [5 7 3]

delete([5 7 3]) → [7 3]

add([7 3], 10) → [7 3 10]

front([7 3 10]) → 7

FIFO list.

Lect 27 Goutam Biswas

Page 11: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 11'

&

$

%

Implementation of Stack & Queue

A stack (queue) may grow to any arbitrary size.So an ideal stack (ideal queue) cannot beimplemented in a real machine (finite capacity).We can implement an approximation of a stack(queue).

Lect 27 Goutam Biswas

Page 12: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 12'

&

$

%

Stack on an Array: data representation

typedef struct {

int data[SIZE];

int tos; // top of stack

} stack;

Lect 27 Goutam Biswas

Page 13: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 13'

&

$

%

3

stack

tos

data20

25

10

30

0

1

2

3

Lect 27 Goutam Biswas

Page 14: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 14'

&

$

%

Stack on an Array: operations

void init(stack *) ;

int push(stack * , int) ;

int pop(stack *) ;

int top(stack *, int *) ;

int isEmpty(stack *) ;

int isFull(stack *) ; // For finite size

Lect 27 Goutam Biswas

Page 15: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 15'

&

$

%

Note

The data type stack is a big structure and weshall always pass a pointer to it to avoid largevolume of data copy in parameter passing(call-by value).

Lect 27 Goutam Biswas

Page 16: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 16'

&

$

%

stack.h

#ifndef _STACK_H

#define _STACK_H

#define SIZE 200

#define ERROR 1

#define OK 0

typedef struct {

int data[SIZE];

int tos;

} stack ;

void init(stack *) ;

int push(stack * , int) ;

Lect 27 Goutam Biswas

Page 17: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 17'

&

$

%

int pop(stack *) ;

int top(stack *, int *) ;

int isEmpty(stack *) ;

int isFull(stack *) ; // For finite size

#endif

Lect 27 Goutam Biswas

Page 18: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 18'

&

$

%

Implementation: stack.c

#include "stack.h"

void init(stack *s) // stack.c

{ s->tos = -1;}

int isFull(stack *s)

{ return s->tos == SIZE-1;}

int isEmpty(stack *s)

{ return s->tos == -1;}

int push(stack *s, int n) {

if(isFull(s)) {

Lect 27 Goutam Biswas

Page 19: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 19'

&

$

%

printf("The STACK is full\n");

return ERROR ;

}

s->data[++s->tos]=n;

return OK ;

}

int Pop(stack *s) {

if(isEmpty(s)) {

printf("The STACK is empty\n");

return ERROR ;

}

s -> tos-- ;

return OK ;

Lect 27 Goutam Biswas

Page 20: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 20'

&

$

%

}

int Top(stack *s , int *val) {

if(isEmpty(s)) {

printf("The STACK is empty\n") ;

return ERROR ;

}

*val = (s -> data[s -> tos]) ;

return OK ;

}

Lect 27 Goutam Biswas

Page 21: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 21'

&

$

%

Compiling the datatype

$ cc -Wall -c stack.cWe get the object module stack.o. We canconstruct library from it.

Lect 27 Goutam Biswas

Page 22: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 22'

&

$

%

User Program: testStack.c

#include <stdio.h>

#include "stack.h"

int main() // testStack.c

{

stack s ;

int x , err , val ;

char c ;

init(&s);

printf(" ’U’ for push (U 15)\n ’O’ for pop\n ’T’ for top

printf(" ’E’ for exit :\n");

Lect 27 Goutam Biswas

Page 23: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 23'

&

$

%

while((c = getchar()) != ’e’ && c != ’E’)

switch(c) {

case ’u’ :

case ’U’ :

scanf("%d",&x);

err = push(&s,x);

break;

case ’o’ :

case ’O’ :

err = pop(&s);

break;

case ’t’ :

case ’T’ :

err = top(&s , &val) ;

Lect 27 Goutam Biswas

Page 24: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 24'

&

$

%

if(!err) printf("%d\n", val);

break;

case ’\n’ :

case ’\t’ :

case ’ ’ :

break;

default :

printf("Token Unknown\n");

}

return 0;

}

Lect 27 Goutam Biswas

Page 25: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 25'

&

$

%

Compiling the User Program

$ cc -Wall testStack.c stack.oWe get the executable module a.out.

Lect 27 Goutam Biswas

Page 26: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 26'

&

$

%

Queue on Circular Array: Representation

#define MAX 200

typedef struct {

int data[MAX] ;

int front , rear ;

} queue;

The queue may contain MAX - 1 data.

Lect 27 Goutam Biswas

Page 27: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 27'

&

$

%queue

data

front

rear

20

5

10

2

5

0

123

4

5

6 7

Lect 27 Goutam Biswas

Page 28: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 28'

&

$

%

data

front

rear

20

123

4

5

6 7

2

empty queue

Lect 27 Goutam Biswas

Page 29: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 29'

&

$

%

data

front20

123

4

5

6 7

5

12

7

20 37

13

71 2 rear

full queue

Lect 27 Goutam Biswas

Page 30: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 30'

&

$

%

Queue on Circular Array: Operations

void init(queue *) ;

int add(queue *, int) ;

int delete(queue *);

int front(queue *, int *) ;

int isEmpty(queue *) ;

int isFull(queue *) ;

Lect 27 Goutam Biswas

Page 31: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 31'

&

$

%

Interface File: queue.h

#include <stdio.h>

#ifndef _QUEUE_H

#define _QUEUE_H

#define MAX 200

#define ERROR 1

#define OK 0

typedef struct { // queue.h

Lect 27 Goutam Biswas

Page 32: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 32'

&

$

%

int data[MAX];

int front, rear;

} queue;

/* Queue may contain MAX-1 data.*/

void init(queue *);

int add(queue *, int);

int delete(queue *);

int front(queue *, int *);

int isEmpty(queue *);

Lect 27 Goutam Biswas

Page 33: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 33'

&

$

%

int isFull(queue *);

#endif

Lect 27 Goutam Biswas

Page 34: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 34'

&

$

%

Implementation File: queue.c

#include "queue.h"

void init(queue *q) // queue.c

{ q->front=q->rear=0; }

int isEmpty(queue *q)

{ return q->rear == q->front; }

int isFull(queue *q)

{ return (q->rear+1)%MAX == q->front; }

Lect 27 Goutam Biswas

Page 35: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 35'

&

$

%

int add(queue *q, int n) {

if(isFull(q)) return ERROR;

q->rear=(q->rear+1)%MAX;

q->data[q->rear]=n;

return OK ;

}

int delete(queue *q) {

if(isEmpty(q)) return ERROR ;

q->front=(q->front+1)%MAX ;

Lect 27 Goutam Biswas

Page 36: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 36'

&

$

%

return OK ;

}

int front(queue *q , int *v) {

if(isEmpty(q)) return ERROR ;

*v=q->data[(q->front+1)%MAX] ;

return OK ;

}

Lect 27 Goutam Biswas

Page 37: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 37'

&

$

%

User Program: testQueue.c

#include "queue.h"

int main() // testQueue.c

{

queue q ;

int x , err , val ;

char c;

init(&q);

printf(" ’A’ for add (A 15)\n") ;

printf(" ’D’ for delete\n ’F’ for front\n ’E’ for exit

while((c = getchar()) != ’e’ && c != ’E’)

switch(c) {

Lect 27 Goutam Biswas

Page 38: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 38'

&

$

%

case ’a’ :

case ’A’ :

scanf("%d",&x);

err = add(&q,x);

if(err) printf("The Queue is full\n") ;

break;

case ’d’ :

case ’D’ :

err = delete(&q);

if(err) printf("The Queue is empty\n") ;

break;

case ’f’ :

case ’F’ :

err = front(&q , &val) ;

Lect 27 Goutam Biswas

Page 39: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 39'

&

$

%

if(err) printf("The Queue is empty\n") ;

else printf("%d\n",val);

break;

case ’\n’ :

case ’\t’ :

case ’ ’ :

break;

default :

printf("Token Unknown\n");

}

return 0 ;

}

Lect 27 Goutam Biswas

Page 40: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 40'

&

$

%

Time Complexity of Operations

Both in stack and queue the running time ofeach operation is O(1). Similarly the spacecomplexity of each operation is also O(1).

Lect 27 Goutam Biswas

Page 41: Data TypeStack&Queuerogers/pds_theory/lect27.pdfBoth stack and queue are important data types used in computing. They are essentially lists of data with restricted entry and exit orderings

PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 41'

&

$

%

Queue on Circular Array: A Variation

We may use a counter and and keep data in all

the array locations. In practice the array

locations may be a structure and a counter is

just a variable of type int.

#define MAX 200

typedef struct {

int data[MAX] ;

int front, rear, count ;

} queue;

Lect 27 Goutam Biswas