forked from theja-m/Data-Structures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
644 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
class Graph: | ||
|
||
def __init__(self): | ||
self.numberofnodes = 0 | ||
self.adjacentlist = {} | ||
|
||
def __str__(self): | ||
return str(self.__dict__) | ||
|
||
def addVertex(self,node): | ||
self.adjacentlist[node] = [] | ||
self.numberofnodes += 1 | ||
|
||
def addEdge(self,node1,node2): | ||
self.adjacentlist[node1].append(node2) | ||
self.adjacentlist[node2].append(node1) | ||
|
||
def showconnection(self): | ||
for x in self.adjacentlist: | ||
print(x , end = '-->') | ||
for i in self.adjacentlist[x]: | ||
print(i , end = ' ') | ||
print() | ||
|
||
myGraph = Graph() | ||
myGraph.addVertex('0') | ||
myGraph.addVertex('1') | ||
myGraph.addVertex('2') | ||
myGraph.addVertex('3') | ||
myGraph.addVertex('4') | ||
myGraph.addVertex('5') | ||
myGraph.addVertex('6') | ||
myGraph.addEdge('3', '1') | ||
myGraph.addEdge('3', '4') | ||
myGraph.addEdge('4', '2') | ||
myGraph.addEdge('4', '5') | ||
myGraph.addEdge('1', '2') | ||
myGraph.addEdge('1', '0') | ||
myGraph.addEdge('0', '2') | ||
myGraph.addEdge('6', '5') | ||
print(myGraph) | ||
myGraph.showconnection() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
def func(mylist): | ||
|
||
for i in range(0,len(mylist)): | ||
for j in range(i+1,len(mylist)): | ||
if mylist[i] == mylist[j]: | ||
return mylist[i] | ||
return 0 | ||
|
||
def hashtable(mylist): | ||
mydict = {} | ||
for i in range(0,len(mylist)): | ||
if mylist[i] in mydict: | ||
return mylist[i] | ||
else: | ||
mydict[mylist[i]]=i | ||
return 0 | ||
|
||
|
||
mylist = [2,1,1,2,3,4,5] | ||
x = hashtable(mylist) | ||
print(x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from random import randint | ||
class Hashtable(): | ||
def __init__(self): | ||
self.mydict = ['None']*50 | ||
self.addr_list = [] | ||
|
||
def __str__(self): | ||
return str(self.__dict__) | ||
|
||
def _hash(self): | ||
while True: | ||
x = randint(0,49) | ||
if x not in self.addr_list: | ||
return x | ||
|
||
def set(self,key,value): | ||
address = self._hash() | ||
self.mydict[address] = [key,value] | ||
self.addr_list.append(address) | ||
|
||
def get(self,key): | ||
|
||
for i in self.addr_list: | ||
if self.mydict[i][0] == key: | ||
return self.mydict[i][1] | ||
|
||
def keys(self): | ||
key_arr=[] | ||
for i in self.addr_list: | ||
key_arr.append(self.mydict[i][0]) | ||
return key_arr | ||
|
||
h=Hashtable() | ||
h.set('grapes',1000) | ||
h.set('apples',10) | ||
h.set('oranges',300) | ||
h.set('bananas',200) | ||
x=h.get('grapes') | ||
key_arr = h.keys() | ||
print(h) | ||
print(x) | ||
print(key_arr) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
class Node: | ||
def __init__(self,data): | ||
self.data = data | ||
self.next = None | ||
self.prev = None | ||
|
||
class DoublyLinkedList: | ||
def __init__(self): | ||
self.head = None | ||
self.tail = None | ||
|
||
def append(self,data): | ||
new_node = Node(data) | ||
if self.head == None: | ||
self.head = new_node | ||
self.tail = self.head | ||
self.length = 1 | ||
else: | ||
self.tail.next = new_node | ||
new_node.prev = self.tail | ||
self.tail = new_node | ||
self.length += 1 | ||
|
||
def prepend(self,data): | ||
new_node = Node(data) | ||
new_node.next = self.head | ||
self.head.prev = new_node | ||
self.head = new_node | ||
self.length += 1 | ||
|
||
def insert(self,index,data): | ||
new_node = Node(data) | ||
if index==0: | ||
self.prepend(data) | ||
return | ||
if index >= self.length: | ||
self.append(data) | ||
return | ||
else: | ||
leader = self.traversetoindex(index - 1) | ||
holder = leader.next | ||
leader.next = new_node | ||
new_node.next = holder | ||
new_node.prev = leader | ||
holder.prev = new_node | ||
self.length+=1 | ||
|
||
def remove(self,index): | ||
if index == self.length-1: | ||
self.tail = self.tail.prev | ||
self.tail.next = None | ||
self.length -= 1 | ||
return | ||
leader = self.traversetoindex(index-1) | ||
unwanted_node = leader.next | ||
holder = unwanted_node.next | ||
leader.next = holder | ||
holder.prev = leader | ||
self.length -= 1 | ||
|
||
|
||
def traversetoindex(self,index): | ||
curr_node = self.head | ||
i = 0 | ||
while i!= index: | ||
curr_node = curr_node.next | ||
i+=1 | ||
return curr_node | ||
|
||
def printt(self): | ||
temp = self.head | ||
while temp != None: | ||
print(temp.data , end = ' ') | ||
temp = temp.next | ||
print() | ||
print('Length ' + str(self.length)) | ||
|
||
|
||
d = DoublyLinkedList() | ||
d.append(10) | ||
d.append(5) | ||
d.append(6) | ||
d.prepend(1) | ||
d.insert(2,22) | ||
d.remove(3) | ||
d.printt() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# 15 --> 6 --> 8 | ||
|
||
class Node(): | ||
|
||
def __init__(self,data): | ||
self.data = data | ||
self.next = None | ||
|
||
class LinkedList(): | ||
|
||
def __init__(self): | ||
self.head = None | ||
self.tail = None | ||
|
||
def append(self,data): | ||
new_node = Node(data) | ||
if self.head == None: | ||
self.head = new_node | ||
self.tail = self.head | ||
self.length = 1 | ||
else: | ||
self.tail.next = new_node | ||
self.tail = new_node | ||
self.length += 1 | ||
|
||
def prepend(self,data): | ||
new_node = Node(data) | ||
new_node.next = self.head | ||
self.head = new_node | ||
|
||
|
||
self.length += 1 | ||
|
||
def insert(self,index,data): | ||
new_node = Node(data) | ||
i = 0 | ||
temp = self.head | ||
if index>=self.length: | ||
self.append(data) | ||
return | ||
while i<self.length: | ||
if i == index-1: | ||
temp.next , new_node.next = new_node , temp.next | ||
self.length+=1 | ||
break | ||
temp = temp.next | ||
i+=1 | ||
|
||
|
||
def remove(self,index): | ||
temp = self.head | ||
i=0 | ||
while i<self.length: | ||
if index == 0: | ||
self.head = temp.next | ||
self.length -= 1 | ||
break | ||
if i == self.length-1: | ||
temp.next = None | ||
self.tail = temp | ||
self.length -= 1 | ||
break | ||
if i == index-1: | ||
temp.next = temp.next.next | ||
self.length-=1 | ||
break | ||
i+=1 | ||
temp = temp.next | ||
|
||
def printl(self): | ||
temp = self.head | ||
while temp != None: | ||
print(temp.data , end = ' ') | ||
temp = temp.next | ||
print() | ||
print('Length = '+str(self.length)) | ||
|
||
def reverse(self): | ||
prev = None | ||
self.tail = self.head | ||
while self.head != None: | ||
temp = self.head | ||
self.head = self.head.next | ||
temp.next = prev | ||
prev = temp | ||
self.head = temp | ||
|
||
|
||
l = LinkedList() | ||
l.append(10) | ||
l.append(5) | ||
l.append(6) | ||
l.prepend(1) | ||
l.insert(2,99) | ||
l.insert(34,23) | ||
#l.remove(4) | ||
l.reverse() | ||
l.printl() |
54 changes: 54 additions & 0 deletions
54
Data Structures - Stacks and Queues/Queue using Linked List.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
class Node: | ||
def __init__(self,val): | ||
self.val = val | ||
self.next = None | ||
|
||
class Queue: | ||
def __init__(self): | ||
self.first = None | ||
self.last = None | ||
self.length = 0 | ||
|
||
def peek(self): | ||
return self.first.val | ||
|
||
def enqueue(self,val): | ||
new_node = Node(val) | ||
if self.first == None: | ||
self.first = new_node | ||
self.last = self.first | ||
self.length += 1 | ||
else: | ||
self.last.next = new_node | ||
self.last = new_node | ||
self.length += 1 | ||
|
||
def dequeue(self): | ||
temp = self.first.next | ||
dequeued_element = self.first | ||
if temp == None: | ||
self.first = None | ||
self.length -= 1 | ||
return | ||
self.first.next = None | ||
self.first = temp | ||
self.length -= 1 | ||
|
||
def printt(self): | ||
temp = self.first | ||
while temp != None: | ||
print(temp.val , end = '->') | ||
temp = temp.next | ||
print() | ||
print(self.length) | ||
|
||
myq = Queue() | ||
myq.enqueue('google') | ||
myq.enqueue('microsoft') | ||
myq.enqueue('facebook') | ||
myq.enqueue('apple') | ||
myq.printt() | ||
myq.dequeue() | ||
myq.printt() | ||
x = myq.peek() | ||
print(x) |
33 changes: 33 additions & 0 deletions
33
Data Structures - Stacks and Queues/Stacks using Arrays.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
class Stack: | ||
def __init__(self): | ||
self.arr = [] | ||
self.length = 0 | ||
|
||
def __str__(self): | ||
return str(self.__dict__) | ||
|
||
def peek(self): | ||
return self.arr[self.length-1] | ||
|
||
def push(self,value): | ||
self.arr.append(value) | ||
self.length += 1 | ||
|
||
def pop(self): | ||
popped_item = self.arr[self.length-1] | ||
del self.arr[self.length-1] | ||
self.length -= 1 | ||
return popped_item | ||
|
||
mystack = Stack() | ||
mystack.push('google') | ||
mystack.push('microsoft') | ||
mystack.push('facebook') | ||
mystack.push('apple') | ||
print(mystack) | ||
x = mystack.peek() | ||
print(x) | ||
mystack.pop() | ||
print(mystack) | ||
|
||
|
Oops, something went wrong.