Skip to content
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
31 changes: 31 additions & 0 deletions Python/HashTable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import pprint
class Hashtable:
def __init__(self, elements):
self.bucket_size = len(elements)
self.buckets = [[] for i in range(self.bucket_size)]
self._assign_buckets(elements)
def _assign_buckets(self, elements):
for key, value in elements: #calculates the hash of each key
hashed_value = hash(key)
index = hashed_value % self.bucket_size # positions the element in the bucket using hash
self.buckets[index].append((key, value)) #adds a tuple in the bucket
def get_value(self, input_key):
hashed_value = hash(input_key)
index = hashed_value % self.bucket_size
bucket = self.buckets[index]
for key, value in bucket:
if key == input_key:
return(value)
return None
def __str__(self):
return pprint.pformat(self.buckets) # pformat returns a printable representation of the object
if __name__ == "__main__":
capitals = [
('France', 'Paris'),
('United States', 'Washington D.C.'),
('Italy', 'Rome'),
('Canada', 'Ottawa')
]
hashtable = Hashtable(capitals)
print(hashtable)
print(f"The capital of Italy is {hashtable.get_value('Italy')}")
122 changes: 122 additions & 0 deletions stack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import static java.lang.System.exit;


class StackUsingLinkedlist {


private class Node {

int data;
Node link;
}

Node top;

StackUsingLinkedlist()
{
this.top = null;
}


public void push(int x)
{

Node temp = new Node();


if (temp == null) {
System.out.print("\nHeap Overflow");
return;
}


temp.data = x;


temp.link = top;


top = temp;
}


public boolean isEmpty()
{
return top == null;
}


public int peek()
{

if (!isEmpty()) {
return top.data;
}
else {
System.out.println("Stack is empty");
return -1;
}
}


public void pop()
{

if (top == null) {
System.out.print("\nStack Underflow");
return;
}


top = (top).link;
}

public void display()
{

if (top == null) {
System.out.printf("\nStack Underflow");
exit(1);
}
else {
Node temp = top;
while (temp != null) {


System.out.printf("%d->", temp.data);


temp = temp.link;
}
}
}
}
// main class
public class GFG {
public static void main(String[] args)
{

StackUsingLinkedlist obj = new StackUsingLinkedlist();
// insert Stack value
obj.push(20);
obj.push(40);
obj.push(60);
obj.push(80);


obj.display();


System.out.printf("\nTop element is %d\n", obj.peek());


obj.pop();
obj.pop();


obj.display();


System.out.printf("\nTop element is %d\n", obj.peek());
}
}