node swap for doubly linked list python
Still working on my quick sort.
I think I got this all shiny. Will be going to the Doubly Linked List post to add it in.
It will swap left or right. end or begin. I've been using it on large randomly generated lists without a problem. Well, without a problem NOW.
I had so many gremlins pop up making this thing work...
The order in which the links are set are soooo touchy..... bull in a china shop.
Throw a match into the oxygen chamber and everything goes to hell.
Anyhoo. Here it is.
If anyone happens to spot anymore of those little bastards that threw all the monkey wrenches at me, feel free to point them out. I'd love to get my hands on them.
Oh and I just realized.... someone might want to swap two items on a list.... if there are just an end and a begin node.... I should add a swap for that..... But that seems like it could lead to REALLY big gremlins.... Troll sized, fire breathing, puppy eating gremlins.....
######### swap nodes left ###############
def swap_left(self, node1, node2):
# or NONE left right or NONE
#spam-[NONE-ARC--n1-->][<-abc-node1--n2--->][<--n1---node2---lvx->][<--n2---RUM or NONE]
#eggs-[NONE ARC--n2-->][<-abc-node2--n1--->][<--n2---node1---lvx->][<--n1--RUM or NONE]
if node1.prev != None and node1.next != None and node2.prev != None and node2.next != None:
print(" swap 1 ")
#### get addresses#####
prevnode1 = node1.prev
prevnode2 = node2.prev
if prevnode1 == node2: ### eggs -
#node1.next = node2
#node2.prev = node1
print(" eggs ")
oldARC = node2.prev
node2.prev.next = node1
oldRUM = node1.next
node1.next.prev = node2
node1.prev = oldARC
node2.next = oldRUM
node1.next = node2
node2.prev = node1
#oldARC.next = node2
#oldRUM.prev = node1
print(node1)
print(node2)
elif prevnode2 == node1:
print("spam") ### spam-
#node1.prev = node2
#node2.next = node1
oldARC = node1.prev
node1.prev.next = node2
node2.prev = oldARC
oldRUM = node2.next
node2.next.prev = node1
node1.next = oldRUM
node2.next = node1
node1.prev = node2
print(f"oldArc = {oldARC}")
print(f"oldRUM = {oldRUM}")
print(node1)
print(node2)
else:
print(" THESE nodes can not be swapped! ")
elif node1.prev == None and node2.next != None:
print("swap 2 (begin node ")
### this one works!!! WOOT! ###
# or NONE left right or NONE
#[NONE----node1--n2--->][<--n1---node2---nlink--->][<--n2---lvx or NONE]
#[NONE----node2--n1--->][<--n2---node1---nlink--->][<--n1---lvx or NONE]
# if node1 prev is none... node1 is begin.
self.begin = node2
nlink = node2.next
node2.prev = None
node2.next = node1
node1.prev = node2
node1.next = nlink
nlink.prev = node1
elif node2.next== None and node1.next != None:
print(" swap 3 (end node)")
### node2 is the end of the list:
##[<--plink---- node1 --- n2 ---->][ <---n1-- node2-- NONE]
##[<--plink---- node2 --- n1 ---->][ <---n2-- node1-- NONE]
self.end = node1
plink = node1.prev
node1.prev.next = node2
node1.next = None
node1.prev = node2
node2.next = node1
node2.prev = plink
elif node2.prev == None and node1.next != None:
print("swap (begin node) 2 - B")
#node2.prev = None, node2 is self.begin of list
# or NONE left right or NONE
#[NONE----node2--n1--->][<--n2---node1---nlink->][<--n1---lvx or NONE]
#[NONE----node1--n2--->][<--n1---node2---nlink->][<--n2---lvx or NONE]
# if node1 prev is none... node1 is begin.
self.begin = node1
nlink = node1.next
node1.prev = None
node1.next = node2
node2.prev = node1
node2.next = nlink
nlink.prev = node2
elif node1.next== None and node2.next != None:
print("swap 3 - B (end node) ")
### node1 is the end of the list:
##[<--plink---- node2 --- n1 ---->][ <---n2-- node1-- NONE]
##[<--plink---- node1 --- n2 ---->][ <---n1-- node2-- NONE]
self.end = node2
plink = node2.prev
node2.prev.next = node1
node2.next = None
node2.prev = node1
node1.next = node2
node1.prev = plink
else:
print("swap 4")
print(" THIS should not take place.... ")
print(" Are you sure the nodes are connected next to each other?")
print(" They may not be in this list.....")
########## END swap nodes left ##########
I think I got this all shiny. Will be going to the Doubly Linked List post to add it in.
It will swap left or right. end or begin. I've been using it on large randomly generated lists without a problem. Well, without a problem NOW.
I had so many gremlins pop up making this thing work...
The order in which the links are set are soooo touchy..... bull in a china shop.
Throw a match into the oxygen chamber and everything goes to hell.
Anyhoo. Here it is.
If anyone happens to spot anymore of those little bastards that threw all the monkey wrenches at me, feel free to point them out. I'd love to get my hands on them.
Oh and I just realized.... someone might want to swap two items on a list.... if there are just an end and a begin node.... I should add a swap for that..... But that seems like it could lead to REALLY big gremlins.... Troll sized, fire breathing, puppy eating gremlins.....
######### swap nodes left ###############
def swap_left(self, node1, node2):
# or NONE left right or NONE
#spam-[NONE-ARC--n1-->][<-abc-node1--n2--->][<--n1---node2---lvx->][<--n2---RUM or NONE]
#eggs-[NONE ARC--n2-->][<-abc-node2--n1--->][<--n2---node1---lvx->][<--n1--RUM or NONE]
if node1.prev != None and node1.next != None and node2.prev != None and node2.next != None:
print(" swap 1 ")
#### get addresses#####
prevnode1 = node1.prev
prevnode2 = node2.prev
if prevnode1 == node2: ### eggs -
#node1.next = node2
#node2.prev = node1
print(" eggs ")
oldARC = node2.prev
node2.prev.next = node1
oldRUM = node1.next
node1.next.prev = node2
node1.prev = oldARC
node2.next = oldRUM
node1.next = node2
node2.prev = node1
#oldARC.next = node2
#oldRUM.prev = node1
print(node1)
print(node2)
elif prevnode2 == node1:
print("spam") ### spam-
#node1.prev = node2
#node2.next = node1
oldARC = node1.prev
node1.prev.next = node2
node2.prev = oldARC
oldRUM = node2.next
node2.next.prev = node1
node1.next = oldRUM
node2.next = node1
node1.prev = node2
print(f"oldArc = {oldARC}")
print(f"oldRUM = {oldRUM}")
print(node1)
print(node2)
else:
print(" THESE nodes can not be swapped! ")
elif node1.prev == None and node2.next != None:
print("swap 2 (begin node ")
### this one works!!! WOOT! ###
# or NONE left right or NONE
#[NONE----node1--n2--->][<--n1---node2---nlink--->][<--n2---lvx or NONE]
#[NONE----node2--n1--->][<--n2---node1---nlink--->][<--n1---lvx or NONE]
# if node1 prev is none... node1 is begin.
self.begin = node2
nlink = node2.next
node2.prev = None
node2.next = node1
node1.prev = node2
node1.next = nlink
nlink.prev = node1
elif node2.next== None and node1.next != None:
print(" swap 3 (end node)")
### node2 is the end of the list:
##[<--plink---- node1 --- n2 ---->][ <---n1-- node2-- NONE]
##[<--plink---- node2 --- n1 ---->][ <---n2-- node1-- NONE]
self.end = node1
plink = node1.prev
node1.prev.next = node2
node1.next = None
node1.prev = node2
node2.next = node1
node2.prev = plink
elif node2.prev == None and node1.next != None:
print("swap (begin node) 2 - B")
#node2.prev = None, node2 is self.begin of list
# or NONE left right or NONE
#[NONE----node2--n1--->][<--n2---node1---nlink->][<--n1---lvx or NONE]
#[NONE----node1--n2--->][<--n1---node2---nlink->][<--n2---lvx or NONE]
# if node1 prev is none... node1 is begin.
self.begin = node1
nlink = node1.next
node1.prev = None
node1.next = node2
node2.prev = node1
node2.next = nlink
nlink.prev = node2
elif node1.next== None and node2.next != None:
print("swap 3 - B (end node) ")
### node1 is the end of the list:
##[<--plink---- node2 --- n1 ---->][ <---n2-- node1-- NONE]
##[<--plink---- node1 --- n2 ---->][ <---n1-- node2-- NONE]
self.end = node2
plink = node2.prev
node2.prev.next = node1
node2.next = None
node2.prev = node1
node1.next = node2
node1.prev = plink
else:
print("swap 4")
print(" THIS should not take place.... ")
print(" Are you sure the nodes are connected next to each other?")
print(" They may not be in this list.....")
########## END swap nodes left ##########
Comments
Post a Comment