{
 "metadata": {
  "name": "",
  "signature": "sha256:6b5cf549b3fdac88be0a759d2fc29d02814154da669e9ab30e73ad0c8e2bbf9e"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Chapter 19: Program Design"
     ]
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example stack1.c, Page 488. Other files- stack.h\n"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import sys\n",
      "STACK_SIZE=100\n",
      "contents=[None]*STACK_SIZE\n",
      "top=0\n",
      "def terminate(message):\n",
      "    print message\n",
      "    sys.exit()\n",
      "def make_empty():\n",
      "    top=0\n",
      "def is_empty():\n",
      "    if(top==0):\n",
      "        return True\n",
      "    else:\n",
      "        return False\n",
      "def is_full():\n",
      "    if(top==STACK_SIZE):\n",
      "        return True\n",
      "    else:\n",
      "        return False\n",
      "def push(i):\n",
      "    if(is_full()):\n",
      "        terminate(\"Error in push: stack is full.\")\n",
      "    contents[top+1]=i\n",
      "def pop():\n",
      "    if(is_empty()):\n",
      "        terminate(\"Error in push: stack is empty.\")\n",
      "    return contents[top-1]\n",
      "    "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example stack2.c, Page 489"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import sys\n",
      "STACK_SIZE=100\n",
      "contents=[None]*STACK_SIZE\n",
      "top=0\n",
      "def terminate(message):\n",
      "    print message\n",
      "    sys.exit()\n",
      "def make_empty():\n",
      "    top=0\n",
      "def is_empty():\n",
      "    if(top==0):\n",
      "        return True\n",
      "    else:\n",
      "        return False\n",
      "def is_full():\n",
      "    if(top==STACK_SIZE):\n",
      "        return True\n",
      "    else:\n",
      "        return False\n",
      "def push(i):\n",
      "    if(is_full()):\n",
      "        terminate(\"Error in push: stack is full.\")\n",
      "    contents[top+1]=i\n",
      "def pop():\n",
      "    if(is_empty()):\n",
      "        terminate(\"Error in push: stack is empty.\")\n",
      "    return contents[top-1]\n",
      "    "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 2
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example stackclient.c, Page 494"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "class Stack:\n",
      "     def __init__(self):\n",
      "         self.items = []\n",
      "     def is_empty(self):\n",
      "         return self.items == []\n",
      "     def push(self, item):\n",
      "         self.items.append(item)\n",
      "     def pop(self):\n",
      "         return self.items.pop()\n",
      "     def size(self):\n",
      "         return len(self.items)\n",
      "     def make_empty(self):\n",
      "         while len(self.items) > 0 : self.items.pop()\n",
      "s1=Stack()\n",
      "s2=Stack()\n",
      "s1.push(1)\n",
      "s1.push(2)\n",
      "n=s1.pop()\n",
      "print \"Popped %d from s1\"%n\n",
      "s2.push(n)\n",
      "n=s1.pop()\n",
      "print \"Popped %d from s1\"%n\n",
      "s2.push(n)\n",
      "while(s2.is_empty()!=True):\n",
      "    print \"Popped %d from s2\"%s2.pop()\n",
      "s2.push(3)\n",
      "s2.make_empty()\n",
      "if(s2.is_empty()):\n",
      "    print \"s2 is empty\"\n",
      "else:\n",
      "    print \"s2 is not empty\"\n",
      "    "
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "Popped 2 from s1\n",
        "Popped 1 from s1\n",
        "Popped 1 from s2\n",
        "Popped 2 from s2\n",
        "s2 is empty\n"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example stackADT.c, Page 495"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import sys\n",
      "STACK_SIZE=100\n",
      "contents=[None]*STACK_SIZE\n",
      "top=0\n",
      "def terminate(message):\n",
      "    print message\n",
      "    sys.exit()\n",
      "def make_empty():\n",
      "    top=0\n",
      "def is_empty():\n",
      "    if(top==0):\n",
      "        return True\n",
      "    else:\n",
      "        return False\n",
      "def is_full():\n",
      "    if(top==STACK_SIZE):\n",
      "        return True\n",
      "    else:\n",
      "        return False\n",
      "def push(i):\n",
      "    if(is_full()):\n",
      "        terminate(\"Error in push: stack is full.\")\n",
      "    contents[top+1]=i\n",
      "def pop():\n",
      "    if(is_empty()):\n",
      "        terminate(\"Error in push: stack is empty.\")\n",
      "    return contents[top-1]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 7
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example stackADT2.c, Page 498"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#similar as above since Python doesn't have pointers\n",
      "import sys\n",
      "STACK_SIZE=100\n",
      "contents=[None]*STACK_SIZE\n",
      "top=0\n",
      "def terminate(message):\n",
      "    print message\n",
      "    sys.exit()\n",
      "def make_empty():\n",
      "    top=0\n",
      "def is_empty():\n",
      "    if(top==0):\n",
      "        return True\n",
      "    else:\n",
      "        return False\n",
      "def is_full():\n",
      "    if(top==STACK_SIZE):\n",
      "        return True\n",
      "    else:\n",
      "        return False\n",
      "def push(i):\n",
      "    if(is_full()):\n",
      "        terminate(\"Error in push: stack is full.\")\n",
      "    contents[top+1]=i\n",
      "def pop():\n",
      "    if(is_empty()):\n",
      "        terminate(\"Error in push: stack is empty.\")\n",
      "    return contents[top-1]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 8
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example stackADT3.c, Page 500"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#similar as above since Python doesn't have pointers\n",
      "import sys\n",
      "STACK_SIZE=100\n",
      "contents=[None]*STACK_SIZE\n",
      "top=0\n",
      "def terminate(message):\n",
      "    print message\n",
      "    sys.exit()\n",
      "def make_empty():\n",
      "    top=0\n",
      "def is_empty():\n",
      "    if(top==0):\n",
      "        return True\n",
      "    else:\n",
      "        return False\n",
      "def is_full():\n",
      "    if(top==STACK_SIZE):\n",
      "        return True\n",
      "    else:\n",
      "        return False\n",
      "def push(i):\n",
      "    if(is_full()):\n",
      "        terminate(\"Error in push: stack is full.\")\n",
      "    contents[top+1]=i\n",
      "def pop():\n",
      "    if(is_empty()):\n",
      "        terminate(\"Error in push: stack is empty.\")\n",
      "    return contents[top-1]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 9
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}