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
Changes from 1 commit
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
65 changes: 65 additions & 0 deletions queueusingstack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
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
return self.mainstack.pop(0)

def peek(self):
#returning the front element
return self.mainstack[0]

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