Quick actions

cmd+k|ctrl+k

Navigation

Languages

MyLinkedList_nodeClass

Snippet info

Language

Python

Visibility

public

Author

arthurpc02

Created

2024-09-24T21:51:23.105945Z

Updated

2024-09-25T14:38:02.855348Z

class Node():
    def __init__(self, value, next_node=None):
        self.value = value
        self.next_node = next_node

class LinkedList():
    def __init__(self, value_first_item):
        self.head = Node(value_first_item)
        self.length = 1
        self.tail = self.head
    
        
    def get_head(self):
        return self.head.value
    
        
    def get_tail(self):
        return self.tail.value
        
    
    def append(self, value):
        new_node = Node(value)
        self.tail.next_node = new_node
        self.tail = new_node
        self.length+=1
    
    
    def prepend(self, value):
        new_node = Node(value, self.head)
        self.head = new_node
        self.length+=1
    
    
    def get_position(self, position):
        """ get the value of a specific position in the linked list."""
        if position < 0:
            raise IndexError("No negative positions")
        elif position > self.length:
            raise IndexError("Position out of boundaries")
        elif position == self.length:
            return self.get_tail()
        elif position == 0:
            return self.get_head()
        
        node = self.head.next_node
        curr_position = 1
        if node != None:
            if position == 1:
                return node.value
            else:
                while node.next_node != None: # traversing
                    node = node.next_node
                    curr_position+=1
                    if curr_position == position:
                        break
                return node.value


    def insert(self, value, position):
        """ Insert a new node in the middle of the list"""
        
        if position < 0:
            raise IndexError("No negative positions")
        elif position > self.length:
            raise IndexError("Position out of boundaries")
        
        new_node = Node(value)
        
        curr_node = self.head
        curr_position = 0
        while curr_node.next_node != None:
            curr_node = self.head.next_node
            curr_position += 1
            if curr_position == position-1:
                new_node = Node(value, curr_node.next_node)
                curr_node.next_node = new_node
                break
        pass
    
    
    def printLinkedList(self):
        
        curr_node = self.head
        curr_position = 0
        print(f"{curr_position}: {curr_node.value}")
        while curr_node.next_node != None:
            curr_node = curr_node.next_node
            curr_position+=1
            print(f"{curr_position}: {curr_node.value}")
        
        
        
print("Hello World!")
myLinkedList = LinkedList(5)
print(myLinkedList.get_head())
print(myLinkedList.get_tail())
myLinkedList.append(10)
myLinkedList.append(15)
print(myLinkedList.get_head())
assert(myLinkedList.get_head() == 5)
assert(myLinkedList.get_tail() == 15)
print(myLinkedList.get_position(1))
myLinkedList.append(20)
print(myLinkedList.get_position(3))
print(myLinkedList.get_position(4))
assert(myLinkedList.get_position(4) == 20)
myLinkedList.prepend(2)
assert(myLinkedList.get_head() == 2)
assert(myLinkedList.get_position(0) == 2)
myLinkedList.insert(value=7,position=2)
assert(myLinkedList.get_position(2)==7)
myLinkedList.printLinkedList()


print("Good-bye Cruel World")
INFO