Simplify code by dropping support for legacy Python (#1143)

* Simplify code by dropping support for legacy Python

* sort() --> sorted()
This commit is contained in:
Christian Clauss
2019-08-19 15:37:49 +02:00
committed by GitHub
parent 32aa7ff081
commit 47a9ea2b0b
145 changed files with 367 additions and 976 deletions

View File

@@ -4,14 +4,13 @@
- Each link references the next link and the previous one.
- A Doubly Linked List (DLL) contains an extra pointer, typically called previous pointer, together with next pointer and data which are there in singly linked list.
- Advantages over SLL - IT can be traversed in both forward and backward direction.,Delete operation is more efficent'''
from __future__ import print_function
class LinkedList: #making main class named linked list
def __init__(self):
self.head = None
self.tail = None
def insertHead(self, x):
newLink = Link(x) #Create a new link with a value attached to it
if(self.isEmpty() == True): #Set the first element added to be the tail
@@ -20,52 +19,52 @@ class LinkedList: #making main class named linked list
self.head.previous = newLink # newLink <-- currenthead(head)
newLink.next = self.head # newLink <--> currenthead(head)
self.head = newLink # newLink(head) <--> oldhead
def deleteHead(self):
temp = self.head
self.head = self.head.next # oldHead <--> 2ndElement(head)
self.head = self.head.next # oldHead <--> 2ndElement(head)
self.head.previous = None # oldHead --> 2ndElement(head) nothing pointing at it so the old head will be removed
if(self.head is None):
self.tail = None #if empty linked list
return temp
def insertTail(self, x):
newLink = Link(x)
newLink.next = None # currentTail(tail) newLink -->
self.tail.next = newLink # currentTail(tail) --> newLink -->
newLink.previous = self.tail #currentTail(tail) <--> newLink -->
self.tail = newLink # oldTail <--> newLink(tail) -->
def deleteTail(self):
temp = self.tail
self.tail = self.tail.previous # 2ndLast(tail) <--> oldTail --> None
self.tail.next = None # 2ndlast(tail) --> None
return temp
def delete(self, x):
current = self.head
while(current.value != x): # Find the position to delete
current = current.next
if(current == self.head):
self.deleteHead()
elif(current == self.tail):
self.deleteTail()
else: #Before: 1 <--> 2(current) <--> 3
current.previous.next = current.next # 1 --> 3
current.next.previous = current.previous # 1 <--> 3
def isEmpty(self): #Will return True if the list is empty
return(self.head is None)
def display(self): #Prints contents of the list
current = self.head
while(current != None):
current.displayLink()
current = current.next
current = current.next
print()
class Link:

View File

@@ -1,6 +1,3 @@
from __future__ import print_function
class Node: # create a Node
def __init__(self, data):
self.data = data # given data
@@ -10,7 +7,7 @@ class Node: # create a Node
class Linked_List:
def __init__(self):
self.Head = None # Initialize Head to None
def insert_tail(self, data):
if(self.Head is None): self.insert_head(data) #If this is first node, call insert_head
else:
@@ -37,7 +34,7 @@ class Linked_List:
self.Head = self.Head.next
temp.next = None
return temp
def delete_tail(self): # delete from tail
tamp = self.Head
if self.Head != None:
@@ -46,7 +43,7 @@ class Linked_List:
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
tamp.next, tamp = None, tamp.next #(2nd last element).next = None and tamp = last element
return tamp
def isEmpty(self):
@@ -79,7 +76,7 @@ def main():
print("\nPrint List : ")
A.printList()
print("\nInserting 1st at Tail")
a3=input()
a3=input()
A.insert_tail(a3)
print("Inserting 2nd at Tail")
a4=input()
@@ -96,6 +93,6 @@ def main():
A.reverse()
print("\nPrint List : ")
A.printList()
if __name__ == '__main__':
main()