Skip to content

Instantly share code, notes, and snippets.

@jskDr
Created January 25, 2020 15:31
Show Gist options
  • Save jskDr/3b571fb0dfee8598266e8c03abb0d74d to your computer and use it in GitHub Desktop.
Save jskDr/3b571fb0dfee8598266e8c03abb0d74d to your computer and use it in GitHub Desktop.
Single LinkedList - Testing for inserting and deleting
class LinkedList:
def __init__(self, d):
self.d = d
self.r = None
def append(self, d):
self.r = LinkedList(d)
def print_list(alist):
while alist:
print(alist.d, end=' --> ')
alist = alist.r
print('None')
def append_list(alist, sequence):
for d in sequence:
alist.r = LinkedList(d)
alist = alist.r
def pop_list(alist):
alist = alist.r
return alist
def insert_list(alist, pos_d, ins_d):
while alist:
if alist.d == pos_d:
org_r = alist.r
alist.r = LinkedList(ins_d)
next_list = alist.r
next_list.r = org_r
alist = alist.r
def delete_list(alist, pos_d):
org_alist = alist
if alist.d == pos_d:
org_alist = alist.r
else:
new_alist = alist
alist = alist.r
while alist:
if alist.d == pos_d:
new_alist.r = alist.r
alist = new_alist.r
new_alist = alist
if alist:
alist = new_alist.r
else:
break
return org_alist
org = LinkedList('a')
alist = org
alist.append('b')
alist = alist.r
alist.append('c')
print_list(org)
insert_list(org, 'b', 'bb')
print_list(org)
new_org = delete_list(org, 'c')
print_list(new_org)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment