单链表

作者: 四喜汤圆 | 来源:发表于2018-03-31 21:18 被阅读11次

一、特点

  • 不占用连续存储空间
  • 删除插入元素较方便
  • 查询比较繁琐,需遍历链表元素

二、基本操作演示

// 向链表的尾部插入一个元素
public void addOne(int data)
// 删除链表中第index个元素
public boolean deleteNode(int index)
// 得到链表长度
public int length()
// 将链表中元素排序,返回排序后的链表头指针
public Node sortList()
import java.util.Stack;

/**
 * 
 * <p>
 * Description: 链表
 * </p>
 * 
 * @author
 * @date 2018-3-30
 * @version 1.0
 */
public class MyLinkedList {
    // 头指针
    Node head = null;

    // 尾插法:向链表的尾部插入一个元素
    public void addOne(int data) {
        Node newNode = new Node(data);
        if (isEmpty()) {
            head = newNode;
        } else {
            Node cur = head;
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = newNode;
        }
    }

    /**
     * 判断链表是否为空
     * @return
     */
    public boolean isEmpty() {
        return head == null;
    }

    /**
     * 删除链表中第index个元素 
     * @param index:从1开始
     * @return
     */
    public boolean deleteNode(int index) {
        // 链表为空
        if (isEmpty()) {
            return false;
        }
        // 删除元素位置不合理
        if (index <= 0 || index > length()) {
            return false;
        }
        if (index == 1) {
            head = head.next;
            return true;
        } else {
            Node pre = head;
            Node cur = head.next;
            // 当前正在判断的节点
            int i = 2;
            // 从链表的第二个元素开始遍历
            while (cur != null) {
                if (index == i) {
                    pre.next = cur.next;
                    return true;
                }
                pre = cur;
                cur = cur.next;
                i++;
            }
            return false;
        }
    }

    // 得到链表长度
    public int length() {
        // 遍历链表
        int count = 0;
        Node cur = head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    // 将链表中元素排序,返回排序后的链表头指针
    public Node sortList(){
        return null;
    }
    
    /**
     * 从头结点开始打印链表
     */
    public void print(){
        Node cur=head;
        while(cur!=null){
            System.out.print(cur.data+" ");
            cur=cur.next;
        }
        System.out.println();
    }
    
    /**
     * 借助栈:逆序输出栈中元素
     */
    public void reversePrint_stack() {
        Stack<Integer> ms = new Stack<Integer>();
        Node cur = head;
        while (cur != null) {
            ms.push(cur.data);
            cur = cur.next;
        }
        for (int i = ms.size() - 1; i >= 0; i--) {
            Integer item = ms.get(i);
            System.out.print(item.toString() + " ");
        }
        System.out.println();
    }

    /**
     * 利用递归:逆序输出栈中元素
     */
    private void reversePrint_recursion() {
        reversePrintStack(head);
        System.out.println();
    }
    
    private void reversePrintStack(Node head) {
        if (head == null) {
            return;
        }
        reversePrintStack(head.next);
        System.out.print(head.data + " ");
    }
    
    class Node {
        // 数据域
        int data;
        // 指针域
        Node next;

        public Node(int data) {
            this.data = data;
            this.next = null;
        }
    }
    
    public static void main(String[] args) {
        MyLinkedList l=new MyLinkedList();
        l.addOne(1);
        l.addOne(2);
        l.addOne(3);
        l.addOne(4);
        // 从头结点开始顺序输出
        l.print();
        l.deleteNode(1);
        l.print();
        // 逆序输出
        l.reversePrint_stack();
        l.reversePrint_recursion();
    }
}

相关文章

  • 单链表 C++

    单链表 C++ 题目 1、创建单链表2、初始化单链表3、释放单链表4、获取单链表中元素的数量5、输出单链表中的所有...

  • 线性表:顺序表和链表

    顺序表(数组)优缺点 链表优点 单链表使用 单链表结构 单链表初始化 单链表初始化 单链表建立: 头插法 尾插法 ...

  • 单向链表算法

    单向链表 反转单向链表 单链表查找倒数第k个节点 单链表递归倒序打印 单链表排序 单链表删除重复节点

  • 链表基本操作

    1、删除单链表节点 2、插入单链表结点 单链表具体实现

  • 25_静态单链表的实现

    关键词: 单链表的一个缺点、静态单链表设计思路、静态单链表的继承层次结构、静态单链表的实现思路、静态单链表的实现 ...

  • 线性表的链式存储-单链表

    单链表操作 [x] 单链表的创建(尾插法、头插法) [x] 单链表的查找操作 [x] 单链表的删除操作 [x] 单...

  • Algorithm小白入门 -- 单链表

    单链表递归反转链表k个一组反转链表回文链表 1. 递归反转链表 单链表节点的结构如下: 1.1 递归反转整个单链表...

  • 单链表反转

    单链表 单链表反转 递归方法

  • JavaScript数据结构2——单链表

    以下的代码包括了以下几部分 单链表初始化 单链表的插入 单链表的删除 单链表的创建(头插法) 单链表的创建(尾插法...

  • 链表

    链表:通过“指针”将零散的内存块联系起来。常见链表结构:单链表、循环链表和双链表。 单链表 对比数组学习单链表 循...

网友评论

      本文标题:单链表

      本文链接:https://www.haomeiwen.com/subject/eahwcftx.html