class Node:
def __init__(self, data=None, next=None):
self.data = data
self.next = next
class LinkedList:
def __init__(self):
self.head = None
def print(self):
if self.head is None:
print("Linked is empty")
return
# itr = current node, itr.next = next node of current node
itr = self.head
llstr = ''
while itr is not None:
llstr += str(itr.data) + '---->'
itr = itr.next
print(llstr)
def insert_at_begining(self, data):
node = Node(data, self.head)
self.head = node
def insert_at_end(self, data):
if self.head is None:
self.head = Node(data, None)
return
itr = self.head
while itr.next:
itr = itr.next
itr.next = Node(data, None)
def insert_values(self, data_list):
self.head = None
for data in data_list:
self.insert_at_end(data)
def count_nodes(self):
counter = 0
itr = self.head
while itr:
counter += 1
itr = itr.next
return counter
def remove_node(self, index):
if index < 0 or index > self.count_nodes():
raise Exception(" Out of bound index")
if index == 0:
self.head = self.head.next
return
counter = 0
itr = self.head
while itr:
if counter == index - 1:
itr.next = itr.next.next
break
itr = itr.next
counter += 1
def insert_value_at(self, index, data):
if index < 0 or index > self.count_nodes():
raise Exception(" Out of bound index")
if index == 0:
self.insert_at_begining(data)
return
count = 0
itr = self.head
while itr:
if count == index - 1:
node = Node(data, itr.next)
itr.next = node
break
itr = itr.next
count += 1
if __name__ == "__main__":
llist = LinkedList()
llist.insert_values(["vadapav", "samosa", "chutney", "dosa", "chowmin"])
llist.print()
llist.count_nodes()
print("Total nodes = ", llist.count_nodes())
class Node:
def __init__(self, data=None, next=None, prev=None):
self.data = data
self.next = next
self.prev = prev
class DoublyLinkedList:
def __init__(self):
self.head = None
def print_forward(self):
if self.head is None:
print("Linked list is empty")
return
itr = self.head
llstr = ''
while itr:
llstr += str(itr.data)+' --> ' if itr.next else str(itr.data)
itr = itr.next
print(llstr)
def print_backward(self):
if self.head is None:
print("Linked list is empty")
return
last_node = self.get_last_node()
itr = last_node
llstr = ''
while itr:
llstr += itr.data + '-->'
itr = itr.prev
print("Link list in reverse: ", llstr)
def get_last_node(self):
itr = self.head
while itr.next:
itr = itr.next
return itr
def get_length(self):
count = 0
itr = self.head
while itr:
count += 1
itr = itr.next
return count
def insert_at_begining(self, data):
node = Node(data, self.head)
self.head = node
def insert_at_end(self, data):
if self.head is None:
self.head = Node(data, None)
return
itr = self.head
while itr.next:
itr = itr.next
itr.next = Node(data, None)
def insert_at(self, index, data):
if index < 0 or index > self.get_length():
raise Exception("Invalid Index")
if index == 0:
self.insert_at_begining(data)
return
count = 0
itr = self.head
while itr:
if count == index - 1:
node = Node(data, itr.next)
itr.next = node
break
itr = itr.next
count += 1
def remove_at(self, index):
if index < 0 or index >= self.get_length():
raise Exception("Invalid Index")
if index == 0:
self.head = self.head.next
return
count = 0
itr = self.head
while itr:
if count == index - 1:
itr.next = itr.next.next
break
itr = itr.next
count += 1
def insert_values(self, data_list):
self.head = None
for data in data_list:
self.insert_at_end(data)
def insert_after_value(self, data_after, data_to_insert):
# Search for first occurance of data_after value in linked list
# Now insert data_to_insert after data_after node
itr = self.head
while itr:
if data_after == itr.data:
node = Node(data_to_insert, itr.next)
itr.next = node
break
itr = itr.next
def remove_by_value(self, data):
if self.head is None:
return
if self.head.data == data:
self.head = self.head.next
return
itr = self.head
while itr.next:
if itr.next.data == data:
itr.next = itr.next.next
break
itr = itr.next
if __name__ == '__main__':
ll = DoublyLinkedList()
ll.insert_values(["banana", "mango", "grapes", "orange"])
ll.get_last_node()
ll.print_backward()
class Element(object):
def __init__(self, value):
self.value = value
self.next = None
class LinkedList(object):
def __init__(self, head=None):
self.head = head
def append(self, new_element):
current = self.head
if self.head:
while current.next:
current = current.next
current.next = new_element
else:
self.head = new_element
def insert_first(self, new_element):
"Insert new element as the head of the LinkedList"
new_element.next = self.head
self.head = new_element
def delete_first(self):
"Delete the first (head) element in the LinkedList as return it"
if self.head:
deleted_element = self.head
temp = self.head.next
self.head = temp
return deleted_element
else:
return None
class Stack(object):
def __init__(self, top=None):
self.ll = LinkedList(top)
def push(self, new_element):
"Push (add) a new element onto the top of the stack"
self.ll.insert_first(new_element)
def pop(self):
"Pop (remove) the first element off the top of the stack and return it"
return self.ll.delete_first()
# Test cases
# Set up some Elements
e1 = Element(1)
e2 = Element(2)
e3 = Element(3)
e4 = Element(4)
# Start setting up a Stack
stack = Stack(e1)
# Test stack functionality
stack.push(e2)
stack.push(e3)
print(stack.pop().value)
print(stack.pop().value)
print(stack.pop().value)
print(stack.pop())
stack.push(e4)
print(stack.pop().value)
from collections import deque
class Queue:
def __init__(self, head=None):
self.storage = [head]
def enqueue(self, new_element):
self.storage.append(new_element)
def peek(self):
return self.storage[0]
def dequeue(self):
return self.storage.pop(0)
# Setup
q = Queue(1)
q.enqueue(2)
q.enqueue(3)
# Test peek
# Should be 1
print(q.peek())
# Test dequeue
# Should be 1
print(q.dequeue())
# Test enqueue
q.enqueue(4)
# Should be 2
print(q.dequeue())
# Should be 3
print(q.dequeue())
# Should be 4
print(q.dequeue())
q.enqueue(5)
# Should be 5
print(q.peek())