Circular Linked List error

Submitted 6 months, 3 weeks ago
Ticket #360
Views 132
Language Python
Priority Low
Status Closed

I've created a Circular Simple Linked List class, but when I try to delete the first element from the list, instead of seeing the list without the deleted element, I receive only the second element.
Here are my 3 files:
 

class Node:   
    def __init__(self, value=None):
        self.value = value
        self.next = None
# NOT SURE WHAT THE ERROR WAS, SO HERE IS THE UPDATED CODE 

from Node import Node as Node


class CircularLinkedList:
    def __init__(self):
        """
        In lista circular inlantuita, TAIL 
        va contine o referinta catre HEAD
        """
        self.head = Node(None)
        self.tail = Node(None)
        self.head.next = self.tail
        self.tail.next = self.head
        self.length = 0
        
    def __iter__(self):
        temp = self.head
        
        while temp:
            yield temp
            
            if temp == self.tail.next:
                break
            
            temp = temp.next
            
    def __str__(self):
        temp = self.head
        values = []
        while temp:
            values.append(str(temp.value))
            
            if temp == self.tail:
                break
            
            temp = temp.next
            
        return ", ".join(values)
    
    def search(self, value):
        temp = self.head
        index = 0
        
        while temp:
            if temp.value == value:
                return index
            
            if temp == self.tail:
                return -1
            
            temp = temp.next
            index += 1
        
    def insert_node(self, value, location=0):
        node = Node(value)
        if location == 0:
            self.__insert_first(node)
        elif location == self.length:
            self.__insert_last(node)
        else:
            self.__insert_at(node, location)
            
        self.length += 1
        
        return True
    
    def delete_node(self, location=0):
        # daca lista contine doar un element
        if self.head == self.tail:
            temp = self.head
            self.head.next = None
            self.head = None
            self.tail.next = None
            self.tail = None
            
            return temp
            
        if location == 0:
            temp = self.__delete_first()
        elif location == self.length - 1:
            temp = self.__delete_last()
        else:
            temp = self.__delete_at(location)
            
        self.length -= 1
        
        return temp
    
    def clear(self):
        self.head.next = None
        self.head = None
        self.tail.next = None
        self.tail = None
        self.length = 0
    
    def __insert_first(self, node):
        # daca lista este goala
        if self.length == 0: 
            node.next = node
            self.head = node
            self.tail = node
        # daca lista contine elemente
        else:
            node.next = self.head
            self.head = node
            self.tail.next = self.head
            
    def __insert_last(self, node):
        node.next = self.tail.next
        self.tail.next = node
        self.tail = node
     
    def __insert_at(self, node, location):
        prev_node = self.__goto_prev_index(location)
        node.next = prev_node.next
        prev_node.next = node    
    
    def __delete_first(self):
        temp = self.head

        self.head = self.head.next
        self.tail.next = self.head
            
        return temp
    
    def __delete_last(self):
        temp = self.tail
        # penultimul nod din lista
        prev_node = self.__goto_prev_index(self.length - 1)
        prev_node.next = self.head
        self.tail = prev_node
        
        return temp
    
    def __delete_at(self, location):
        prev_node = self.__goto_prev_index(location)
        temp = prev_node.next
        prev_node.next = temp.next
        
        return temp
        
    def __goto_prev_index(self, location):
        temp = self.head
        index = 0
        
        while index < location - 1:
            temp = temp.next
            index += 1
            
        return temp
            
from Node import Node as Node
from CircularSimpleLinkedList import CircularLinkedList


l1 = CircularLinkedList()
l1.insert_node("Arg1")
l1.insert_node("Arg5", 1)
l1.insert_node("Arg2", 1)
l1.insert_node("Arg3", 2)
l1.insert_node("Arg4", 3)
print(l1)
print(f"Arg3 at index: {l1.search('Arg3')}")
print(f"Arg6 at index: {l1.search('Arg6')}")
l1.delete_node()
print(l1)
l1.delete_node(l1.length - 1)
print(l1)
l1.delete_node(1)
print(l1)

On last test it fails: instead of Val 1, Val 4, Val 5, Val 3 I get only Val 1

Can someone spot the error inside CircularLinkedList class? I think the __delete_first(self) method causes the problem, but not sure

Submitted on Feb 26, 21
add a comment

1 Answer

Verified

I've uploaded the updated code, after recreating the entire class (it's a study project)

Submitted 6 months, 3 weeks ago

Thanks for letting know the status

- arun 6 months, 3 weeks ago


Wanna Post Ad?
Reach a large group of audience by posting an ad about your business here. For more details
Drop us a Message