Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Sep 7, 2025. It is now read-only.
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
Added data structure
  • Loading branch information
Vishesh-dd4723 committed Oct 1, 2021
commit 6fbcd9fe4fff6d732f618b78255ad28d281cb40e
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