Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement queue using stack #43

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
90 changes: 90 additions & 0 deletions hashset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
class Node(object):

def __init__(self, key):
self.key = key
self.next = None


class MyHashSet(object):

def __init__(self):
"""
Initialize your data structure here.
"""
self.size = 1000
self.hashset = [None] * self.size


def hashing(self, key):
return key % self.size


def add(self, key):
"""
:type key: int
:rtype: None
"""
hashed = self.hashing(key)
if self.hashset[hashed]:
current = self.hashset[hashed]
while current:
if current.key == key:
return
elif current.next is None:
break
current = current.next
current.next = Node(key)
else:
self.hashset[hashed] = Node(key)

def remove(self, key):
"""
:type key: int
:rtype: None
"""
hashed = self.hashing(key)
if self.hashset[hashed]:
prev, current = self.hashset[hashed], self.hashset[hashed]
if not current:
return
elif current.key == key:
self.hashset[hashed] = current.next
else:
current = current.next
while current:
if current.key == key:
prev.next = current.next
break
prev, current = prev.next, current.next



def contains(self, key):
"""
Returns true if this set contains the specified element
:type key: int
:rtype: bool
"""
hashed = self.hashing(key)
if self.hashset[hashed]:
current = self.hashset[hashed]
while current:
if current.key == key:
return True
current = current.next
return False


hashSet = MyHashSet()
hashSet.add(1)
hashSet.add(2)
print(hashSet.contains(1))
# // returns true
print(hashSet.contains(3))
# // returns false (not found)
hashSet.add(2)
print(hashSet.contains(2))
# // returns true
hashSet.remove(2)
print(hashSet.contains(2))
# // returns false (already removed)
67 changes: 67 additions & 0 deletions queueusingstack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
class MyQueue(object):
def __init__(self):
self.mainstack=[]

def push(self, x):
#appending at the back
self.mainstack.append(x)

def pop(self):
#poppping the element from front
#since it acts as a list last element would be -1 index
return self.mainstack.pop(-1)

def peek(self):
#returning the front element
#since it acts as a list last element would be -1 index
return self.mainstack[-1]

def empty(self):
return len(self.mainstack)==0
#here this is a python list even if its queue or stack it will be inserted from back and deleted from front.



# def __init__(self):
# self.mainstack=[]
# self.tmpstack=[]

# def push(self, x):
# while self.mainstack:
# self.tmpstack.append(self.mainstack.pop())
# self.tmpstack.append(x)
# while self.tmpstack:
# self.mainstack.append(self.tmpstack.pop())

# def pop(self):
# return self.mainstack.pop()


# def peek(self):
# return self.mainstack[-1]


# def empty(self):
# return len(self.mainstack)==0

# def __init__(self):
# self.mainstack=[]
# self.tmpstack=[]

# def push(self, x):
# while self.mainstack:
# self.tmpstack.append(self.mainstack.pop())
# self.tmpstack.append(x)
# while self.tmpstack:
# self.mainstack.append(self.tmpstack.pop())

# def pop(self):
# return self.mainstack.pop()


# def peek(self):
# return self.mainstack[-1]


# def empty(self):
# return len(self.mainstack)==0