UNDO/REDO: It can also be used for performing UNDO/REDO operations.For example, we want to reverse a " javaTpoint" string, so we can achieve this with the help of a stack.įirst, we push all the characters of the string in a stack until we reach the null character.Īfter pushing all the characters, we start taking out the character one by one until we reach the bottom of the stack. String reversal: Stack is also used for reversing a string.If any symbol is left in the stack, it means that some syntax occurs in a program. Therefore, the net value comes out to be zero. For example, we have the following program:Īs we know, each program has an opening and closing braces when the opening braces come, we push the braces in a stack, and when the closing braces appear, we pop the opening braces from the stack. Balancing of symbols: Stack is used for balancing a symbol. The following are the applications of the stack: Once the pop operation is performed, the top is decremented by 1, i.e., top=top-1.If the stack is not empty, we first access the element which is pointed by the top.If we try to delete the element from the empty stack, then the underflow condition occurs.Before deleting the element from the stack, we check whether the stack is empty.The steps involved in the POP operation is given below: The elements will be inserted until we reach the max size of the stack.When the new element is pushed in a stack, first, the value of the top gets incremented, i.e., top=top+1, and the element will be placed at the new position of the top.When we initialize a stack, we set the value of top as -1 to check that the stack is empty.If we try to insert the element in a stack, and the stack is full, then the overflow condition occurs.Before inserting an element in a stack, we check whether the stack is full.The steps involved in the PUSH operation is given below: display(): It prints all the elements available in the stack.change(): It changes the element at the given position.count(): It returns the total number of elements available in a stack.peek(): It returns the element at the given position.isFull(): It determines whether the stack is full or not.'. isEmpty(): It determines whether the stack is empty or not.If the stack is empty means that no element exists in the stack, this state is known as an underflow state. pop(): When we delete an element from the stack, the operation is known as a pop.If the stack is full then the overflow condition occurs. push(): When we insert an element in a stack then the operation is known as a push.The following are some common operations implemented on the stack: In the above case, the value 5 is entered first, so it will be removed only after the deletion of all the other elements. It follows the LIFO pattern, which means that the value entered first will be removed last. When we perform the delete operation on the stack, there is only one way for entry and exit as the other end is closed. The stack gets filled up from the bottom to the top. In the above cases, we can observe that it goes from the top to the bottom when we were entering the new element in the stack. Since our stack is full as the size of the stack is 5. We have taken the stack of size 5 as shown below in which we are pushing the elements one by one until the stack becomes full. Suppose we want to store the elements in a stack and let's assume that stack is empty. As we can observe in the below figure there are five memory blocks in the stack therefore, the size of the stack is 5.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |