Open In App

sllist class of llist module in Python

Last Updated : 05 Sep, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

llist is an extension module of CPython that provides a basic linked list structure. They are significantly faster than dequeue and even the standard list for that matter.

Singly Linked List

It is a simple unidirectional data structure hence it can be traversed only form head to the last node. Each node of a singly linked list contains data and the address of the node preceding it. To simply frame this, each node points to the node after it. The following diagram shows the general structure of a singly linked list:

In llist, there is a sllist object that helps implement a singly linked list successfully.

Sllist Objects

class llist.sllist([iterable]): returns a new linked list initialized from the iterables provided. If no iterables are given the linked list is produced but empty.




import llist
from llist import sllist, sllistiterator
  
lst = llist.sllist(['first', 'second', 'third'])
print(lst)


Output:

sllist([first, second, third])

sllist supports following attributes:

  • first : read only attribute, prints the first attribute of the list and None if the list is empty.




    print(lst.first)

    
    

    Output:

    sllistnode(first)
  • last: read only property, returns the last element of the list(tail) and None if the list is empty.




    print(lst.last)

    
    

    Output:

    sllistnode(third)
  • size : read only attribute that returns the size of the list




    print(lst.size)

    
    

    Output:

    3

sslist also supports following methods:

  • append(x) : adds x to the right side of the list and returns a inserted sllist node. If x already is a sllist node then a new node is created and initialized with the value extracted from x.




    lst.append('fourth')
    print(lst)

    
    

    Output:

    sllist([first, second, third, fourth])
  • appendleft(x) : adds x to the left side of the list and returns a inserted sllist node. If x already is a sllist node then a new node is created and initialized with the value extracted from x.




    lst.appendleft('fourth')
    print(lst)

    
    

    Output:

    sllist([fourth, first, second, third])
  • appendright(x) : adds x to the right side of the list and returns a inserted sllist node. If x already is a sllist node then a new node is created and initialized with the value extracted from x.




    lst.appendright('fourth')
    print(lst)

    
    

    Output:

    sllist([first, second, third, fourth])
  • clear() clears all nodes from the list.




    lst.clear()
    print(lst)

    
    

    Output:

    sllist()
  • extend(iterable) : appends elements from the iterable to the right side of the list




    lst.extend(['fourth', 'fifth'])
    print(lst)

    
    

    Output:

    sllist([first, second, third, fourth, fifth])
  • extendleft(iterable) : appends element from the iterable to the left side of the list.




    lst.extendleft(['fourth', 'fifth'])
    print(lst)

    
    

    Output:

    sllist([fifth, fourth, first, second, third])
  • extendright(iterable) : appends elements from the iterable to the right side of the list




    lst.extend(['fourth', 'fifth'])
    print(lst)

    
    

    Output:

    sllist([first, second, third, fourth, fifth])
  • insertafter(x, node) : inserts x after specified node, the argument x should be a sllist node, the value of x is extracted and is given to the node to be inserted.




    node = lst.nodeat(0)
    lst.insertafter('fourth', node)
    print(lst)

    
    

    Output:

    sllist([first, fourth, second, third])
  • insertbefore(x, node) : inserts x before the specified node, the argument x should be a sllist node, the value of x is extracted and is given to the node to be inserted.




    node = lst.nodeat(1)
    lst.insertbefore('fourth', node)
    print(lst)

    
    

    Output:

    sllist([first, fourth, second, third])
  • nodeat() : returns node at the specified index. Negative indices are allowed if counting is started from right side.




    node = lst.nodeat(1)
    print(node)

    
    

    Output:

    sllistnode(second)
  • pop() : removes an element from the right side of the list.




    lst.pop()
    print(lst)

    
    

    Output:

    sllist([first, second])
  • popleft() : removes an element from the left side of the list.




    lst.popleft()
    print(lst)

    
    

    Output:

    sllist([second, third])
  • popright() : removes an element from the right side of the list.




    lst.pop()
    print(lst)

    
    

    Output:

    sllist([first, second])
  • remove(node): removes the specified node




    node = lst.nodeat(1)
    lst.remove(node)
    print(lst)

    
    

    Output:

    sllist([first, third])
  • rotate(n) : rotates the list n steps. If n is positive rotate to right and if negative to the left.




    lst.rotate(-1)
    print(lst)

    
    

    Output:

    sllist([second, third, first])

In addition to these methods, sllist supports iteration, cmp(lst1, lst2), rich comparison operators, constant time len(lst), hash(lst) and subscript references lst[1234] for accessing elements by index.

We further discuss more objects of llist which are related to sllist:

sllistnode

Implements a node in a singly linked list which can optionally be initialized if value is provided.




node = llist.sllistnode('zeroth')
print(node)


Output:

sllistnode(zeroth)

This objects also supports following attributes:

  • next : next node in the list
  • value : extracts value stored in a specific node




node = lst.nodeat(0)
print(node.next)
print(node.value)


Output:

sllistnode(second)
first

sllistiterator object

returns a new singly linked list iterator. These objects are not created by users rather they are returned by sllist.__iter__() method to hold iteration state. Iterating through sllistiterator interface will directly yield values stored in the nodes.




import llist 
from llist import sllist
  
lst = llist.sllist(['first', 'second', 'third'])
  
for value in lst:
  print(value)


Output:

first
second
third


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads