Linked List, Queue, and Stack Implementations in Python

Graham's Code

Screen Shot 2013-05-23 at 3.37.05 PM
# Programmer: Graham Nedelka
# Linked List, Queue, and Stack Implementations

import random

class Node(object):

def __init__(self, other = None):
self.other = other
self.next = None
self.previous = None

def __str__(self):
return str(self.other)

class List(object):
def __init__(self):
self.head = None
self.tail = None

def append(self, value):

newNode = Node(value)
if self.head == None:
self.head = newNode
self.tail = newNode

else:
self.tail.next = newNode
newNode.previous = self.tail
self.tail = newNode

def __str__(self):
someString = “”
current = self.head
while current != None:
someString = someString + current.other
current = current.next
return someString

class Queue(List):
def enqueue(self, value):
self.append(value)

def dequeue(self):
self.temp = self.head
self.head = self.head.next
return self.temp

class Stack(List):
def push(self, value):
self.append(value)

def pop(self):
self.temp = self.tail
self.tail = self.tail.previous
return self.temp

def main():
list1 = List()
for i in xrange(0, 100):
list1.append(str(random.randrange(0,100)))
print list1
queue = Queue()
for i in xrange(0,100):
queue.enqueue(str(random.randrange(0,100)))
for i in xrange(0,100):
print queue.dequeue()

View original post 26 more words

Advertisements
Standard

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s