Deck 1 Flashcards
Linear Structures
Data structure that is thought of having two ends, left and right or sometimes front and rear. Top and bottom. Distinguished by how data is added and removed.
stack
push-down stack. top to base linear data structure. LIFO. For example, every web browser has a Back button. As you navigate from web page to web page, those pages are placed on a stack (actually it is the URLs that are going on the stack).
Stack Code
class Stack: def \_\_init\_\_(self): self.stack = []
def push(self, value): return self.stack.append(value)
def pop(self): if self.is_empty(): return None else: return self.stack.pop()
def peak(self): if self.is_empty(): return None else: return self.stack[-1]
def size(self): return len(self.stack)
def is_empty(self): return self.size() == 0
Queue Code
class Queue: def \_\_init\_\_(self): self.queue = []
def enqueue(self, val): # critical differnce between stack and queue self.queue.insert(0,val)
def dequeue(self): if self.is_empty(): return None else: return self.queue.pop()
def size(self): return len(self.dequeue)
def is_empty(self): return self.size() == 0
Binary Search Code
class BinarySearch(alist, val): if len(alist) == 0: return False else: midpoint = len(alist)//2 if val == alist[midpoint]: return True else: if val < alist[midpoint]: return BinarySearch(alist[:midpoint], val) else: return BinarySearch(alist[midpoint+1:], val)
Queue Data Structure
FIFO. front to rear. basically every line you can think of. Common function are insert(queue) or pop(dequeue)
Hash Table Get Function
def hashFunction(self, key, size): return key%size
def rehash(self, oldhash, size): return (oldhash+1)%size
def get(self, key):
startingslot = self.hashFunction(key, len(self.slots))
data = None
stop = False
found = False
position = startingslot
while self.slot[position] != None and not stop and not found:
if self.slot[position] == key:
found = True
data = self.data[position]
else:
position = self.rehash(position,len(self.slots))
if position == startslot:
stop = True
return data
Hash Table put function
def hashFunction(self, key, size): return key%size
def rehash(self, oldhash, size): return (oldhash+1)%size
def put(self,key,data): hashvalue = self.hashfunction(key,len(self.slots))
if self.slots[hashvalue] == None: self.slots[hashvalue] = key self.data[hashvalue] = data else: if self.slots[hashvalue] == key: self.data[hashvalue] = data #replace else: nextslot = self.rehash(hashvalue,len(self.slots)) while self.slots[nextslot] != None and \ self.slots[nextslot] != key: nextslot = self.rehash(nextslot,len(self.slots)) if self.slots[nextslot] == None: self.slots[nextslot]=key self.data[nextslot]=data else: self.data[nextslot] = data #replace
Deque
Deques are data structures that allow hybrid behavior like that of stacks and queues. Double-ended queue order collection. allow for LIFO and FIFO mixture of stack and queue
adding an removing from the front is O(1) whereas adding or removing from the end is O(n)
Deque code
class Deque: def \_\_init\_\_(self): self.items = []
def isEmpty(self): return self.items == []
def addFront(self, item): self.items.append(item)
def addRear(self, item): self.items.insert(0,item)
def removeFront(self): return self.items.pop()
def removeRear(self): return self.items.pop(0)
def size(self): return len(self.items)
Linked List
Unordered list. The location of the next item is stored in the item so the relative position doesn’t matter. First item is know as the head. Basic building block is a node. Node will contain data and the next node reference. None indicates there is no next node.
Node function names
__init__, getData, getNext, setData, setNext
Node Code
class Node: def \_\_init\_\_(self,initdata): self.data = initdata self.next = None
def getData(self): return self.data
def getNext(self): return self.next
def setData(self,newdata): self.data = newdata
def setNext(self,newnext): self.next = newnext
UnorderedList functions
__int__, isEmpty O(1), add O(n), size O(n), search O(n), remove O(n)
UnorderList Code
class Node: def \_\_init\_\_(self,initdata): self.data = initdata self.next = None
def getData(self): return self.data
def getNext(self): return self.next
def setData(self,newdata): self.data = newdata
def setNext(self,newnext): self.next = newnext
class UnorderedList:
def \_\_init\_\_(self): self.head = None
def isEmpty(self): return self.head == None
def add(self,item): temp = Node(item) temp.setNext(self.head) self.head = temp
def size(self): current = self.head count = 0 while current != None: count = count + 1 current = current.getNext()
return count def search(self,item): current = self.head found = False while current != None and not found: if current.getData() == item: found = True else: current = current.getNext() return found
def remove(self,item): current = self.head previous = None found = False while not found: if current.getData() == item: found = True else: previous = current current = current.getNext()
if previous == None: self.head = current.getNext() else: previous.setNext(current.getNext())
mylist = UnorderedList()