Monday 23 September 2013

Basic Stack Operations












Basic stack operation :

stack member functions

Member function
job of the function
Isempty()
return true if stack is empty, return false otherwise

Isfull()
return true if stack is full, return false otherwise
Push()
add an data to the stack
Pop()
delete the data from the stack
Display()
print all the data of the stack
Topmost()
Returns top most data of the stack



In a stack, one end is a closed end and the other end is open end. Elements can be inserted or deleted only from the open end. An element that is entered last will be the first element that will be deleted. That is why stack are also called as Last In First Out (LIFO) data structures.

Example:




Order of insertion:

1. Element A is inserted as first element.
2. Element B is inserted as second element.
3. Element C is inserted as third element.
4. Element D is inserted as fourth element.
5. If we try to insert element E into the stack, it will result in overflow, because the stack does not have space to hold the fifth element.


Order of deletion:
1. Element D is deleted as first element.
2. Element C is deleted as second element. It is not possible to delete element C without deleting element D frist, because element D is on top of element C.
3. Element B is deleted as third element. It is not possible to delete element B without deleting element C frist, because element C is on top of element B.
4. Element A is deleted as fourth element. It is not possible to delete element A without deleting element B frist, because element B is on top of element A.
Maximum size of the stack represents number of storage locations a stack can have for holding its elements. Stack also maintains a pointer called top which always points to the top of the stack. It is important to note that initially the pointer top = -1. Whenever an element is to be inserted on to the stack then top must be increased by one. Whenever an element is deleted from the stack then top must be decremented by one.



Stack push operations
Stack pop operations


Top most value of the stack
 

Program to convert decimal to binary using Stack
Reverse string using stack
dynamic Stack data structure program  
 (or)
Implementation of stack using Arrays:

Static implementation of stack

Program in c
     

Stack:

No comments:

Post a Comment