DATA STRUCTURE & ALGORITHMS - STACK DATA STRUCTURE & ALGORITHMS
What is a Data Structure Stack?
An Abstract Data Type (ADT) used in the programming languages is known as a Stack. As the name implies it works as a real stack like a deck of cards or pile of plates.
A real-world stack allows operations at one end only. For instance, a card or plate can be placed or removed from the top of the stack only. Even Stack ADT allows the data operations at only on end. Only the top element of a stack can be accessed at any time.
This feature makes it LIFO data structure. LIFO stands for Last-in-first-out. The element placed last is accessed first. In Stack, operation is called PUSH operation and removal operation is called POP operation.
How Data Structure Stack is represented?
The following diagram depicts a stack and its operations −
Array, structure, pointer and Linked list can implement a Stack. Stack may be of a fixed size or a have an option of dynamic resizing. In this tutorial stacks are implemented using arrays and hence makes it a fixed stack implementation.
What are the basic operations supported by Stack?
The operations supported by stack involve initializing of the stack, de-initializing. Apart from these, the other operations supported by Stack are:
- push() − Pushing (storing) an element on the stack.
- pop() − Removing (accessing) an element from the stack.
By checking the status of the tack, the stack is used efficiently for which some of the functions are added to the stacks:
- peek() − get the top data element of the stack, without removing it.
- isFull() − check if stack is full.
- isEmpty() − check if stack is empty.
At all times, a pointer is maintained to the last PUSHed data on the stack. As this pointer always represents the top of the stack, hence named top. The top pointer provides top value of the stack without actually removing it.
What are the procedures to support Stack functions?
Procedures to support stack functions are−
peek()
Algorithm of peek() function −
Implementation of peek() function in C programming language −
Example
isfull()
Algorithm of isfull() function −
Implementation of isfull() function in C programming language −
Example
isempty()
Algorithm of isempty() function −
Implementation of isempty() function in C programming language is slightly different. Top is initialized at -1 and the index in array starts from 0. Check if the stock is zero or -1 and determine that the stack is empty. The code for this is:
Example
What are the steps involved in a Stack Push Operation?
The process of putting a new data element onto stack is known as a Push Operation. Push operation involves a series of steps −
- Step 1 − Checks if the stack is full.
- Step 2 − If the stack is full, produces an error and exit.
- Step 3 − If the stack is not full, increments top to point next empty space.
- Step 4 − Adds data element to the stack location, where top is pointing.
- Step 5 − Returns success.
If the linked list is used to implement the stack, then in step 3, allocate space dynamically.
Algorithm for PUSH Operation
A simple algorithm for Push operation can be derived as follows −
Implementation of this algorithm in C, is very easy by the code −
Example
What are the steps involved in a Stack Pop Operation?
Pop operation is to access the content while removing it from the stack. In an array implementation of pop() operation, top is decremented to a lower position in the stack to point to the next value and the data element is not actually removed.But in linked-list implementation, pop() actually removes data element and deallocates memory space.
The steps in the Pop operation are −
- Step 1 − Checks if the stack is empty.
- Step 2 − If the stack is empty, produces an error and exit.
- Step 3 − If the stack is not empty, accesses the data element at which top is pointing.
- Step 4 − Decreases the value of top by 1.
- Step 5 − Returns success.
Algorithm for Pop Operation
A simple algorithm for Pop operation can be derived as follows −
Implementation of this algorithm in C, is as follows −
Example
No comments:
Post a Comment