将图形实现为邻接列表时,删除双链表中的方法

时间:2014-05-12 14:13:21

标签: java data-structures graph adjacency-list

我一直在尝试将简单图表(定向/无向+加权/未加权)作为我的数据结构课程项目的一部分作为邻接列表,我无法使用删除方法,除非我为每种类型使用instanceof(Vertex,Edge和GraphNode)。我有equals方法在每个类中实现,但delete方法默认不调用它们,我必须在执行之前进行向下转换。以下是此方法的代码:

public void delete(T data)
{
    if(!isEmpty())
    {
        if(data instanceof Edge)
        {
            Edge edge = (Edge)data;
            Node<T> temp = head;
            while(temp != null)
            {
                if(edge.equals((Edge)head.data))
                {
                    deleteHead();
                    return;
                }   
                else if(edge.equals((Edge)tail.data))
                {
                    deleteTail();
                    return;
                }
                else
                {
                    while(temp != null)
                    {   
                        if(edge.equals((Edge)temp.data))
                        {
                            (temp.previous).next = temp.next;
                            (temp.next).previous = temp.previous;
                            return;
                        }
                        temp = temp.next;
                    }
                }
            }
        }

        if(data instanceof Vertex)
        {
            Vertex vertex = (Vertex)data;
            Node<T> temp = head;
            while(temp != null)
            {
                if(vertex.equals((Vertex)head.data))
                {
                    deleteHead();
                    return;
                }   
                else if(vertex.equals((Vertex)tail.data))
                {
                    deleteTail();
                    return;
                }
                else
                {
                    while(temp != null)
                    {   
                        if(vertex.equals((Vertex)temp.data))
                        {
                            (temp.previous).next = temp.next;
                            (temp.next).previous = temp.previous;
                            return;
                        }
                        temp = temp.next;
                    }
                }
            }
        }

        if(data instanceof GraphNode)
        {
            GraphNode gn = (GraphNode)data;
            Node<T> temp = head;
            while(temp != null)
            {
                if(gn.equals((GraphNode)head.data))
                {
                    deleteHead();
                    return;
                }   
                else if(gn.equals((GraphNode)tail.data))
                {
                    deleteTail();
                    return;
                }
                else
                {
                    while(temp != null)
                    {   
                        if(gn.equals((GraphNode)temp.data))
                        {
                            (temp.previous).next = temp.next;
                            (temp.next).previous = temp.previous;
                            return;
                        }
                        temp = temp.next;
                    }
                }
            }
        }
        if(data.equals(head.data))
        {
            deleteHead();
            return;
        }   
        else if(data.equals(tail.data))
        {
            deleteTail();
            return;
        }
        Node<T> current = head;
        while(current != null)
        {   
            if(current.data.equals(data))
            {
                current = current.next;
                current.previous = current;
                return;
            }
            current = current.next;
        }
        throw new NodeDoesNotExistException();
    }
    else 
        throw new EmptyListException();
}

0 个答案:

没有答案