{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Implementation of Stack\n", "\n", "## Stack Attributes and Methods\n", "\n", "Before we implement our own Stack class, let's review the properties and methods of a Stack.\n", "\n", "The stack abstract data type is defined by the following structure and operations. A stack is structured, as described above, as an ordered collection of items where items are added to and removed from the end called the “top.” Stacks are ordered LIFO. The stack operations are given below.\n", "\n", "* Stack() creates a new stack that is empty. It needs no parameters and returns an empty stack.\n", "* push(item) adds a new item to the top of the stack. It needs the item and returns nothing.\n", "* pop() removes the top item from the stack. It needs no parameters and returns the item. The stack is modified.\n", "* peek() returns the top item from the stack but does not remove it. It needs no parameters. The stack is not modified.\n", "* isEmpty() tests to see whether the stack is empty. It needs no parameters and returns a boolean value.\n", "* size() returns the number of items on the stack. It needs no parameters and returns an integer." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "____\n", "\n", "## Stack Implementation" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "class Stack:\n", " \n", " \n", " def __init__(self):\n", " self.items = []\n", "\n", " def isEmpty(self):\n", " return self.items == []\n", "\n", " def push(self, item):\n", " self.items.append(item)\n", "\n", " def pop(self):\n", " return self.items.pop()\n", "\n", " def peek(self):\n", " return self.items[len(self.items)-1]\n", "\n", " def size(self):\n", " return len(self.items)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's try it out!" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "s = Stack()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "print s.isEmpty()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "s.push(1)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "s.push('two')" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'two'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.peek()" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "s.push(True)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.size()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.isEmpty()" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.pop()" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "two\n" ] } ], "source": [ "s.pop()" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.size()" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.pop()" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.isEmpty()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Good Job!" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" } }, "nbformat": 4, "nbformat_minor": 0 }