From f941aa74a0a19cf027d441b2ce59cd264f51ddee Mon Sep 17 00:00:00 2001 From: Chethana Wickramaarachchi <71955126+Dhavaji@users.noreply.github.com> Date: Sun, 10 Oct 2021 19:14:24 +0530 Subject: [PATCH 1/2] Create stack.md --- stack.md | 122 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 stack.md diff --git a/stack.md b/stack.md new file mode 100644 index 0000000..23a4689 --- /dev/null +++ b/stack.md @@ -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()); + } +} From a7b185c23691c41290bc6fe658b5f534f7429d9c Mon Sep 17 00:00:00 2001 From: Chethana Wickramaarachchi <71955126+Dhavaji@users.noreply.github.com> Date: Sat, 22 Oct 2022 11:35:43 +0530 Subject: [PATCH 2/2] Hash table added --- Python/HashTable.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Python/HashTable.py diff --git a/Python/HashTable.py b/Python/HashTable.py new file mode 100644 index 0000000..52bb502 --- /dev/null +++ b/Python/HashTable.py @@ -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')}") \ No newline at end of file