|
9 | 9 | Delete operation is more efficient"""
|
10 | 10 |
|
11 | 11 |
|
12 |
| -class LinkedList: # making main class named linked list |
13 |
| - def __init__(self): |
14 |
| - self.head = None |
15 |
| - self.tail = None |
| 12 | +class LinkedList: |
| 13 | + """ |
| 14 | + >>> linked_list = LinkedList() |
| 15 | + >>> linked_list.insert_at_head("a") |
| 16 | + >>> linked_list.insert_at_tail("b") |
| 17 | + >>> linked_list.delete_tail() |
| 18 | + 'b' |
| 19 | + >>> linked_list.is_empty |
| 20 | + False |
| 21 | + >>> linked_list.delete_head() |
| 22 | + 'a' |
| 23 | + >>> linked_list.is_empty |
| 24 | + True |
| 25 | + """ |
16 | 26 |
|
17 |
| - def insertHead(self, x): |
18 |
| - newLink = Link(x) # Create a new link with a value attached to it |
19 |
| - if self.isEmpty(): # Set the first element added to be the tail |
20 |
| - self.tail = newLink |
| 27 | + def __init__(self): |
| 28 | + self.head = None # First node in list |
| 29 | + self.tail = None # Last node in list |
| 30 | + |
| 31 | + def insert_at_head(self, data): |
| 32 | + new_node = Node(data) |
| 33 | + if self.is_empty: |
| 34 | + self.tail = new_node |
| 35 | + self.head = new_node |
21 | 36 | else:
|
22 |
| - self.head.previous = newLink # newLink <-- currenthead(head) |
23 |
| - newLink.next = self.head # newLink <--> currenthead(head) |
24 |
| - self.head = newLink # newLink(head) <--> oldhead |
25 |
| - |
26 |
| - def deleteHead(self): |
27 |
| - temp = self.head |
28 |
| - self.head = self.head.next # oldHead <--> 2ndElement(head) |
29 |
| - # oldHead --> 2ndElement(head) nothing pointing at it so the old head will be |
30 |
| - # removed |
31 |
| - self.head.previous = None |
32 |
| - if self.head is None: |
33 |
| - self.tail = None # if empty linked list |
34 |
| - return temp |
35 |
| - |
36 |
| - def insertTail(self, x): |
37 |
| - newLink = Link(x) |
38 |
| - newLink.next = None # currentTail(tail) newLink --> |
39 |
| - self.tail.next = newLink # currentTail(tail) --> newLink --> |
40 |
| - newLink.previous = self.tail # currentTail(tail) <--> newLink --> |
41 |
| - self.tail = newLink # oldTail <--> newLink(tail) --> |
42 |
| - |
43 |
| - def deleteTail(self): |
44 |
| - temp = self.tail |
45 |
| - self.tail = self.tail.previous # 2ndLast(tail) <--> oldTail --> None |
46 |
| - self.tail.next = None # 2ndlast(tail) --> None |
47 |
| - return temp |
48 |
| - |
49 |
| - def delete(self, x): |
| 37 | + self.head.previous = new_node |
| 38 | + new_node.next = self.head |
| 39 | + self.head = new_node |
| 40 | + |
| 41 | + def delete_head(self) -> str: |
| 42 | + if self.is_empty: |
| 43 | + return "List is empty" |
| 44 | + |
| 45 | + head_data = self.head.data |
| 46 | + if self.head.next: |
| 47 | + self.head = self.head.next |
| 48 | + self.head.previous = None |
| 49 | + |
| 50 | + else: # If there is no next previous node |
| 51 | + self.head = None |
| 52 | + self.tail = None |
| 53 | + |
| 54 | + return head_data |
| 55 | + |
| 56 | + def insert_at_tail(self, data): |
| 57 | + new_node = Node(data) |
| 58 | + self.tail.next = new_node |
| 59 | + new_node.previous = self.tail |
| 60 | + self.tail = new_node |
| 61 | + |
| 62 | + def delete_tail(self) -> str: |
| 63 | + if self.is_empty: |
| 64 | + return "List is empty" |
| 65 | + |
| 66 | + tail_data = self.tail.data |
| 67 | + if self.tail.previous: |
| 68 | + self.tail = self.tail.previous |
| 69 | + self.tail.next = None |
| 70 | + else: # if there is no previous node |
| 71 | + self.head = None |
| 72 | + self.tail = None |
| 73 | + |
| 74 | + return tail_data |
| 75 | + |
| 76 | + def delete(self, data) -> str: |
50 | 77 | current = self.head
|
51 | 78 |
|
52 |
| - while current.value != x: # Find the position to delete |
53 |
| - current = current.next |
| 79 | + while current.data != data: # Find the position to delete |
| 80 | + if current.next: |
| 81 | + current = current.next |
| 82 | + else: # We have reached the end an no value matches |
| 83 | + return "No data matching given value" |
54 | 84 |
|
55 | 85 | if current == self.head:
|
56 |
| - self.deleteHead() |
| 86 | + self.delete_head() |
57 | 87 |
|
58 | 88 | elif current == self.tail:
|
59 |
| - self.deleteTail() |
| 89 | + self.delete_tail() |
60 | 90 |
|
61 | 91 | else: # Before: 1 <--> 2(current) <--> 3
|
62 | 92 | current.previous.next = current.next # 1 --> 3
|
63 | 93 | current.next.previous = current.previous # 1 <--> 3
|
| 94 | + return data |
64 | 95 |
|
65 |
| - def isEmpty(self): # Will return True if the list is empty |
| 96 | + @property |
| 97 | + def is_empty(self): # Will return True if the list is empty |
66 | 98 | return self.head is None
|
67 | 99 |
|
68 | 100 | def display(self): # Prints contents of the list
|
| 101 | + if self.is_empty: |
| 102 | + return "List is empty" |
69 | 103 | current = self.head
|
70 | 104 | while current is not None:
|
71 |
| - current.displayLink() |
| 105 | + current.display_node() |
72 | 106 | current = current.next
|
73 | 107 | print()
|
74 | 108 |
|
75 | 109 |
|
76 |
| -class Link: |
77 |
| - next = None # This points to the link in front of the new link |
78 |
| - previous = None # This points to the link behind the new link |
79 |
| - |
80 |
| - def __init__(self, x): |
81 |
| - self.value = x |
| 110 | +class Node: |
| 111 | + def __init__(self, data): |
| 112 | + self.data = data |
| 113 | + self.previous = None |
| 114 | + self.next = None |
82 | 115 |
|
83 |
| - def displayLink(self): |
84 |
| - print(f"{self.value}", end=" ") |
| 116 | + def display_node(self): |
| 117 | + print(f"{self.data}", end=" ") |
0 commit comments