mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-03-27 17:06:44 +00:00
Merge pull request #243 from cclauss/patch-1
Fix unresolved name: insert_tail()
This commit is contained in:
commit
00c13c29b9
@ -1,21 +1,22 @@
|
|||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
|
|
||||||
|
|
||||||
class Node: # create a Node
|
class Node: # create a Node
|
||||||
def __int__(self, data):
|
def __int__(self, data):
|
||||||
self.data = data # given data
|
self.data = data # given data
|
||||||
self.next = None # given next to None
|
self.next = None # given next to None
|
||||||
|
|
||||||
|
|
||||||
class Linked_List:
|
class Linked_List:
|
||||||
|
|
||||||
pass
|
|
||||||
|
|
||||||
def insert_tail(Head, data):
|
def insert_tail(Head, data):
|
||||||
if(Head.next is None):
|
if Head.next is None:
|
||||||
Head.next = Node(data)
|
Head.next = Node(data)
|
||||||
else:
|
else:
|
||||||
insert_tail(Head.next, data)
|
Head.next.insert_tail(data)
|
||||||
|
|
||||||
def insert_head(Head, data):
|
def insert_head(Head, data):
|
||||||
tamp = Head
|
tamp = Head
|
||||||
if (tamp == None):
|
if tamp is None:
|
||||||
newNod = Node() # create a new Node
|
newNod = Node() # create a new Node
|
||||||
newNod.data = data
|
newNod.data = data
|
||||||
newNod.next = None
|
newNod.next = None
|
||||||
@ -29,22 +30,23 @@ class Linked_List:
|
|||||||
|
|
||||||
def printList(Head): # print every node data
|
def printList(Head): # print every node data
|
||||||
tamp = Head
|
tamp = Head
|
||||||
while tamp!=None:
|
while tamp is not None:
|
||||||
print(tamp.data)
|
print(tamp.data)
|
||||||
tamp = tamp.next
|
tamp = tamp.next
|
||||||
|
|
||||||
def delete_head(Head): # delete from head
|
def delete_head(Head): # delete from head
|
||||||
if Head!=None:
|
if Head is not None:
|
||||||
Head = Head.next
|
Head = Head.next
|
||||||
return Head # return new Head
|
return Head # return new Head
|
||||||
|
|
||||||
def delete_tail(Head): # delete from tail
|
def delete_tail(Head): # delete from tail
|
||||||
if Head!=None:
|
if Head is not None:
|
||||||
tamp = Node()
|
tamp = Node()
|
||||||
tamp = Head
|
tamp = Head
|
||||||
while (tamp.next).next!= None:#find the 2nd last element
|
while tamp.next.next is not None: # find the 2nd last element
|
||||||
tamp = tamp.next
|
tamp = tamp.next
|
||||||
tamp.next=None#delete the last element by give next None to 2nd last Element
|
# delete the last element by give next None to 2nd last Element
|
||||||
|
tamp.next = None
|
||||||
return Head
|
return Head
|
||||||
|
|
||||||
def isEmpty(Head):
|
def isEmpty(Head):
|
||||||
@ -54,7 +56,7 @@ class Linked_List:
|
|||||||
prev = None
|
prev = None
|
||||||
current = Head
|
current = Head
|
||||||
|
|
||||||
while(current):
|
while current:
|
||||||
# Store the current node's next node.
|
# Store the current node's next node.
|
||||||
next_node = current.next
|
next_node = current.next
|
||||||
# Make the current node's next point backwards
|
# Make the current node's next point backwards
|
||||||
|
Loading…
x
Reference in New Issue
Block a user