- Notifications
You must be signed in to change notification settings - Fork1.6k
Better messages when no data is found#41
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
Rohan-Somadder wants to merge2 commits intocodebasics:masterChoose a base branch fromRohan-Somadder:master
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
Show all changes
2 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
99 changes: 0 additions & 99 deletionsdata_structures/3_LinkedList/3_linked_list.py
This file was deleted.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
158 changes: 158 additions & 0 deletionsdata_structures/3_LinkedList/3_linked_lists.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
class Node: | ||
def __init__(self,val=None,next=None): | ||
self.val = val | ||
self.next = next | ||
class LinkedList: | ||
def __init__(self): | ||
self.head=None | ||
def show(self): | ||
if self.head == None: | ||
print('Linked List is empty') | ||
return | ||
else: | ||
itr = self.head | ||
while itr: | ||
print(itr.val,' --> ',end='') | ||
itr=itr.next | ||
print('None') | ||
def getlength(self): | ||
itr = self.head | ||
c=0 | ||
while itr: | ||
c+=1 | ||
itr = itr.next | ||
return c | ||
def insert_at_end(self,val): | ||
if self.head is None: | ||
node = Node(val,None) | ||
self.head = node | ||
return | ||
else: | ||
itr = self.head | ||
while itr.next : | ||
itr=itr.next | ||
itr.next=Node(val,None) | ||
def insert_at_beginning(self,val): | ||
node = Node(val,self.head) | ||
self.head=node | ||
def insert_values(self,x): | ||
self.head = None | ||
for d in x: | ||
self.insert_at_end(d) | ||
def insert_at_pos(self,pos,val): | ||
if pos < 0 or pos >= self.getlength(): | ||
raise Exception("Invalid syntax") | ||
if pos == 0: | ||
self.insert_at_beginning(val) | ||
return | ||
elif pos == self.getlength() - 1: | ||
self.insert_at_end(val) | ||
return | ||
else: | ||
itr = self.head | ||
c = 0 | ||
while itr: | ||
if c==pos -1: | ||
node = Node(val,itr.next) | ||
itr.next=node | ||
break | ||
c+=1 | ||
itr = itr.next | ||
def remove_at_pos(self,pos): | ||
if pos < 0 or pos >= self.getlength(): | ||
raise Exception('Invalid position') | ||
if pos ==0: | ||
self.head=self.head.next | ||
return | ||
itr = self.head | ||
c=0 | ||
while itr: | ||
if c==pos-1: | ||
itr.next=itr.next.next | ||
break | ||
c+=1 | ||
itr=itr.next | ||
pass | ||
# Adding for task | ||
def insert_after_value(self,val_before,val): | ||
if self.head is None: | ||
return | ||
if self.head.val==val_before: | ||
self.head.next = Node(val,self.head.next) | ||
return | ||
itr=self.head | ||
c=0 | ||
while itr: | ||
if val_before==itr.val: | ||
self.insert_at_pos(c+1,val) | ||
break | ||
c+=1 | ||
itr=itr.next | ||
else: | ||
print('Data is not in the linked list') | ||
def remove_by_val(self,val): | ||
if self.head is None: | ||
return | ||
if self.head.val == val: | ||
self.head = self.head.next | ||
return | ||
itr=self.head | ||
c=0 | ||
while itr: | ||
if itr.val == val: | ||
self.remove_at_pos(c) | ||
break | ||
itr=itr.next | ||
c+=1 | ||
else: | ||
print('No such value present in linked list') | ||
if __name__ == '__main__': | ||
''' | ||
l=LinkedList() | ||
l.insert_values(['A','B','C','D']) | ||
l.show() | ||
l.insert_at_pos(2,'F') | ||
l.show() | ||
l.remove_at_pos(3) | ||
l.show() | ||
l.insert_at_end('R') | ||
l.show() | ||
l.insert_at_beginning('J') | ||
l.show() | ||
l.insert_after_value('A','K') | ||
l.show() | ||
l.remove_by_val('K') | ||
l.show() | ||
''' | ||
ll = LinkedList() | ||
ll.insert_values(["banana","mango","grapes","orange"]) | ||
ll.show() | ||
ll.insert_after_value("mango","apple") # insert apple after mango | ||
ll.show() | ||
ll.remove_by_val("orange") # remove orange from linked list | ||
ll.show() | ||
ll.remove_by_val("figs") | ||
ll.show() | ||
ll.remove_by_val("banana") | ||
ll.remove_by_val("mango") | ||
ll.remove_by_val("apple") | ||
ll.remove_by_val("grapes") | ||
ll.show() |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.