2017-11-25 09:23:50 +00:00
|
|
|
from __future__ import print_function
|
2018-01-21 07:56:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Node: # create a Node
|
2018-03-07 07:04:37 +00:00
|
|
|
def __init__(self, data):
|
2018-01-21 07:56:16 +00:00
|
|
|
self.data = data # given data
|
|
|
|
self.next = None # given next to None
|
|
|
|
|
|
|
|
|
2017-09-09 19:03:31 +00:00
|
|
|
class Linked_List:
|
2018-01-21 07:56:16 +00:00
|
|
|
def insert_tail(Head, data):
|
2018-01-21 07:59:55 +00:00
|
|
|
if Head.next is None:
|
2017-10-13 06:56:14 +00:00
|
|
|
Head.next = Node(data)
|
2017-09-09 19:03:31 +00:00
|
|
|
else:
|
2018-01-21 07:56:16 +00:00
|
|
|
Head.next.insert_tail(data)
|
2017-10-13 06:56:14 +00:00
|
|
|
|
2018-01-21 07:56:16 +00:00
|
|
|
def insert_head(Head, data):
|
2017-09-09 19:03:31 +00:00
|
|
|
tamp = Head
|
2018-01-21 07:56:16 +00:00
|
|
|
if tamp is None:
|
|
|
|
newNod = Node() # create a new Node
|
2017-09-09 19:03:31 +00:00
|
|
|
newNod.data = data
|
|
|
|
newNod.next = None
|
2018-01-21 07:56:16 +00:00
|
|
|
Head = newNod # make new node to Head
|
2017-09-09 19:03:31 +00:00
|
|
|
else:
|
|
|
|
newNod = Node()
|
|
|
|
newNod.data = data
|
2018-01-21 07:56:16 +00:00
|
|
|
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
|
|
|
|
tamp = Head
|
|
|
|
while tamp is not None:
|
2017-09-09 19:03:31 +00:00
|
|
|
print(tamp.data)
|
2018-01-21 07:56:16 +00:00
|
|
|
tamp = tamp.next
|
|
|
|
|
|
|
|
def delete_head(Head): # delete from head
|
|
|
|
if Head is not None:
|
|
|
|
Head = Head.next
|
|
|
|
return Head # return new Head
|
|
|
|
|
|
|
|
def delete_tail(Head): # delete from tail
|
|
|
|
if Head is not None:
|
2017-09-09 19:03:31 +00:00
|
|
|
tamp = Node()
|
|
|
|
tamp = Head
|
2018-01-21 07:59:55 +00:00
|
|
|
while tamp.next.next is not None: # find the 2nd last element
|
2017-09-09 19:03:31 +00:00
|
|
|
tamp = tamp.next
|
2018-01-21 07:56:16 +00:00
|
|
|
# delete the last element by give next None to 2nd last Element
|
|
|
|
tamp.next = None
|
2017-09-09 19:03:31 +00:00
|
|
|
return Head
|
|
|
|
|
2017-10-13 06:56:14 +00:00
|
|
|
def isEmpty(Head):
|
2018-01-21 07:56:16 +00:00
|
|
|
return Head is None # Return if Head is none
|
|
|
|
|
2017-10-22 23:04:10 +00:00
|
|
|
def reverse(Head):
|
|
|
|
prev = None
|
|
|
|
current = Head
|
2018-01-21 07:56:16 +00:00
|
|
|
|
2018-01-21 07:59:55 +00:00
|
|
|
while current:
|
2018-01-21 07:56:16 +00:00
|
|
|
# Store the current node's next node.
|
2017-10-22 23:04:10 +00:00
|
|
|
next_node = current.next
|
|
|
|
# Make the current node's next point backwards
|
|
|
|
current.next = prev
|
|
|
|
# Make the previous node be the current node
|
|
|
|
prev = current
|
|
|
|
# Make the current node the next node (to progress iteration)
|
|
|
|
current = next_node
|
2018-01-21 07:56:16 +00:00
|
|
|
# Return prev in order to put the head at the end
|
2017-10-22 23:04:10 +00:00
|
|
|
Head = prev
|
2018-10-17 21:28:57 +00:00
|
|
|
return Head
|