Presentation is loading. Please wait.

Presentation is loading. Please wait.

MEMORY REPRESENTATION OF STACKS

Similar presentations


Presentation on theme: "MEMORY REPRESENTATION OF STACKS"— Presentation transcript:

1 MEMORY REPRESENTATION OF STACKS
A stack can be represented in memory either as an array or as a singly linked list. In both the cases, insertion and deletion of elements is allowed at one end only. Insertion and deletion in the middle of the array or the linked list is not allowed. An array representation of a stack is static but linked list representation is dynamic in nature. Though array representation is a simple technique. it provides less flexibility and is not very efficient with respect to memory utilization because of wastage of memory space. Conversely, if the number of elements to be handled by the stack is more than the size of the stack, then it will not be possible to increase the size of stack to store these elements. .

2 To insert an element 1 in the STACK, Top is incremented by one and the element 1 is stored at STACK [Top]. Similarly, other elements can be added to the STACK until Top reaches 2 (see Figure 3.3). To pop an element from the STACK (data element 3), Top is decremented by one, which removes the element 3 from the STACK. Similarly, other ele­ments can be removed from the STACK until Top reaches -1. Figure 3.3 shows different states of STACK after performing push and pop operations on it.

3

4 struct stack { int item[MAX]; int Top; };
To implement stack as an array in C language, the following structure named stack needs to be defined. struct stack { int item[MAX]; int Top; };

5

6 APPLICATIONS OF STACKS
reversing strings check­ing whether the arithmetic expression is properly parenthesized converting infix notation to postfix and prefix notations evaluating postfix expressions implementing recursion and function calls


Download ppt "MEMORY REPRESENTATION OF STACKS"

Similar presentations


Ads by Google