STACK
STACK(Definition)
• Stack is an ordered collection of items in
which items may be inserted at only one end,
called top of the stack.
• Stack is also called Last-in-First-Out (LIFO)data
structure as last element inserted will be
removed first.
• The insert operation on its stack is called
PUSH.
• The delete operation on its stack is called POP.
• Stacks are dynamic data structures that follow
the Last In First Out (LIFO) principle. The last
item to be inserted into a stack is the first one
to be deleted from it.
• For example, you have a stack of trays on a
table. The tray at the top of the stack is the
first item to be moved if you require a tray
from that stack.
Features of stacks
• Dynamic data structures
• Do not have a fixed size
• Do not consume a fixed amount of memory
• Size of stack changes with each push() and
pop() operation.
• Each and operation increases and decreases
the size of the stack by , respectively.
STACK IMPLEMENTATION USING ARRAY
• Algorithm stackfull(S)
Input:S->Stack S is to be created
Output:S->S is full return 1,oterwise 0
1.if top=MAX-1 then
2. return 1
3. else
4. Return 0
Algorithm stackempty(S)
Input:S->Stack S is to be created
Output:S->S is empty return 1,oterwise 0
1.if top=-1 then
2. return 1
3. else
4. Return 0
Algorithm PUSH(S,value)
Input:S->Stack S onto which an element is to be
added
Value->The element which to be added
Output:S->Stack after insertion of a value
TOP->Adjusted TOP due to insertion of an
element “value”
1. if stackfull(S) then
2. print “Stack Overflow”
3.else
4. TOP🡨 TOP+1
5. S[TOP]🡨 Value
End if
6.End
Algorithm POP(S)
Input:S->Stack S onto which an element is to be
removed
Output:S->Stack after insertion of a value
Value->Popped element if stack is not empty
1. if stackempty(S) then
2. print “Stack Underflow”
3. return(-999)
end if
4.Value🡨 S[TOP]
5.TOP🡨 TOP-1
6. return(value)
7. End
Algorithm Display(S)
Input:S->Stack S is to be created
Output:S->Displays the created stack
1. Repeat for i🡨 TOP to 0
2. Print “S[i]”
End for
3.End