Blog Details
| Blog Title: | Stack | Data Structure |
|---|---|
| Blogger: | rutikaundale@gmail.com |
| Image: | View |
| Content: | What is Stack Data Structure?Stack is a linear data structure that follows a specific order that performs the activities. It's just like a stack of overlapping plates. It works on the Last-In-First-Out (LIFO) principle–the last item to be positioned is the first item to be released. It's an abstract type of data. It is widely used in most languages of programming.It is named stack as it behaves like a real-world stack. For example, a deck of plates. Working of Stack : Stack data structure works on LIFO basis. LIFO stands for Last-in-first-out. Here, the element which is inserted at last, is accessed first. In stack terminology, insertion operation is called PUSH operation and removal operation is called POP operation. It has only one pointer TOP that points the last or top most element of Stack. Insertion and Deletion in stack can only be done from top only.
The most significant thing here is the TOP point. At the time of stack initialization, we need to set its value to-1 so we can check if the stack is empty or not by comparing TOP==-1. Whenever an item is inserted, boost the TOP value by 1 and place the fresh item in the TOP-pointed position. When removing an element, return the TOPpointed element and reduce its value by 1. Before operating push() it is necessary to verify whether the stack is already complete. [ Stack Overflow Condition ] Check if the stack is already empty before the pop() procedure. [Condition of stack underflow ] Stack OperationsStack is an ADT (Abstract Data structure). We can implement this linear structure using array, linked list or some other way. It is supported in different programming languages like C, C++, Java, C# etc. We can perform following operations in a stack: Push()Push() is used to add an element to the top of stack. Before inserting an element into the stack, we must check for stack overflow i.e. whether the stack has space to add an item or its already full.
void push(int data)
{
if(!isFull()) {
top = top + 1;
stack[top] = data;
}
else
{
printf("Stack is already full.\n");
}
}
|

