From aa919129f2835cd8a76d28afc43472c1b85f717a Mon Sep 17 00:00:00 2001 From: temitayo Date: Fri, 26 Mar 2021 07:07:29 -0700 Subject: [PATCH] Add Python linkedlist (#119) * fixed incorrect links * added singly list Data structure * removed semi-column * added doubly linked list * added doubly linked list * docs: update linked list index add python double linked list in the linked list inded Co-authored-by: Arsenic <54987647+Arsenic-ATG@users.noreply.github.com> --- linked-lists/Python/doubly.py | 34 ++++++++++++++++++++++++++++++++++ linked-lists/README.md | 1 + 2 files changed, 35 insertions(+) create mode 100644 linked-lists/Python/doubly.py diff --git a/linked-lists/Python/doubly.py b/linked-lists/Python/doubly.py new file mode 100644 index 00000000..7310e36d --- /dev/null +++ b/linked-lists/Python/doubly.py @@ -0,0 +1,34 @@ +# A simple Python program to create a linked list + +# Node class +class Node: + def __init__(self, data): + self.data = data + self.next = None + self.prev = None + +class doubly_linked_list: + + def __init__(self): + self.head = None + +# Adding data elements + def push(self, NewVal): + NewNode = Node(NewVal) + NewNode.next = self.head + if self.head is not None: + self.head.prev = NewNode + self.head = NewNode + +# Print the Doubly Linked list + def listprint(self, node): + while (node is not None): + print(node.data), + last = node + node = node.next + +dllist = doubly_linked_list() +dllist.push(12) +dllist.push(8) +dllist.push(62) +dllist.listprint(dllist.head) \ No newline at end of file diff --git a/linked-lists/README.md b/linked-lists/README.md index 1a774658..a387ddde 100644 --- a/linked-lists/README.md +++ b/linked-lists/README.md @@ -19,3 +19,4 @@ ### Python 1. [Singly Linked List](Python/singly.py) +2. [Doubly Linked List](Python/doubly.py)