Stack Data Structure with PUSH,POP and STACK-EMPTY Algorithm and Code in C
Stack Data Structure
Stacks are data structures that follow the Last In First Out (LIFO) principle or First in last out(FILO) . 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.
Inserting and deleting elements
Stacks have restrictions on the insertion and deletion of elements. Elements can be inserted or deleted only from one end of the stack from the top. The element at the top is called the top element.
The operations of inserting element is called push() and deleting elements is called pop().
When the top element of a stack is deleted, if the stack remains non-empty, then the element just below the previous top element becomes the new top element of the stack.
For example, in the stack of trays, if you take the tray on the top and do not replace it, then the second tray automatically becomes the top element (tray) of that stack.
Size of stack changes with each push and pop operation. Each push
increases size of the stack by 1 and pop operation decreases the size of the stack by 1.
Application of stack
- Recursion
- Infix to postfix conversion
- Parsing:- matching pattern, match tags < >
- Browser
- Editors
- Tree Traversal
- Graph Traversal
The operations of inserting element is called push() and deleting elements is called pop().
increases size of the stack by 1 and pop operation decreases the size of the stack by 1.
Stack Operations
When top of stack = 0, stack contain no elements and is empty. Then we can check by STACK-EMPTY operation. If empty stack is popped, then we can say stack underflow.
If top of stack exceed the MAX size, the stack is overflow.
ALGORITHMS for PUSH and POP operations
STACK-EMPTY(S)
- if top[S]=0 //S = Stack
- then return TRUE
- else return FALSE
PUSH(S,item) //item to be inserted in stack
- top[S] <--- top[S] +1
- S[top[S]] <--- item
POP(S)
- if STACK-EMPTY(S)
- then return "underflow"
- else top[S] <--- top[S]-1
- return S[top[S]+1]
Comments
Post a Comment