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

Precourse 1 #2070

Open
wants to merge 1 commit 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
19 changes: 14 additions & 5 deletions Exercise_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,28 @@ class myStack:
#Please read sample.java file before starting.
#Kindly include Time and Space complexity at top of each file
def __init__(self):

self.stack = []
def isEmpty(self):

if len(self.stack)==0:
return True
else:
return False

def push(self, item):

self.stack.append(item)
def pop(self):

if len(self.stack)!=0:
self.stack.pop()

def peek(self):

if len(self.stack)!=0:
return self.stack[-1]
return []
def size(self):
return len(self.stack)

def show(self):
return self.stack


s = myStack()
Expand Down
12 changes: 10 additions & 2 deletions Exercise_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ def __init__(self, data):

class Stack:
def __init__(self):

self.pointer = None
def push(self, data):

node = Node(data)
node.next = self.pointer
self.pointer = node
def pop(self):
if self.pointer is None:
print("Stack Empty")
else:
data = self.pointer.data
self.pointer = self.pointer.next
return data

a_stack = Stack()
while True:
Expand Down
40 changes: 39 additions & 1 deletion Exercise_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class ListNode:
A node in a singly-linked list.
"""
def __init__(self, data=None, next=None):
self.data =data
self.next = next

class SinglyLinkedList:
def __init__(self):
Expand All @@ -17,16 +19,52 @@ def append(self, data):
Insert a new element at the end of the list.
Takes O(n) time.
"""
node = ListNode(data=data)
if self.head is None:
self.head = node
return
current = self.head
while current.next:
current = current.next

current.next = node



def find(self, key):
"""
Search for the first element with `data` matching
`key`. Return the element or `None` if not found.
Takes O(n) time.
"""


current = self.head
while current.next:
if current.data ==key:
return current
current = current.next
return None
def remove(self, key):
"""
Remove the first occurrence of `key` in the list.
Takes O(n) time.
"""

current = self.head
previous = None

if current is None:
print("List Empty")
return

while current:
if current.data == key:
if previous:
previous.next = current.next
else:
self.head = current.next
return
else:
previous = current
current = current.next