stacks and queues

28
STACKS AND QUEUES

Upload: kaiser

Post on 24-Feb-2016

36 views

Category:

Documents


0 download

DESCRIPTION

STACKS AND QUEUES. data. data. data. next. next. next. NULL. A LINKED LIST IMPLEMENTATION OF A QUEUE. queue. Queue: First-In-First-Out (FIFO) data structure. cnt. front. rear. node. node. node. The header file: queue.h. #define EMPTY 0 #define FULL 10000 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: STACKS AND QUEUES

STACKS AND QUEUES

Page 2: STACKS AND QUEUES

A LINKED LIST IMPLEMENTATION OF A QUEUE

data nextdata next

NULLdata next

cnt

front

rear

queue

node nodenode

Queue: First-In-First-Out (FIFO) data structure

Page 3: STACKS AND QUEUES

The header file: queue.h

#define EMPTY 0#define FULL 10000

typedef int data;typedef enum {false, true} boolean;

struct node { int d; struct node *next;

};

typedef struct node node;

struct queue { int cnt; /* count of the elements */

node *front; /* ptr to the front element */ node *rear; /* ptr to the rear

element */};

typedef struct queue queue;

Page 4: STACKS AND QUEUES

The header file: queue.h

data front(const queue *q);

boolean isempty(const queue *q);

boolean isfull(const queue *q);

void initialize(queue *q);

void enqueue(data x, queue *q);

data dequeue(queue *q);

Page 5: STACKS AND QUEUES

Basic queue routines : ISEMPTY(),ISFULL()

boolean isempty(const queue *q) /* RETURNS TRUE IF QUEUE IS EMPTY */

{ return ((boolean) (q -> cnt == EMPTY));

/* if(q->cnt==EMPTY) return true; else return false

*/ }

boolean isfull(const queue *q) /* RETURNS TRUE IF QUEUE IS FULL */

{ return ((boolean) (q -> cnt == FULL)); }

Page 6: STACKS AND QUEUES

Basic queue routines : FRONT() , INITIALIZE()

data front(queue *q) // RETURNS DATA OF FRONT NODE

{ return (q -> front -> d); }

void initialize(queue *q) // Count=0, No front and rear nodes

{ q -> cnt = 0;q -> front = NULL;

q -> rear = NULL; }

Page 7: STACKS AND QUEUES

Basic queue routines : ENQUEUE()

/* Creating queue enqueue */void enqueue(data x, queue *q){ node *p; p = malloc(sizeof(node)); // CREATE A NEW NODE p -> d = x; p -> next = NULL;

if (!isempty(q)) /* IF QUEUE IS NOT EMPTY APPEND NEW NODE FROM REAR */

{ q -> rear -> next = p; q -> rear = p; }

else /* IF QUEUE IS EMPTY NEW NODE WILL BE FRONT AND REAR */

q -> front = q -> rear = p;q -> cnt++; }

Page 8: STACKS AND QUEUES

data dequeue(queue *q){ data x;

node *p; x = q -> front -> d; /* store data in x */ p = q -> front; /* store

address of front node that will be deleted */

q -> front = q -> front -> next; /* determine new

front node */ q -> cnt- - ; /* decrease count

*/ free(p); /* free node */ return x; }

Basic queue routines : DEQUEUE()

Page 9: STACKS AND QUEUES

void print(queue *q){

node * p; for(p=q->front; p!=NULL; p=p-

>next)printf("%d ", p->d);

printf(“\n”);}

Basic queue routines :PRINT()

Page 10: STACKS AND QUEUES

main(){ queue my_q;data x; int op;

initialize(&my_q);

do{ printf(" PLEASE SELECT OPERATION\n");

printf(" ENQUEUE 1\n DEQUEUE 2\n FRONT 3\n IS FULL 4\n

IS EMPTY 5\n PRINT 6 \n EXIT 7\n " );

scanf("%d",&op);

/* SWITCH- CASE NEXT SLIDE */

} while(op!=7);}

Page 11: STACKS AND QUEUES

switch(op){ case 1 : printf("Enter data : "); scanf("%d",&x);

enqueue(x,&my_q); break;

case 2: dequeue(&my_q); break;

case 3: printf(" front: %d",front(&my_q));

break;case 4: if(isfull(&my_q)==true)

printf("Queue is FULL");else printf("Queue is NOT

FULL");break;

case 5: /* similar to case 6 */

case 6: print(&my_q); break;

case 7: exit(0);

default : break; }

Page 12: STACKS AND QUEUES

Array Implementation of Queues

• An array to store elements, Array, and the positions Front and Rear to represent the ends of the queue are kept.

• We also keep track of the number of elements: Size.

12

Page 13: STACKS AND QUEUES

13

Array Implementation of Queues – Problem

5 2 7 1

Front Rear

• To enqueue an element X, increment Size and Rear and set Array[Rear]=X

• To dequeue an element, set the return value to Array[Front], decrement Size and then increment Front.

• Assume after several enqueue operations, the Rear is at the last index position. Assume also that some elements, in the meantime, have been dequeued to make up room. The next enqueue would fail, although there would be free slots.

Page 14: STACKS AND QUEUES

• The simple solution is whenever Front or Rear gets to the end of the Array, it is wrapped around to the beginning (hence the name circular array).

14

Array Implementation of Queues – Problem Solved

2 4

Front Rear

1 2 4

FrontRear

1 3 2 4

FrontRear

initially

After enqueue(1)

After enqueue(3)

Page 15: STACKS AND QUEUES

QUEUE : Array Implementation

#define MAX_QUEUE_SIZE 10

typedef struct { int cnt;int front;int rear;data[MAX_QUEUE_SIZE];

} queue;

Page 16: STACKS AND QUEUES

QUEUES(array imp. ) : Basic routines

void initializeQueue(queue * q){

q -> cnt = 0;q -> front = 0;q -> rear = -1;

}

boolean IsEmpty( queue * q ) { return q->cnt == 0;}

boolean IsFull( queue * q ) { return q->cnt == MAX_QUEUE_SIZE;}

Page 17: STACKS AND QUEUES

QUEUES(array imp. ) : Basic routinesvoid enqueue(queue * q, int x){

if (!IsFull(q)){q->rear++;q->cnt++;if (q->rear == MAX_QUEUE_SIZE)

q->rear = 0;q->data [q->rear] = x;

}}

Page 18: STACKS AND QUEUES

QUEUES(array imp. ) : Basic routinesint dequeue(queue * q){

if (!IsEmpty(q)){int x=q->data[q->front];q->cnt--;q->front++;if (q->front == MAX_QUEUE_SIZE)

q->front= 0; return x;

}}

Page 19: STACKS AND QUEUES

STACKSSAMPLE PSEUDO CODE:

push(1) to Stackpush(2) to Stackpush(3) to Stackpop() from Stackpush(4) to Stack

Page 20: STACKS AND QUEUES

STACKS : Array Implementation

#define STACK_SIZE 4

typedef struct { int data [STACK_SIZE];

int top;} stack;

Page 21: STACKS AND QUEUES

STACKS(array imp. ) : Basic routines

void reset(stack * stk){ stk->top=-1; }

void push( int c, stack * stk){ if(stk->top!= STACK_SIZE-1)

{ stk->top++; stk->data[stk->top]=c;

}else printf(“Stack is full!!\n”);

}

int pop (stack * stk){ if(stk->top!=-1)return (stk->data[stk-

>top- -]) else printf(“stack is empty”);}

Page 22: STACKS AND QUEUES

STACKS (array implementation): main()

x= pop(&n);

push(11,&n);

x= pop(&n);x= pop(&n);x= pop(&n);

push(3,&n);push(2,&n);

x= pop(&n); }

main(){

stack n; int x;reset(&n);

push(4,&n);push(5,&n);push(3,&n);push(2,&n);push(1,&n);

Page 23: STACKS AND QUEUES

Stacks: Linked List Implementation

Page 24: STACKS AND QUEUES

#define EMPTY 0 #define FULL 10000

typedef char data;typedef enum {false, true} boolean;

typedef struct { data d; struct node *next; } node ;

typedef struct{ int cnt; /* count of the elements */ node *top; /* pointer to the top element */ } stack;

The header file: stack.h

Page 25: STACKS AND QUEUES

boolean empty(const stack *stk);

boolean full(const stack *stk);

void initialize(stack *stk);

void push(data d, stack *stk);

char pop(stack *stk);

char top(stack *stk);

The header file: stack.h

Page 26: STACKS AND QUEUES

boolean isempty(const stack *stk)

{ return (stk -> cnt == EMPTY); }

boolean isfull(const stack *stk)

{ return (stk -> cnt == FULL); } void initialize(stack *stk)

{ stk -> cnt = 0; stk -> top = NULL; }

data top(stack *stk) { return (stk -> top -> d); }

Basic routines : Isempty(),Isfull(), Initialize(), Top()

Page 27: STACKS AND QUEUES

void push(data d, stack *stk)

{ /* create new node */ node *p; p = malloc(sizeof(node)); p -> d = d; p -> next = stk -> top;

/* assign new node as top node */ stk -> top = p; stk -> cnt++; }

Basic routines : Push(),Pop()

data pop(stack *stk) {

data x; // will store the top datanode *p; x = stk -> top -> d; p = stk -> top; stk -> top = stk -> top -> next; stk -> cnt- -; free(p); return x; }

Page 28: STACKS AND QUEUES

void main(void) { char str[] = “CS115”;

int i; stack s; initialize(&s); /* initialize the stack */

for (i = 0; str[i] != '\0'; ++i) /* fill stack */

{ if (!full(&s)) push(str[i], &s); }

printf(“String in the stack: ");

while (!empty(&s)) putchar(pop(&s)); }