-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathImplementHashMapUsingLinearChaining.swift
More file actions
100 lines (88 loc) · 2.86 KB
/
ImplementHashMapUsingLinearChaining.swift
File metadata and controls
100 lines (88 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//
// ImplementHashMapUsingLinearChaining.swift
// DSA-Practice
//
// Created by Paridhi Malviya on 12/29/25.
//
class MyHashMap {
class LCNode {
var key: Int
var value: Int
var next: LCNode?
init(key: Int, value: Int, next: LCNode? = nil) {
self.key = key
self.value = value
self.next = next
}
}
var storage: [LCNode?]
private var buckets: Int
private func hash(key: Int) -> Int {
return key % buckets
}
//storage will be an arry of linked lists.
init() {
//an array of 10000 elements while having range from 0 to 1 million (for input)
self.storage = Array(repeating: nil, count: 10000)
buckets = 10000
}
//for below three functions, this function will be used
private func find(head: LCNode, key: Int) -> LCNode {
//set previous pointer to head of the linked list
var prev = head //at head we will be having a dummy node.
var current = head.next
//Stop when the current reaches the length or we find the value
while (current != nil && current?.key != key) {
prev = current!
current = current?.next
}
return prev
}
func put(key: Int, value: Int) {
let index = hash(key: key)
if (storage[index] != nil) {
//traverse through this linked list
//thers is no linked list, so initiate the linked list
storage[index] = LCNode(key: -1, value: -1)
}
//when linked list is already there
let prev = find(head: storage[index]!, key: key)
//if key is in the current linked list.
//if current key is not there in the list
//it will tell us that the current node is not present in the node.
if (prev.next == nil) {
let newNode = LCNode(key: key, value: value)
prev.next = newNode
} else {
//current key is there.
prev.next?.value = value
}
}
func get(key: Int) -> Int {
let index = hash(key: key)
if (storage[index] == nil) {
return -1
}
let prev = find(head: storage[index]!, key: key)
if (prev.next == nil) {
return -1
}
return prev.next!.value
}
//key is equal to the index of the array we took
func remove(key: Int) {
let index = hash(key: key)
//If linked list is not present then return
if (storage[index] == nil) {
return
}
//otherwise traverse through the linked list
let prev: LCNode = find(head: storage[index]!, key: key)
if (prev.next == nil) {
return
}
let temp: LCNode = prev.next!
prev.next = prev.next?.next
temp.next = nil
}
}