mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
parent
ab65a3915c
commit
cd10c944d1
@ -6,56 +6,56 @@ class Node: # create a Node
|
|||||||
|
|
||||||
class Linked_List:
|
class Linked_List:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.Head = None # Initialize Head to None
|
self.head = None # Initialize head to None
|
||||||
|
|
||||||
def insert_tail(self, data):
|
def insert_tail(self, data):
|
||||||
if self.Head is None:
|
if self.head is None:
|
||||||
self.insert_head(data) # If this is first node, call insert_head
|
self.insert_head(data) # If this is first node, call insert_head
|
||||||
else:
|
else:
|
||||||
temp = self.Head
|
temp = self.head
|
||||||
while temp.next != None: # traverse to last node
|
while temp.next != None: # traverse to last node
|
||||||
temp = temp.next
|
temp = temp.next
|
||||||
temp.next = Node(data) # create node & link to tail
|
temp.next = Node(data) # create node & link to tail
|
||||||
|
|
||||||
def insert_head(self, data):
|
def insert_head(self, data):
|
||||||
newNod = Node(data) # create a new node
|
newNod = Node(data) # create a new node
|
||||||
if self.Head != None:
|
if self.head != None:
|
||||||
newNod.next = self.Head # link newNode to head
|
newNod.next = self.head # link newNode to head
|
||||||
self.Head = newNod # make NewNode as Head
|
self.head = newNod # make NewNode as head
|
||||||
|
|
||||||
def printList(self): # print every node data
|
def printList(self): # print every node data
|
||||||
tamp = self.Head
|
temp = self.head
|
||||||
while tamp is not None:
|
while temp is not None:
|
||||||
print(tamp.data)
|
print(temp.data)
|
||||||
tamp = tamp.next
|
temp = temp.next
|
||||||
|
|
||||||
def delete_head(self): # delete from head
|
def delete_head(self): # delete from head
|
||||||
temp = self.Head
|
temp = self.head
|
||||||
if self.Head != None:
|
if self.head != None:
|
||||||
self.Head = self.Head.next
|
self.head = self.head.next
|
||||||
temp.next = None
|
temp.next = None
|
||||||
return temp
|
return temp
|
||||||
|
|
||||||
def delete_tail(self): # delete from tail
|
def delete_tail(self): # delete from tail
|
||||||
tamp = self.Head
|
temp = self.head
|
||||||
if self.Head != None:
|
if self.head != None:
|
||||||
if self.Head.next is None: # if Head is the only Node in the Linked List
|
if self.head.next is None: # if head is the only Node in the Linked List
|
||||||
self.Head = None
|
self.head = None
|
||||||
else:
|
else:
|
||||||
while tamp.next.next is not None: # find the 2nd last element
|
while temp.next.next is not None: # find the 2nd last element
|
||||||
tamp = tamp.next
|
temp = temp.next
|
||||||
tamp.next, tamp = (
|
temp.next, temp = (
|
||||||
None,
|
None,
|
||||||
tamp.next,
|
temp.next,
|
||||||
) # (2nd last element).next = None and tamp = last element
|
) # (2nd last element).next = None and temp = last element
|
||||||
return tamp
|
return temp
|
||||||
|
|
||||||
def isEmpty(self):
|
def isEmpty(self):
|
||||||
return self.Head is None # Return if Head is none
|
return self.head is None # Return if head is none
|
||||||
|
|
||||||
def reverse(self):
|
def reverse(self):
|
||||||
prev = None
|
prev = None
|
||||||
current = self.Head
|
current = self.head
|
||||||
|
|
||||||
while current:
|
while current:
|
||||||
# Store the current node's next node.
|
# Store the current node's next node.
|
||||||
@ -67,15 +67,15 @@ 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
|
||||||
self.Head = prev
|
self.head = prev
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
A = Linked_List()
|
A = Linked_List()
|
||||||
print("Inserting 1st at Head")
|
print("Inserting 1st at head")
|
||||||
a1 = input()
|
a1 = input()
|
||||||
A.insert_head(a1)
|
A.insert_head(a1)
|
||||||
print("Inserting 2nd at Head")
|
print("Inserting 2nd at head")
|
||||||
a2 = input()
|
a2 = input()
|
||||||
A.insert_head(a2)
|
A.insert_head(a2)
|
||||||
print("\nPrint List : ")
|
print("\nPrint List : ")
|
||||||
@ -88,7 +88,7 @@ def main():
|
|||||||
A.insert_tail(a4)
|
A.insert_tail(a4)
|
||||||
print("\nPrint List : ")
|
print("\nPrint List : ")
|
||||||
A.printList()
|
A.printList()
|
||||||
print("\nDelete Head")
|
print("\nDelete head")
|
||||||
A.delete_head()
|
A.delete_head()
|
||||||
print("Delete Tail")
|
print("Delete Tail")
|
||||||
A.delete_tail()
|
A.delete_tail()
|
||||||
|
Loading…
Reference in New Issue
Block a user