Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added data structure #134

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions algorithms/data-structures/linked-lists/doubly linked list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
class Node:
def __init__(self, value):
self.value = value
self.next = None
self.prev = None

class DoublyLinkedList:
def __init__(self, object):
self.tail = self.head = Node(object)

def append(self, object):
c = self.tail
self.tail.next = Node(object)
self.tail = self.tail.next
self.tail.prev = c

def insert(self, index, object):
new_node = Node(object)
if index == 0:
ll = new_node
self.head.prev = ll
ll.next = self.head
self.head = ll
else:
current_indx = 0
ll = self.head
while current_indx < index - 1:
ll = ll.next
current_indx += 1
ll.next.prev = new_node
new_node.next = ll.next
ll.next = new_node
new_node.prev = ll

def pop(self):
curr = self.head
while curr.next.next != None:
curr = curr.next
self.tail = curr
self.tail.next = None

def show(self, reverse = False):
if reverse:
curr = self.tail
while curr:
print(curr.value)
curr = curr.prev
else:
curr = self.head
while curr:
print(curr.value)
curr = curr.next
43 changes: 43 additions & 0 deletions algorithms/data-structures/queue/queue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Node():
def __init__(self, data):
self.data = data
self.next = None
self.prev = None

class Queue():
def __init__(self):
self.tail = self.head = None
self.length = 0

def insert(self, object):
if self.length == 0:
self.tail = self.head = Node(object)
else:
c = Node(object)
self.head.prev = c
c.next = self.head
self.head = c
self.length += 1

def pop(self):
if self.length == 1:
value = self.tail.data
self.tail = self.head = None
else:
c = self.tail.prev
value = self.tail.data
c.next = None
self.tail = c
self.length -= 1
return value

def isEmpty(self):
return not bool(self.length)

def show(self):
curr = self.head
lst = []
while curr:
lst.append(curr.data)
curr = curr.next
return lst