Linked Lists

Agenda

  1. The LinkedList and Node classes
  2. Implementing append
  3. Implementing deletion
  4. Bidirectional links
  5. Run-time analysis
  6. Closing remarks

1. The LinkedList and Node classes

In [1]:
class LinkedList:
    class Node:
        def __init__(self, val, next=None):
            self.val = val
            self.next = next
    
    def __init__(self):
        self.head = None
        self.count = 0
    
    def prepend(self, value):
        self.head=LinkedList.Node(value, self.head)
        self.count+=1
    
    def __len__(self):
        return self.count
        
    def __iter__(self):
        n = self.head
        while n:
            yield n.val
            n = n.next
    
    def __repr__(self):
        return '[' + ', '.join(str(x) for x in self) + ']'
    
In [2]:
lst = LinkedList()
for i in range(10):
    lst.prepend(i)
lst
Out[2]:
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

2. Implementing append

Option 1

In [4]:
class LinkedList (LinkedList): # note: using inheritance to extend prior definition
    def append(self, value):
        if len(self)==0:
            self.prepend(value)
        else:
            cursor=self.head
            while cursor.next:
                cursor=cursor.next
            cursor.next=LinkedList.Node(value)
            self.count+=1
In [5]:
lst = LinkedList()
for i in range(10):
    lst.append(i)
lst
Out[5]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Option 2

In [6]:
class LinkedList (LinkedList):
    def __init__(self):
        self.head = self.tail = None
        self.count = 0
        
    def prepend(self, value):
        self.head=LinkedList.Node(value, self.head)
        self.count+=1
        if not self.tail:
            self.tail=self.head
        
    def append(self, value):
        if len(self)==0:
            self.prepend(value)
        else:
            self.tail.next=LinkedList.Node(value)
            self.tail=self.tail.next
            self.count+=1
In [7]:
lst = LinkedList()
for i in range(10):
    lst.append(i)
lst
Out[7]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

3. Implementing deletion

Deleting the head

In [8]:
class LinkedList (LinkedList):
    def del_head(self):
        assert(len(self) > 0)
        self.head=self.head.next
        self.count-=1
        if self.count==0:
            self.tail=None
In [9]:
lst = LinkedList()
for i in range(10):
    lst.append(i)
lst.del_head()
lst.del_head()
lst
Out[9]:
[2, 3, 4, 5, 6, 7, 8, 9]

Deleting the tail

In [10]:
class LinkedList (LinkedList):
    def del_tail(self):
        assert(len(self) > 0)
        if self.count==1:
            self.head=self.tail=None
        else:
            cursor=self.head
            while cursor.next is not self.tail:
                cursor=cursor.next
            self.tail=cursor
            self.tail.next=None
            self.count-=1
In [11]:
lst = LinkedList()
for i in range(10):
    lst.append(i)
lst.del_tail()
lst.del_tail()
lst
Out[11]:
[0, 1, 2, 3, 4, 5, 6, 7]
In [ ]:
class LinkedList:
    class Node:
        def __init__(self, val, prior=None, next=None):
            self.val = val
            self.prior = prior
            self.next  = next
    
    def __init__(self):
        self.head = LinkedList.Node(None) # *sentinel* node!
        self.head.prior = self.head
        self.head.next = self.head
        self.count = 0
        
    def prepend(self, value):
        # create a node with the value
        n = LinkedList.Node(value, prior=self.head, next=self.head.next)
        self.head.next.prior = n
        self.head.next = n
        self.count += 1
        
    def append(self, value):
        n = LinkedList.Node(value, prior=self.head.prior, next=self.head)
        n.prior.next = n 
        n.next.prior = n

        self.count += 1
        
    def __len__(self):
        return self.count
        
    def __iter__(self):
        n = self.head.next
        while n is not self.head:
            yield n.val
            n = n.next
    
    def __repr__(self):
        return '[' + ', '.join(str(x) for x in self) + ']'
In [ ]:
lst = LinkedList()
for i in range(10):
    lst.prepend(i)
for i in range(10):
    lst.append(i)
lst

5. Run-time analysis

Run-time complexities for circular, doubly-linked list of $N$ elements:

  • indexing (position-based access) = $O(?)$
  • search (unsorted) = $O(?)$
  • search (sorted) = $O(?)$ --- binary search isn't possible!
  • prepend = $O(?)$
  • append = $O(?)$
  • insertion at arbitrary position: traversal = $O(?)$ + insertion = $O(?)$
  • deletion of arbitrary element: traversal = $O(?)$ + deletion = $O(?)$

6. Closing remarks