Skip to content

Commit

Permalink
DAY 1
Browse files Browse the repository at this point in the history
  • Loading branch information
terodea committed Sep 17, 2020
1 parent aef51b6 commit 5722636
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 695 deletions.
106 changes: 106 additions & 0 deletions DataStructures/02-Python/01-LinkedLists/01-single_linked_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
class Node:
"""
A base class for single linked list.
"""
def __init__(self, info, link=None):
self.info = info
self.link = link


class SingleLinkedList:
"""
An implementation of single linked list.
1. Insert
2. Delete
3. Traverse
"""
def __init__(self):
self.head= None

def insert_at_begin(self, info):
"""
Method to insert Node into a linked list at first.
"""
new_node = Node(info)
if self.head != None:
new_node.link =self.head
self.head = new_node
else:
self.head = new_node

def insert_at_end(self, info):
"""
Method to inser Node into a linked list at end.
"""
new_node = Node(info)
if self.head != None:
current = self.head
while current.link != None:
current = current.link
current.link = new_node
else:
self.head = new_node

def delete_node(self, ele):
"""
---------->
2 -> 5 -> 10
"""
# CASE 1: When List is empty
if self.head == None:
print("List is empty")
return

# CASE 2: When List is First Element
if self.head.info == ele:
temp = self.head
self.head = temp.link
temp = None
return
# CASE 3: When element is at last and in between
current = self.head
while current.link != None:
if current.link.info == ele:
temp = current.link
current.link = temp.link
temp = None
current = current.link
print("DELETION NOT POSSIBLE")

def display(self):
# CASE 1: List is empty
if self.head == None:
print("List is empty.")
return
current = self.head
while current != None:
print(current.info)
current = current.link

def search(self, ele):
if self.head == None:
print("List is empty.")
return

current = self.head
while current != None:
if current.info == ele:
print("Eelement is found in list)
return
current = current.link
print("LIST NOT FOUND")


if __name__ == "__main__":
LL = LinkedList()
LL.insert_at_begining(10)
LL.insert_at_begining(5)
LL.display()
print("*******")
LL.insert_at_end(20)
LL.insert_at_end(30)
LL.display()
LL.delete_node(40)
LL.delete_node(20)
LL.display()
LL.search(30)
21 changes: 0 additions & 21 deletions DataStructures/02-Python/01-Stack/Stack.py

This file was deleted.

46 changes: 0 additions & 46 deletions DataStructures/02-Python/01-Stack/StackUsingArray.py

This file was deleted.

57 changes: 0 additions & 57 deletions DataStructures/02-Python/01-Stack/StackUsingDynamicArray.py

This file was deleted.

71 changes: 0 additions & 71 deletions DataStructures/02-Python/01-Stack/StackUsingLL.py

This file was deleted.

23 changes: 0 additions & 23 deletions DataStructures/02-Python/01-Stack/p01.py

This file was deleted.

40 changes: 0 additions & 40 deletions DataStructures/02-Python/02-QUEUE/Queue.py

This file was deleted.

Loading

0 comments on commit 5722636

Please sign in to comment.