Introduction to Stack Data Structure

The stack is an ordered set of elements in which insertion and deletion are from one end of the stack called the top of the stack. It is a Last in First Out structure.




A. Static Implementation:
A stack is implemented statically by using an array of size MAX to hold stack elements and an integer top storing the position of the top of the stack. A stack is a single entity that is a structure made up of both the array and the top. The stack elements can be integers, characters, strings or user-defined types.

The operations to be performed on a stack are:
1. init(S) – Create an empty stack by initializing top to -1 indicating the stack is empty
2. push(S, x) – adding an element x to the stack S
3. pop (S) – deletes and returns the top element from the stack S
4. Peek(S) - returns the top element from the stack S without deleting the element from the stack
5. isEmpty(S) – Check if the stack is empty
6. isFull(S) – check if the stack is full which happens when top equals MAX -1

B. Dynamic Implementation:
A stack is implemented dynamically by using a Linked list where each node in the linked list has two parts, the data element and the pointer to the next element of the stack. The stack is a single entity i.e. a pointer pointing to the top node in the linked list. The stack elements can be integers, characters, strings or user-defined types. There is no restriction on how big the stack can grow.
The operations to be performed on a stack are:
1. init(S) – Create an empty stack S using the linked list and by initializing S to NULL indicating the stack is empty.
2. push(S, x) – Adding an element x to the stack S requires creation of node containing x and putting it in front of the top node pointed by S. This changes the top node S and the function should return the changed value of S. The function call will be as follows



S=push(S,x);
3. pop (S) – deletes the top node from the stack S so that next element becomes the top. Since the top node S is changed function should return the changed value of S. The function call will be as follows
S=pop(S);
4. peek(S) - returns the data element in the top node of the stack S.  
5. isEmpty(S) – Check if the stack is empty which is equivalent to checking if S==NULL


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments:

Thanks
Mukesh Rajput