Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@DiegoGallegos4
Created April 15, 2019 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DiegoGallegos4/f57f8818128badaca76fa904984efbd9 to your computer and use it in GitHub Desktop.
Save DiegoGallegos4/f57f8818128badaca76fa904984efbd9 to your computer and use it in GitHub Desktop.
Singly-Linked List
class Node:
def __init__(self, key, next):
self.key = key
self.next = next
class List:
def __init__(self, head=None):
self.head = head
self.tail = None
def push_front(self, key):
self.head = Node(key, self.head)
if self.tail == None:
self.tail = self.head
def top_front(self):
return self.head
def pop_front(self):
if head == None:
raise KeyError
self.head = self.head.next
if self.head == None:
self.tail == None
def push_back(self, key):
new_node = Node(key, None)
if self.tail == None:
self.tail = new_node
self.head = self.tail
else:
self.tail.next = new_node
self.tail = new_node
def pop_back(self):
if self.head == None:
raise KeyError
if self.head == self.tail:
self.head = None
self.tail = self.head
else:
p = self.head
while p.next.next != None:
p = p.next
p.next = None
self.tail = p
def find(self, key):
pass
def erase(self, key):
pass
def empty(self)
pass
def add_before(self, node, key):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment