data structure by digvijay

13
STACK REPRESENTATION BY- DIGVIJAY SINGH KARAKOTI

Upload: digvijay-karakoti

Post on 14-Jun-2015

147 views

Category:

Education


2 download

TRANSCRIPT

Page 1: Data structure by Digvijay

STACK REPRESENTATION

BY- DIGVIJAY SINGH KARAKOTI

Page 2: Data structure by Digvijay

WHAT IS STACK?Stack is the non-linear non-primitive data

structure or an ordered pair of elements.

A Stack is a LIFO structure which follows the principal of “LAST IN FIRST OUT”, which

depicts the insertion of last element in the stack and deletion of first element from the

stack from one end called “TOP” of the stack.

Page 3: Data structure by Digvijay

Some real life examples of Stack

Page 4: Data structure by Digvijay

APPLICATIONS OF STACK

Expression evaluation

Infix, Prefix and Postfix Notation

Expression conversion

Memory Management

Recursion

Page 5: Data structure by Digvijay

OPERATIONS PERFORMED IN STACK

 The major operations performed by stackare:

PUSH( ) – This operation is used to insert the element into the stack. So, it is also known as “INSERTION” operation.

POP( ) - This operation is used to delete or retrieve the element from the stack. So, it is also known as “DELETION” operation.

Page 6: Data structure by Digvijay

PUSH OPERATION

This function will add elements/items into the stack. Whenever we add an element

into the stack the pointer TOP gets incremented as:-

This is used to show the position of the element in the stack.

TOP+1 or TOP++

Page 7: Data structure by Digvijay

PUSH operation on stack is to add values into the stack. Let us assume that 5 items 30, 20, 25, 10 and 40 are to be placed on the stack. The items can be inserted one by one as shown in

following figure:-

Page 8: Data structure by Digvijay

ALGORITHM FOR PUSH OPERATION

PUSH(MAXSTK, ITEM, TOP)

STEP 1: If TOP = MAXSTK THEN [Stack already filled?] 

Print "OVERFLOW" Go to step 4 End if 

STEP 2: TOP = TOP + 1 [Increase TOP by 1] 

STEP 3: Set STK[TOP] = ITEM [Insert ITEM in new TOP position] 

STEP 4: End

Page 9: Data structure by Digvijay

Program implementation of PUSH operation

void push(){ int item;If(TOP==maxsize-1){ printf(“\n Stack is full”);getch();exit(0);}else{ printf(“\n Enter the element to be inserted”);scanf(“%d”, &item);TOP=TOP+1;stack[TOP]=item;}}

Page 10: Data structure by Digvijay

POP OPERATION

This function will delete elements/items from the stack. Whenever we add an

element into the stack the pointer TOP gets decremented as:-

This is used to show the position of the element in the stack

TOP-1 or TOP--

Page 11: Data structure by Digvijay

POP operation on stack is to delete or retrieve values from the stack. Let us assume that 4 items 15, 12, 10 and 5 are to be

deleted from the stack. The items can be deleted one by one as shown in following figure:-

Figure. Deletion operations

Page 12: Data structure by Digvijay

ALGORITHM FOR POP OPERATION

POP[STACK,TOP,ITEM]STEP 1: If TOP=0

Print ”Underflow”

STEP 2: Set ITEM=STACK[TOP]

STEP 3: Set TOP=TOP-1

STEP 4: Return

Page 13: Data structure by Digvijay

Program implementation of POP operation

int pop(){ int item;if(TOP==-1){ printf(“\n Stack is empty”);getch();exit(0);}else{ item=stack[TOP];TOP=TOP-1;printf(“\n Item deleted is= %d”, item);}return(item);}