Transcript of "Lecturestacks 110802115132-phpapp02-120309093554-phpapp02"
S T A Cwww.eshikshak.co.in K
INTRODUCTION● Array allows to insert and delete an element at any place in the list● STACK is a list of elements● It is a linear data structure● Insertion and deletion operation is performed only at one end – known as top of stack● Also known as Last-in-First-out (LIFO), entries are removed in reverse order of insertion www.eshikshak.co.in
BASIC OPERATIONS OF STACKS● push -- insert item on stack● pop -- remove item from stack● peek -- examine, but don’t remove item● isEmpty – reports whether or not stack is empty● isFull- reports whether or not stack is full● size – returns # of items in stack www.eshikshak.co.in
PUSH OPERATION ● Inserting element in a stack is known as PUSH ● When elements are inserted continuously to the stack it grows at one end i.e. top Top 9 => Top 6 8 => Top -7 -7 -7 => Top 8 8 8 8 => Top 5 5 5 5 5 =>Top =NULL www.eshikshak.co.in
POP OPERATION ● Removing of element from a stack is known as pop ● When elements are removed continuously from a stack, it shrinks at the same end i.e. topTop 9=> 8 Top 6 => -7 -7 Top -7 => 8 8 8 Top 8 => 5 5 5 5 Top 5 => Top = NULL www.eshikshak.co.in
STACKS CONDITIONS● Depending on implementation, ● Important to know if stack is may be necessary to check if empty -- attempt to remove an stack is full -- attempt to add item from an empty stack is an item to a full stack is an overflow underflow error. error Top 9 => 8 -7 8 5 Top = NULL www.eshikshak.co.in
PUSH(STACK, TOP, MAXSTK, ITEM)STACK - Linear Array, TOP – location of top element in stack,MAXSTK – Numbers of elements that be stored in Stack,ITEM – value that has to be inserted in stack STEP 1. if top = MaxStk, then print “Overflow” and return STEP 2. set top=top + 1 STEP 3. set STACK[top] = item STEP 4. Return www.eshikshak.co.in
PUSH(STACK, TOP)STACK - Linear Array, TOP – location of top element in stack,ITEM – value that has to be inserted in stack 1. [check for underflow of the stack] if TOP=-1 then Write(“stack underflow on pop) end if 2. [ Assign the top element of the stack to ITEM] ITEM = STACK[TOP] 2. [decrement pointer] TOP=TOP-1 3. [return former top element of the stack] Return(ITEM) 4. Exit www.eshikshak.co.in
PEEP(STACK,TOP)1. [check for stack underflow]if TOP-I+1<=0 thenWrite (“stack underflow on peep”)end if2. [Return the top element of the stack]Return(stack[TOP-MAX+1])3. Exit www.eshikshak.co.in
APPLICATIONS OF STACK● Undo● Redo● Function Call● Recursive Function Call● Convert infix to Postfix expression● Convert infix to Prefix expression● Evaluate infix expression● Tower of hanoi● Reverse string or list or file www.eshikshak.co.in
A particular slide catching your eye?
Clipping is a handy way to collect important slides you want to go back to later.