|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "#### Author: OMKAR PATHAK" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "## What is Stack?" |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "markdown", |
| 19 | + "metadata": {}, |
| 20 | + "source": [ |
| 21 | + "* A stack is a simple data structure used for storing data (similar to Linked Lists). In a stack, the order in which the data arrives is important.\n", |
| 22 | + "* A stack is an ordered list in which insertion and deletion are done at one end, called top. The last element inserted is the first one to be deleted. Hence, it is called the Last in First out (LIFO) or First in Last out(FILO) list.\n" |
| 23 | + ] |
| 24 | + }, |
| 25 | + { |
| 26 | + "cell_type": "markdown", |
| 27 | + "metadata": {}, |
| 28 | + "source": [ |
| 29 | + "### Applications of Stack:" |
| 30 | + ] |
| 31 | + }, |
| 32 | + { |
| 33 | + "cell_type": "markdown", |
| 34 | + "metadata": {}, |
| 35 | + "source": [ |
| 36 | + "* Balancing of symbols\n", |
| 37 | + "* lnfix-to-postfix conversion\n", |
| 38 | + "* Evaluation of postfix expression\n", |
| 39 | + "* Implementing function calls (including recursion)\n", |
| 40 | + "* Page-visited history in a Web browser [Back Buttons]\n", |
| 41 | + "* Undo sequence in a text editor\n", |
| 42 | + "* Matching Tags in HTML and XML\n", |
| 43 | + "* Used in many algorithms like Tower of Hanoi, tree traversals, stock span problem, histogram problem." |
| 44 | + ] |
| 45 | + }, |
| 46 | + { |
| 47 | + "cell_type": "markdown", |
| 48 | + "metadata": {}, |
| 49 | + "source": [ |
| 50 | + "### Implementing Stack using Python Lists:" |
| 51 | + ] |
| 52 | + }, |
| 53 | + { |
| 54 | + "cell_type": "code", |
| 55 | + "execution_count": 13, |
| 56 | + "metadata": {}, |
| 57 | + "outputs": [ |
| 58 | + { |
| 59 | + "name": "stdout", |
| 60 | + "output_type": "stream", |
| 61 | + "text": [ |
| 62 | + "0 1 2 3 4 5 6 7 8 9\n", |
| 63 | + "0 1 2 3 4 5 6 7 8\n" |
| 64 | + ] |
| 65 | + }, |
| 66 | + { |
| 67 | + "data": { |
| 68 | + "text/plain": [ |
| 69 | + "9" |
| 70 | + ] |
| 71 | + }, |
| 72 | + "execution_count": 13, |
| 73 | + "metadata": {}, |
| 74 | + "output_type": "execute_result" |
| 75 | + } |
| 76 | + ], |
| 77 | + "source": [ |
| 78 | + "class Stack(object):\n", |
| 79 | + " def __init__(self, limit = 10):\n", |
| 80 | + " self.stack = []\n", |
| 81 | + " self.limit = limit\n", |
| 82 | + " \n", |
| 83 | + " # for printing the stack contents\n", |
| 84 | + " def __str__(self):\n", |
| 85 | + " return ' '.join([str(i) for i in self.stack])\n", |
| 86 | + " \n", |
| 87 | + " # for pushing an element on to the stack\n", |
| 88 | + " def push(self, data):\n", |
| 89 | + " if len(self.stack) >= self.limit:\n", |
| 90 | + " print('Stack Overflow')\n", |
| 91 | + " else:\n", |
| 92 | + " self.stack.append(data)\n", |
| 93 | + " \n", |
| 94 | + " # for popping the uppermost element\n", |
| 95 | + " def pop(self):\n", |
| 96 | + " if len(self.stack) <= 0:\n", |
| 97 | + " print('Stack Underflow')\n", |
| 98 | + " else:\n", |
| 99 | + " self.stack.pop()\n", |
| 100 | + " \n", |
| 101 | + " # for peeking the top-most element of the stack\n", |
| 102 | + " def peek(self):\n", |
| 103 | + " if len(self.stack) <= 0:\n", |
| 104 | + " print('Stack Underflow')\n", |
| 105 | + " else:\n", |
| 106 | + " return self.stack[-1]\n", |
| 107 | + " \n", |
| 108 | + " # to check if stack is empty\n", |
| 109 | + " def isEmpty(self):\n", |
| 110 | + " return len(self.stack) == []\n", |
| 111 | + " \n", |
| 112 | + " # for checking the size of stack\n", |
| 113 | + " def size(self):\n", |
| 114 | + " return len(self.stack)\n", |
| 115 | + "\n", |
| 116 | + "myStack = Stack()\n", |
| 117 | + "for i in range(10):\n", |
| 118 | + " myStack.push(i)\n", |
| 119 | + "print(myStack)\n", |
| 120 | + "myStack.pop() # popping the top element\n", |
| 121 | + "print(myStack)\n", |
| 122 | + "myStack.peek() # printing the top element\n", |
| 123 | + "myStack.isEmpty()\n", |
| 124 | + "myStack.size()" |
| 125 | + ] |
| 126 | + }, |
| 127 | + { |
| 128 | + "cell_type": "markdown", |
| 129 | + "metadata": {}, |
| 130 | + "source": [ |
| 131 | + "### Time Complexities:" |
| 132 | + ] |
| 133 | + }, |
| 134 | + { |
| 135 | + "cell_type": "markdown", |
| 136 | + "metadata": {}, |
| 137 | + "source": [ |
| 138 | + "* Push: O(1)\n", |
| 139 | + "* Pop: O(1)\n", |
| 140 | + "* Peek: O(1)\n", |
| 141 | + "* isEmpty: O(1)\n", |
| 142 | + "* Size: O(1)" |
| 143 | + ] |
| 144 | + }, |
| 145 | + { |
| 146 | + "cell_type": "markdown", |
| 147 | + "metadata": {}, |
| 148 | + "source": [ |
| 149 | + "### Limitations:" |
| 150 | + ] |
| 151 | + }, |
| 152 | + { |
| 153 | + "cell_type": "markdown", |
| 154 | + "metadata": {}, |
| 155 | + "source": [ |
| 156 | + "* Stack size is to be defined first and cannot be changed.\n", |
| 157 | + "* Trying to push a new element into a full stack causes an implementation-specific exception.\n" |
| 158 | + ] |
| 159 | + } |
| 160 | + ], |
| 161 | + "metadata": { |
| 162 | + "kernelspec": { |
| 163 | + "display_name": "Python 3", |
| 164 | + "language": "python", |
| 165 | + "name": "python3" |
| 166 | + }, |
| 167 | + "language_info": { |
| 168 | + "codemirror_mode": { |
| 169 | + "name": "ipython", |
| 170 | + "version": 3 |
| 171 | + }, |
| 172 | + "file_extension": ".py", |
| 173 | + "mimetype": "text/x-python", |
| 174 | + "name": "python", |
| 175 | + "nbconvert_exporter": "python", |
| 176 | + "pygments_lexer": "ipython3", |
| 177 | + "version": "3.5.2" |
| 178 | + } |
| 179 | + }, |
| 180 | + "nbformat": 4, |
| 181 | + "nbformat_minor": 2 |
| 182 | +} |
0 commit comments