HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Python implementation of a singly linked list

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
singlypythonimplementationlistlinked

Problem

This is my code for the implementation of a singly linked list in Python:

class SList:
    def __init__(self):
        self.root = None
        self.size = 0

    def insert(self, item):
        try:
            if not item:
                raise ValueError
            self.size += 1
            if self.root is None:
                self.root = Node(item)
            else:
                p = Node(item)
                p.next = self.root
                self.root = p
        except ValueError:
            raise ValueError('Cannot add None item to a list')

    def remove(self, index):
        try:
            if index = self.size:
                raise ValueError

            current = self.root
            count = 0
            while count < index:
                current = current.next
                count += 1
            current.next = current.next.next
            self.size -= 1
        except ValueError:
            raise ValueError('Index cannot be negative or greater than the size of the list')

    def __sizeof__(self):
        return self.size

class Node:
    def __init__(self, data):
        try:
            if not data:
                raise ValueError
            self.data = data
            self.next = None
        except ValueError:
            raise ValueError('Node cannot be instantiated without an item')

Solution

raise + except + raise = raise

def remove(self, index):
    try:
        if index = self.size:
            raise ValueError

            ...

    except ValueError:
        raise ValueError('Index cannot be negative or greater than the size of the list')


It make no sense to except and raise again, simplify to:

if index = self.size:
    raise IndexError('Index cannot be negative or greater than the size of the list')

...


The function will look like:

def insert(self, item):
    if item is None:
        raise ValueError('Cannot add None item to a list')
    self.size += 1
    if self.root is None:
        self.root = Node(item)
    else:
        p = Node(item)
        p.next = self.root
        self.root = p


for instead of counting loop

for is a more streamlined way of looping n times than a while loop:

count = 0
        while count < index:
            current = current.next
            count += 1


Becomes:

for _ in range(index):
    current = current.next


(Please note _ is a convention meaning the value will not be used)

Code Snippets

def remove(self, index):
    try:
        if index < 0 or index >= self.size:
            raise ValueError

            ...

    except ValueError:
        raise ValueError('Index cannot be negative or greater than the size of the list')
if index < 0 or index >= self.size:
    raise IndexError('Index cannot be negative or greater than the size of the list')

...
def insert(self, item):
    if item is None:
        raise ValueError('Cannot add None item to a list')
    self.size += 1
    if self.root is None:
        self.root = Node(item)
    else:
        p = Node(item)
        p.next = self.root
        self.root = p
count = 0
        while count < index:
            current = current.next
            count += 1
for _ in range(index):
    current = current.next

Context

StackExchange Code Review Q#150329, answer score: 5

Revisions (0)

No revisions yet.