Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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:master
base:master
Choose a base branch
Loading
fromRohan-Somadder:master
Open
Show file tree
Hide file tree
Changes fromall 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
99 changes: 0 additions & 99 deletionsdata_structures/3_LinkedList/3_linked_list.py
View file
Open in desktop

This file was deleted.

158 changes: 158 additions & 0 deletionsdata_structures/3_LinkedList/3_linked_lists.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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()

[8]ページ先頭

©2009-2025 Movatter.jp