mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-19 00:37:02 +00:00
Merge branch 'fix-singly_linked_list' of git://github.com/ashwek/Python-1 into ashwek-fix-singly_linked_list
This commit is contained in:
commit
608a462965
|
@ -8,53 +8,53 @@ class Node: # create a Node
|
||||||
|
|
||||||
|
|
||||||
class Linked_List:
|
class Linked_List:
|
||||||
def insert_tail(Head, data):
|
def __init__(self):
|
||||||
if Head.next is None:
|
self.Head = None # Initialize Head to None
|
||||||
Head.next = Node(data)
|
|
||||||
|
def insert_tail(self, data):
|
||||||
|
if(self.Head is None): self.insert_head(data) #If this is first node, call insert_head
|
||||||
else:
|
else:
|
||||||
Head.next.insert_tail(data)
|
temp = self.Head
|
||||||
|
while(temp.next != None): #traverse to last node
|
||||||
|
temp = temp.next
|
||||||
|
temp.next = Node(data) #create node & link to tail
|
||||||
|
|
||||||
def insert_head(Head, data):
|
def insert_head(self, data):
|
||||||
tamp = Head
|
newNod = Node(data) # create a new node
|
||||||
if tamp is None:
|
if self.Head != None:
|
||||||
newNod = Node() # create a new Node
|
newNod.next = self.Head # link newNode to head
|
||||||
newNod.data = data
|
self.Head = newNod # make NewNode as Head
|
||||||
newNod.next = None
|
|
||||||
Head = newNod # make new node to Head
|
|
||||||
else:
|
|
||||||
newNod = Node()
|
|
||||||
newNod.data = data
|
|
||||||
newNod.next = Head # put the Head at NewNode Next
|
|
||||||
Head = newNod # make a NewNode to Head
|
|
||||||
return Head
|
|
||||||
|
|
||||||
def printList(Head): # print every node data
|
def printList(self): # print every node data
|
||||||
tamp = Head
|
tamp = self.Head
|
||||||
while tamp is not 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(self): # delete from head
|
||||||
if Head is not None:
|
temp = self.Head
|
||||||
Head = Head.next
|
if self.Head != None:
|
||||||
return Head # return new Head
|
self.Head = self.Head.next
|
||||||
|
temp.next = None
|
||||||
|
return temp
|
||||||
|
|
||||||
|
def delete_tail(self): # delete from tail
|
||||||
|
tamp = self.Head
|
||||||
|
if self.Head != None:
|
||||||
|
if(self.Head.next is None): # if Head is the only Node in the Linked List
|
||||||
|
self.Head = None
|
||||||
|
else:
|
||||||
|
while tamp.next.next is not None: # find the 2nd last element
|
||||||
|
tamp = tamp.next
|
||||||
|
tamp.next, tamp = None, tamp.next #(2nd last element).next = None and tamp = last element
|
||||||
|
return tamp
|
||||||
|
|
||||||
def delete_tail(Head): # delete from tail
|
def isEmpty(self):
|
||||||
if Head is not None:
|
return self.Head is None # Return if Head is none
|
||||||
tamp = Node()
|
|
||||||
tamp = Head
|
|
||||||
while tamp.next.next is not None: # find the 2nd last element
|
|
||||||
tamp = tamp.next
|
|
||||||
# delete the last element by give next None to 2nd last Element
|
|
||||||
tamp.next = None
|
|
||||||
return Head
|
|
||||||
|
|
||||||
def isEmpty(Head):
|
def reverse(self):
|
||||||
return Head is None # Return if Head is none
|
|
||||||
|
|
||||||
def reverse(Head):
|
|
||||||
prev = None
|
prev = None
|
||||||
current = Head
|
current = self.Head
|
||||||
|
|
||||||
while current:
|
while current:
|
||||||
# Store the current node's next node.
|
# Store the current node's next node.
|
||||||
|
@ -66,5 +66,32 @@ class Linked_List:
|
||||||
# Make the current node the next node (to progress iteration)
|
# Make the current node the next node (to progress iteration)
|
||||||
current = next_node
|
current = next_node
|
||||||
# Return prev in order to put the head at the end
|
# Return prev in order to put the head at the end
|
||||||
Head = prev
|
self.Head = prev
|
||||||
return Head
|
|
||||||
|
def main():
|
||||||
|
A = Linked_List()
|
||||||
|
print("Inserting 10 at Head")
|
||||||
|
A.insert_head(10)
|
||||||
|
print("Inserting 0 at Head")
|
||||||
|
A.insert_head(0)
|
||||||
|
print("\nPrint List : ")
|
||||||
|
A.printList()
|
||||||
|
print("\nInserting 100 at Tail")
|
||||||
|
A.insert_tail(100)
|
||||||
|
print("Inserting 1000 at Tail")
|
||||||
|
A.insert_tail(1000)
|
||||||
|
print("\nPrint List : ")
|
||||||
|
A.printList()
|
||||||
|
print("\nDelete Head")
|
||||||
|
A.delete_head()
|
||||||
|
print("Delete Tail")
|
||||||
|
A.delete_tail()
|
||||||
|
print("\nPrint List : ")
|
||||||
|
A.printList()
|
||||||
|
print("\nReverse Linked List")
|
||||||
|
A.reverse()
|
||||||
|
print("\nPrint List : ")
|
||||||
|
A.printList()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user