单链表_JAVA描述《数据结构与算法分析》

简介:
节点
None.gif package DataStructures;
None.gif
ExpandedBlockStart.gif class ListNode  {
InBlock.gif    
InBlock.gif    //Frientdly data;accessible by other package routines
InBlock.gif
    Object element;
InBlock.gif    ListNode next;
InBlock.gif    
InBlock.gif    // Constructers
ExpandedSubBlockStart.gif
    ListNode(Object theElement) {
InBlock.gif        this(theElement, null);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gif    ListNode(Object theElement, ListNode n) {
InBlock.gif        element = theElement;
InBlock.gif        next = n;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
迭代器
None.gif package DataStructures;
None.gif
ExpandedBlockStart.gif public  class LinkedListItr  {
InBlock.gif    ListNode current; // Current position
InBlock.gif

ExpandedSubBlockStart.gif    LinkedListItr(ListNode theNode) {
InBlock.gif        current = theNode;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gif    public boolean IsPastEnd() {
InBlock.gif        return current == null;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gif    public Object Retrive() {
InBlock.gif        return IsPastEnd() ? null : current.element;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gif    public void Advance() {
InBlock.gif        if (!IsPastEnd())
InBlock.gif            current = current.next;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

链表类
None.gif package DataStructures;
None.gif
ExpandedBlockStart.gif public  class LinkedList  {
InBlock.gif    private ListNode header;
InBlock.gif
ExpandedSubBlockStart.gif    public LinkedList() {
InBlock.gif        header = new ListNode(null);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gif    public boolean IsEmpty() {
InBlock.gif        return header.next == null;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gif    public void makeEmpty() {
InBlock.gif        header.next = null;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gif    public LinkedListItr Zeroth() {
InBlock.gif        return new LinkedListItr(header);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gif    public LinkedListItr First() {
InBlock.gif        return new LinkedListItr(header.next);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gif    /**
InBlock.gif     * Return iterator corresponding to the first node containing an item.
InBlock.gif     * 
InBlock.gif     * 
@param x
InBlock.gif     *            the item to search for.
InBlock.gif     * 
@return an iterator; iterator IsPastEnd if item is not found.
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gif    public LinkedListItr Find(Object x) {
InBlock.gif        ListNode itr = header.next;
InBlock.gif
InBlock.gif        while (itr != null && !itr.element.equals(x))
InBlock.gif            itr = itr.next;
InBlock.gif
InBlock.gif        return new LinkedListItr(itr);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gif    /**
InBlock.gif     * Remove th first occurrence of an item.
InBlock.gif     * 
InBlock.gif     * 
@param x
InBlock.gif     *            the item to remove.
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gif    public void Remove(Object x) {
InBlock.gif        LinkedListItr p = FindPrevious(x);
InBlock.gif
InBlock.gif        if (p.current.next != null)
InBlock.gif            p.current.next = p.current.next.next; // Bypass deleted node
ExpandedSubBlockEnd.gif
    }

InBlock.gif
ExpandedSubBlockStart.gif    /**
InBlock.gif     * Return itreator prior to the first node containing an item.
InBlock.gif     * 
InBlock.gif     * 
@param x
InBlock.gif     *            the item to search for.
InBlock.gif     * 
@return appropriate iterator if the item is found.Otherwise, the iterator
InBlock.gif     *         corresponding to the last element in the lis is returned.
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gif    public LinkedListItr FindPrevious(Object x) {
InBlock.gif        ListNode itr = header;
InBlock.gif
InBlock.gif        while (itr.next != null && !itr.next.element.equals(x))
InBlock.gif            itr = itr.next;
InBlock.gif
InBlock.gif        return new LinkedListItr(itr);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gif    /**
InBlock.gif     * Insert after p.
InBlock.gif     * 
InBlock.gif     * 
@param x
InBlock.gif     *            the item to insear.
InBlock.gif     * 
@param p
InBlock.gif     *            the position prior to the newly inserted item.
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gif    public void Insert(Object x, LinkedListItr p) {
InBlock.gif        if (p != null && p.current != null)
InBlock.gif            p.current.next = new ListNode(x, p.current.next);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    // Simple print method
ExpandedSubBlockStart.gif
    public static void PrintLinkedList(LinkedList theList) {
InBlock.gif        if (theList.IsEmpty())
InBlock.gif            System.out.print("Empty list");
ExpandedSubBlockStart.gif        else {
InBlock.gif            LinkedListItr itr = theList.First();
InBlock.gif            for (; !itr.IsPastEnd(); itr.Advance())
InBlock.gif                System.out.print(itr.Retrive() + " ");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        System.out.println();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
本文转自冬冬博客园博客,原文链接:http://www.cnblogs.com/yuandong/archive/2006/08/19/481154.html ,如需转载请自行联系原作者
相关文章
|
1月前
|
负载均衡 NoSQL 算法
一天五道Java面试题----第十天(简述Redis事务实现--------->负载均衡算法、类型)
这篇文章是关于Java面试中Redis相关问题的笔记,包括Redis事务实现、集群方案、主从复制原理、CAP和BASE理论以及负载均衡算法和类型。
一天五道Java面试题----第十天(简述Redis事务实现--------->负载均衡算法、类型)
|
30天前
|
搜索推荐 算法 Java
手写快排:教你用Java写出高效排序算法!
快速排序(QuickSort)是经典的排序算法之一,基于分治思想,平均时间复杂度为O(n log n),广泛应用于各种场合。在这篇文章中,我们将手写一个Java版本的快速排序,从基础实现到优化策略,并逐步解析代码背后的逻辑。
42 1
|
20天前
|
设计模式 缓存 算法
揭秘策略模式:如何用Java设计模式轻松切换算法?
【8月更文挑战第30天】设计模式是解决软件开发中特定问题的可重用方案。其中,策略模式是一种常用的行为型模式,允许在运行时选择算法行为。它通过定义一系列可互换的算法来封装具体的实现,使算法的变化与客户端分离。例如,在电商系统中,可以通过定义 `DiscountStrategy` 接口和多种折扣策略类(如 `FidelityDiscount`、`BulkDiscount` 和 `NoDiscount`),在运行时动态切换不同的折扣逻辑。这样,`ShoppingCart` 类无需关心具体折扣计算细节,只需设置不同的策略即可实现灵活的价格计算,符合开闭原则并提高代码的可维护性和扩展性。
37 2
|
28天前
|
安全 算法 Java
java系列之~~网络通信安全 非对称加密算法的介绍说明
这篇文章介绍了非对称加密算法,包括其定义、加密解密过程、数字签名功能,以及与对称加密算法的比较,并解释了非对称加密在网络安全中的应用,特别是在公钥基础设施和信任网络中的重要性。
|
29天前
|
算法 Java 索引
【Java集合类面试四】、 描述一下Map put的过程
这篇文章详细描述了HashMap中put操作的过程,包括首次扩容、计算索引、插入数据以及链表转红黑树和可能的再次扩容。
【Java集合类面试四】、 描述一下Map put的过程
|
1月前
|
存储 算法 Java
LeetCode经典算法题:打家劫舍java详解
LeetCode经典算法题:打家劫舍java详解
46 2
|
1月前
|
人工智能 算法 Java
LeetCode经典算法题:井字游戏+优势洗牌+Dota2参议院java解法
LeetCode经典算法题:井字游戏+优势洗牌+Dota2参议院java解法
37 1
|
1月前
|
存储 算法 Java
LeetCode经典算法题:预测赢家+香槟塔java解法
LeetCode经典算法题:预测赢家+香槟塔java解法
35 1
|
28天前
|
Java
描述 Java 中的重载和重写
【8月更文挑战第22天】
13 0
|
29天前
|
数据采集 搜索推荐 算法
【高手进阶】Java排序算法:从零到精通——揭秘冒泡、快速、归并排序的原理与实战应用,让你的代码效率飙升!
【8月更文挑战第21天】Java排序算法是编程基础的重要部分,在算法设计与分析及实际开发中不可或缺。本文介绍内部排序算法,包括简单的冒泡排序及其逐步优化至高效的快速排序和稳定的归并排序,并提供了每种算法的Java实现示例。此外,还探讨了排序算法在电子商务、搜索引擎和数据分析等领域的广泛应用,帮助读者更好地理解和应用这些算法。
20 0