算法-第四版-练习1.3.38解答

题目

删除第k个元素。实现一个类并支持表1.3.13中的API:

表1.3.12 泛型一般队列的API
public class GeneralizedQueue<Item>  
GeneralizedQueue() 创建一条空队列
boolean isEmpty() 队列是否为空
void insert(Item x) 添加一个元素
Item delete(int k) 删除并返回最早插入的第k个元素

首先用数组实现该数据类型,然后用链表实现该数据类型。注意:我们在第3章中介绍的算法和数据结构可以保证insert()和delete()的实现所需的运行时间和队列中的元素数量成对数关系——请见联系3.5.27。

思路

数组实现时,把需要delete的元素的后面所有元素前移一位;链表实现时,定位到需要delete的结点的前一个结点。

代码

数组实现

package Chap1.$3;

//数组实现
public class E38$1<Item>
{
    private class Node
    {
        Node next;
        Item item;
    }

    private Node first;
    private Node last;
    private int N = 0;
    private Item[] a = (Item[]) new Object[1];
    public boolean isEmpty()
    {
        return N == 0;
    }
    public void resize(int max)
    {
        Item[] temp = (Item[]) new Object[max];
        for (int i = 0; i < N; i++)
            temp[i] = a[i];
        a = temp;
    }
    public void insert(Item x)
    {
        if (N == a.length) resize(2 * a.length);
        a[N++] = x;
    }
    public Item delete(int k)
    {
        Item item = null;
        if (k > N || k < 0) System.out.print("Wrong Input");
        else
        {
            item = a[k - 1];
            for (int i = k; i < a.length ; i++)
                a[i-1]=a[i];
        }
        N--;
        if (N == a.length/4) resize(a.length / 2);
        return item;
    }
    public static void main(String[] args)
    {
        E38$1 e = new E38$1();
        for (int i = 1; i <= 8; i++)
            e.insert(i);
        e.delete(3);
        e.delete(3);
        while(!e.isEmpty())
            System.out.print(e.delete(1)+" ");
    }
}

链表实现

package Chap1.$3;

public class E38$2<Item>
{
    private class Node
    {
        Node next;
        Item item;
    }

    private Node first;
    private Node last;
    private int N;
    public boolean isEmpty()
    {
        return N == 0;
    }
    public void insert(Item item)
    {
        Node oldlast = last;
        last = new Node();
        last.item = item;
        if (isEmpty()) first = last;
        else oldlast.next = last;
        N++;
    }
    public Item delete(int k)
    {
        if (k > N)
        {
            System.out.print("Wrong Input");
            return null;
        }
        if (k == 1)
        {
            Item item = first.item;
            first = first.next;
            return item;
        }
        else
        {
            Node current = first;
            for (int i = 0; i < k - 2; i++)
            {
                current = current.next;
            }
            Item item = current.next.item;
            current.next = current.next.next;
            return item;
        }
    }
    public void print()
    {
        for (Node current = first; current != null; current = current.next)
            System.out.print(current.item + " ");
    }
    public static void main(String[] args)
    {
        E38$2 e = new E38$2();
        for (int i = 1; i <= 8; i++)
            e.insert(i);
        e.delete(1);
        e.delete(2);
        e.print();
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章