Different Types of Stack OperationsIntroduction:In computer science, a stack is a fundamental data structure that follows the Last-In-First-Out (LIFO) principle.It is an abstract data type containing a number of pieces that can handle push and pop as its two primary operations.The push action adds an element to the top of the stack, whereas the pop operation removes and returns the topmost element from the stack. Additionally, there are other operations, such as peek, isEmpty, and isFull, which allow us to inspect the top element and check if the stack is empty or full, respectively. Stack OverviewA stack is a type of linear data structure that allows operations to be carried out on the top, or "top" of the stack, of the structure. The LIFO principle states that the last element added to the stack is also the first one to be taken away. The main operations that a stack can handle are: - Push: Adds an element to the stack's top.
- Pop: Removes the topmost stack element.
- Peek/Top: Retrieves the uppermost element while leaving the others in place.
- isEmpty: Verifies whether or not the stack is empty.
- isFull: In the event of a fixed-size stack implementation, this function determines if the stack is full or not.
Stack OperationsPush OperationAn element is added to the top of the stack during the push operation. It involves two main steps: - Check if the stack is full: In case the stack has a fixed size, it is essential to verify whether the stack has reached its maximum capacity before adding an element.
- Insert the new element: If the stack isn't already full, move the new element up the stack by increasing the top pointer.
Program Explanation: - The MAX_SIZE constant is defined using the preprocessor directive #define and is set to 100. This constant represents the maximum size of the stack array.
- Next, a class named Stack is defined. It has two private member variables: top and
- To keep track of the element that is currently at the top of the stack, we utilise the top variable.It is initialized to -1 in the constructor of the Stack class.
- The stackArrayis an integer array with a size of MAX_SIZE. It is used to keep the stack's elements in storage.
- The Stack class provides a public member function called push, which is used to add elements to the stack. It takes an integer element as a parameter.
- Inside the push function, it first checks if the top is already at the maximum size of the stack (MAX_SIZE - 1).If it is, it prints "Stack Overflow" and exits, signalling that the stack has reached its maximum capacity and cannot accept any more elements.
- If the stack is not full, the top is incremented, and the element is added to the stackArrayat the new top position. It also prints a message indicating that the element has been pushed to the stack.
- A Stack class object with the name stack is created in the main function. Then, the push function is called three times to push the elements 10, 20, and 30 onto the stack.
Program Output: Pop OperationThe topmost element in the stack is eliminated by the pop operation. It involves two primary steps: - Check if the stack is empty: It is crucial to verify whether the stack is empty before attempting to remove an element.
- Remove the top element: If the stack is not empty, remove the topmost element by decrementing the top pointer
Program Explanation: - The program defines a Stack class that represents a stack and maintains two private member variables: top and
- The stack's elements are kept in the "stackArray" integer array, which is used to contain the stack's elements, while the top variable is used to keep track of the element that is now at the top of the stack.
- The Stack class provides two main methods.
- Firstly, the push(int element)function is used to insert an element into the stack. By contrasting the current value of top with MAX_SIZE - 1, it determines whether the stack is already full.
- If the stack is full, the method terminates with a "Stack Overflow" warning. Otherwise, the top is incremented, and the given element is stored at the corresponding index in the stackArray. Additionally, a notification stating that the element was successfully added to the stack is shown.
- Second, the top element from the stack is removed and returned using the pop() It detects whether the stack is empty by comparing top's value to 0.
- If the stack is empty, a "Stack Underflow" message is displayed, and -1 is returned. Otherwise, the element at the current top index in stackArray is retrieved, top is decremented, and the element is returned.
- A Stack class object with the name stack is created in the main function. Using the push()function, elements 10, 20, and 30 are then added to the stack.
- After that, the pop()function is called to remove the top element from the stack, and the returned value is stored in the poppedElement
Program Output: Peek/Top OperationThe top element of the stack is retrieved by the peek (or top) operation without being removed. It involves a single step: - Check if the stack is empty: Ensure that the stack is not empty before attempting to retrieve the top element.
- Return the top element:If the stack doesn't include an empty element, return the top-most element.
Program Explanation: - The program starts by defining a Stack class that represents a stack. Inside the Stack class, there are two private member variables: top and
- The stack's elements are kept in the "stackArray" integer array, which is used to contain the stack's elements, while the top variable is used to keep track of the element that is now at the top of the stack.
- The Stack class provides two main methods. Firstly, the push(int element)function is used to insert an element into the stack.
- By contrasting the current value of top with MAX_SIZE - 1, it determines whether the stack is already full.A "Stack Overflow" message appears and the method exits if the stack is full.
- Otherwise, the top is incremented, and the given element is stored at the corresponding index in the stackArray. Additionally, a message confirming the successful adding of the element to the stack is shown.
- The top element of the stack is obtained from the second implementation using the peek()function without harming the stack itself.
- The value of top is compared to 0, which determines if the stack is empty. If the stack is empty, a message stating "Stack is Empty" is shown and -1 is returned. If not, the element at the stackArray's top index is returned.
- A Stack class object with the name stack is created in the main function. Then, using the push()function, elements 10, 20, and 30 are added to the stack.
- After that, the top element from the stack is retrieved using the peek()function, and the returned value is saved in thetopElement
Program Output: isEmpty Operation:The isEmpty function determines whether or not the stack is empty. It only requires one action: - Check the top pointer:The top pointer in the stack implementation keeps track of the stack's topmost element. The stack is empty if the top pointer is set to -1, meaning there are no elements in the stack.
- If top is equal to -1, the stack is empty.
- If top is not equal to -1, the stack contains elements and is not empty.
The isEmpty operation helps in determining whether it is safe to perform other operations on the stack, such as popping an element or accessing the top element using the peek operation. Program Explanation: - The program starts by defining a Stack class that represents a stack. Inside the Stack class, there are two private member variables: top and
- The stack's elements are kept in the "stackArray" integer array, which is used to contain the stack's elements, while the top variable is used to keep track of the element that is now at the top of the stack.
- The Stack class provides two main funtions. Firstly, the push(int element)function is used to insert an element into the stack.
- By contrasting the current value of top with MAX_SIZE - 1, it determines whether the stack is already full.A "Stack Overflow" message appears and the method exits if the stack is full.
- Otherwise, the top is incremented, and the given element is stored at the corresponding index in the stackArray. Additionally, a message confirming the successful adding of the element to the stack is shown.
- In addition, the isEmpty()function is used to determine whether the stack is empty.It returns true if the top variable is less than 0, indicating an empty stack. Otherwise, it returns false.
- A Stack class object with the name stack is created in the main function. The push()function is then used to put elements 10, 20, and 30 into the stack. The isEmpty()function is then used to determine whether the stack is empty.
Program Output: isFull Operation:The top pointer in the stack implementation represents the index of the stack's topmost element. When the top pointer equals the maximum size of the stack minus 1, it means that the stack is full and at its limit. - Check the top pointer:The top pointer in the stack implementation represents the index of the stack's topmost element. When the top pointer equals the maximum size of the stack minus 1, it means that the stack is full and at its limit.
- If top is equal to the maximum size minus 1, the stack is full.
- If top is not equal to the maximum size minus 1, the stack is not full and can accommodate more elements.
The isFull operation helps in determining whether it is safe to push additional elements into the stack without causing an overflow. Program Explanation: - The program starts by defining a Stack class that represents a stack. Inside the Stack class, there are two private member variables: top and
- The stack's elements are kept in the "stackArray" integer array, which is used to contain the stack's elements, while the top variable is used to keep track of the element that is now at the top of the stack.
- The Stack class has a number of methods for carrying out stack operations. The top variable is initialised to -1 by the constructor Stack() to denote an empty stack.
- We Utilise the push(int element) function, an element is added to the stack.It first checks if the stack is already full by calling the isFull() method.
- If the stack is full, the method terminates with a "Stack Overflow" warning. Otherwise, the top variable is incremented, and the given element is stored at the corresponding index in the stackArray. The successful addition of the element to the stack is indicated by a message printed.
- The pop()function removes the top element from the stack. It checks to see if the stack is empty by calling the isEmpty()
- A "Stack Underflow" notice is shown and the method exits if the stack is empty.Otherwise, the element at the current top index in the stackArray is retrieved and assigned to thepoppedElement
- The top variable is then decremented to reflect the removal of the element from the stack. A message is printed to indicate which element was popped from the stack.
- The peek() function gets the top element from the stack without removing it. It checks to see if the stack is empty by calling the isEmpty()
- If the stack is empty, a "Stack is empty" message is displayed, and -1 is returned to indicate an empty stack. Otherwise, the element at the current top index in the stackArray is returned.
- The isEmpty() function detects whether the stack is empty by comparing the top value with -1. It returns true if the stack is empty and false otherwise.
- The isFull()function checks if the stack is full by comparing the top variable with MAX_SIZE - 1, where MAX_SIZE is the maximum capacity of the stack.It returns true when the stack is full and false when it is not.
- A stack instance of the Stack class is created in the main() function. Using the push()function, elements 10, 20, and 30 are added to the stack.
- The top element of the stack is pulled out and printed using the peek()function, and two additional elements are pulled out of the stack using the pop()
- Finally, the isEmpty() and isFull()functions are called to check the stack's emptiness and fullness, and corresponding messages are printed based on the results.
Program Output: Conclusion:In conclusion, stack operations in data structures are essential for managing data in a Last-In-First-Out (LIFO) manner.A stack is a type of linear data structure that operates on the idea that elements can only be added to or taken away from the top end. The stack operations mainly include push, pop, peek, and isEmpty. The push function is used to raise an element to the top of the stack.The stack is enlarged by one and the new element is positioned at the top. On the other hand, the pop operation takes the element out of the stack's top position. One is removed from the stack, and the deleted element is then returned. We can inspect the top element of the stack without deleting it by using the peek function. It offers a technique to get to the top element without changing the contents of the stack. The isEmpty operation determines whether the stack is empty or not and indicates the presence of any components. Stacks are widely used in various computer science applications and algorithms. They are commonly employed in programming languages to implement function calls, expression evaluation, and manage recursion. Stacks are also useful in solving problems like backtracking, depth-first search, and parsing.
|