{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Implementation of Deque\n", "\n", "In this lecture we will implement our own Deque class!\n", "\n", "## Methods and Attributes\n", "\n", "* Deque() creates a new deque that is empty. It needs no parameters and returns an empty deque.\n", "* addFront(item) adds a new item to the front of the deque. It needs the item and returns nothing.\n", "* addRear(item) adds a new item to the rear of the deque. It needs the item and returns nothing.\n", "* removeFront() removes the front item from the deque. It needs no parameters and returns the item. The deque is modified.\n", "* removeRear() removes the rear item from the deque. It needs no parameters and returns the item. The deque is modified.\n", "* isEmpty() tests to see whether the deque is empty. It needs no parameters and returns a boolean value.\n", "* size() returns the number of items in the deque. It needs no parameters and returns an integer.\n", "\n", "## Deque Implementation" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class Deque:\n", " def __init__(self):\n", " self.items = []\n", "\n", " def isEmpty(self):\n", " return self.items == []\n", "\n", " def addFront(self, item):\n", " self.items.append(item)\n", "\n", " def addRear(self, item):\n", " self.items.insert(0,item)\n", "\n", " def removeFront(self):\n", " return self.items.pop()\n", "\n", " def removeRear(self):\n", " return self.items.pop(0)\n", "\n", " def size(self):\n", " return len(self.items)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": true }, "outputs": [], "source": [ "d = Deque()" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": true }, "outputs": [], "source": [ "d.addFront('hello')" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ "d.addRear('world')" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d.size()" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello world\n" ] } ], "source": [ "print d.removeFront() + ' ' + d.removeRear()" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d.size()" ] }, { "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 }