a stack is a data linear data structure in which addition of new element or deletion of an existing...

29
PRESENTATION OF STACK AND QUEUE

Upload: kai-hardgrave

Post on 16-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

PRESENTATION OF STACK AND QUEUE

Page 2: 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

STACK

Page 3: 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

STACK:

• 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).

Page 4: 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

PUSH POP

Page 5: 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

ALGORITHM FOR STACK : (push)

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”

Page 6: 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

C function for push operation on stack : 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”); } }

Page 7: 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

Algorithm for stack: (pop)

POP(stack[MAXSIZE],item) : This algorithm deletes an item from the top of the stack STEP-1: Repeat steps 2 to 4 until top>=0

STEP-2: Set item = stack [top]

STEP-3: Set top = top - 1

STEP-4: Print No. deleted is , item

STEP-5: Print stack underflow

Page 8: 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

C function for pop operation on stack int stack [5],top; void pop ( ) { int item; if (top>=0) { item = stack [top]; top=top-1; printf("\n Number deleted is = %d ", item); } else { printf("\n stack is empty"); }}

Page 9: 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

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

ADT Stack is

Page 10: 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

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 TRUEelse return FALSE

Element Pop(stack)::= if(IsEmpty(stack))return else remove and return the element at

the top of the stack.

Page 11: 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

Direct applicationsPage-visited history in a Web browserUndo sequence in a text editorSaving local variables when one function calls another, and this one calls another, and so on.

Indirect applicationsAuxiliary data structure for algorithmsComponent of other data structures

Application of stack

Page 12: 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

QUEUE

Page 13: 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

QUEUE :

• 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).

Page 14: 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

ENQUEUE

BACK FRONT

DEQUEUE

Page 15: 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

Algorithm for insertion of queue :

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

Page 16: 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

front = 0 rear = 0 Else rear = rear + 1

STEP-5: Set Queue [Rear] = item

STEP-6: Print, Queue overflow

Page 17: 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

C function for insertion of Queue :

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; }

Page 18: 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

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

Page 19: 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

Algorithm for deletion of queue :

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

Page 20: 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

STEP-4: Print “ Number deleted is ”, item

STEP-5: print “ Queue is empty ”

Page 21: 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

C function for deletion operation in queue :

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

Page 22: 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

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

Page 23: 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

ADT for Queue

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

Page 24: 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

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

Page 25: 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

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

Page 26: 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

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

Page 27: 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

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.

APPLICATION OF QUEUE :

Page 28: 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

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.

Page 29: 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

THANK YOU