MY_TEST

Then sa place to work ah brother but more appropriate place

Big Brother over ten

Asan song

  1. '''''
    • A linked list is similar to an array, it holds values. However, links in a linked list do not have indexes.
    • This is an example of a double ended, doubly linked list.
    • 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'''
  2. class LinkedList: #making main class named linked list
  3.  **def**  \_\_init\_\_(self):
  4.     self.head = None
  5.     self.tail = None
  6.  **def**  insertHead(self, x):
  7.     newLink = Link(x)                            #Create a new link with a value attached to it
  8.      **if** (self.isEmpty() == True):                  #Set the first element added to be the tail
  9.         self.tail = newLink
  10.      **else** :
  11.         self.head.previous = newLink             # newLink \<-- currenthead(head)
  12.     newLink.next = self.head                     # newLink \<--\> currenthead(head)
  13.     self.head = newLink                          # newLink(head) \<--\> oldhead
  14.  **def**  deleteHead(self):
  15.     temp = self.head
  16.     self.head = self.head.next                   # oldHead \<--\> 2ndElement(head)
  17.     self.head.previous = None                    # oldHead --\> 2ndElement(head) nothing pointing at it so the old head will be removed
  18.      **if** (self.head  **is**  None):
  19.         self.tail = None                         #if empty linked list
  20.      **return**  temp
  21.  **def**  insertTail(self, x):
  22.     newLink = Link(x)
  23.     newLink.next = None                         # currentTail(tail)    newLink --\>
  24.     self.tail.next = newLink                    # currentTail(tail) --\> newLink --\>
  25.     newLink.previous = self.tail                #currentTail(tail) \<--\> newLink --\>
  26.     self.tail = newLink                         # oldTail \<--\> newLink(tail) --\>
  27.  **def**  deleteTail(self):
  28.     temp = self.tail
  29.     self.tail = self.tail.previous              # 2ndLast(tail) \<--\> oldTail --\> None
  30.     self.tail.next = None                       # 2ndlast(tail) --\> None
  31.      **return**  temp
  32.  **def**  delete(self, x):
  33.     current = self.head
  34.      **while** (current.value != x):                  # Find the position to delete
  35.         current = current.next
  36.      **if** (current == self.head):
  37.         self.deleteHead()
  38.      **elif** (current == self.tail):
  39.         self.deleteTail()
  40.      **else** : #Before: 1 \<--\> 2(current) \<--\> 3
  41.         current.previous.next = current.next # 1 --\> 3
  42.         current.next.previous = current.previous # 1 \<--\> 3
  43.  **def**  isEmpty(self):                               #Will return True if the list is empty
  44.      **return** (self.head  **is**  None)
  45.  **def**  display(self):                                #Prints contents of the list
  46.     current = self.head
  47.      **while** (current != None):
  48.         current.displayLink()
  49.         current = current.next
  50.      **print** ()
  51. class Link:
  52. next = None                                       #This points to the link in front of the new link
  53. previous = None                                   #This points to the link behind the new link
  54.  **def**  \_\_init\_\_(self, x):
  55.     self.value = x
  56.  **def**  displayLink(self):
  57.      **print** ("{}".format(self.value), end=" ")

Guess you like

Origin www.cnblogs.com/hichens/p/11774662.html