美文网首页
大话数据结构—单链表(二)

大话数据结构—单链表(二)

作者: 浅浅星空 | 来源:发表于2019-02-18 22:40 被阅读18次

1.链式结构

public class LinkedList {

    private Node head;
    private int size;

    public LinkedList() {
        head = null;
        size = 0;
    }

    public Object getElement(int i) {
        Node temp = head;
        int j = 0;
        while (temp != null && j < i) {
            temp = temp.next;
            j++;
        }
        if (temp == null || j > i) {
            return null;
        }
        return temp.data;
    }

    public void add(Object data) {
        if (head == null) {
            head = new Node(data);
            size++;
        } else {
            Node current = head;
            while (current.next != null) {
                current = current.next;
            }
            current.next = new Node(data);
            size++;
        }

    }

    public boolean delete(int index) {
        if (size == 0) {
            return false;
        }
        Node previous = head;
        Node current = head;
        int j = 0;
        while (current != null && j < index) {
            previous = current;
            current = current.next;
            j++;
        }
        if (current == null || j > index) {
            return false;
        }
        previous.next = current.next;
        current.next = null;
        size--;
        return true;
    }

    public void display() {
        Node temp = head;
        while (temp != null) {
            System.out.print(temp.data);
            temp = temp.next;
        }
        System.out.println("---------------");
    }

    public static void main(String[] args) {
        LinkedList linkedList = new LinkedList();
        linkedList.add(1);
        linkedList.add(2);
        linkedList.add(3);
        linkedList.add(4);
        linkedList.display();

        linkedList.delete(1);
        linkedList.display();

        System.out.println(linkedList.getElement(2));
        System.out.println(linkedList.getElement(5));
        
    }

}

class Node {
    Object data;
    Node next;

    public Node(Object data) {
        this.data = data;
        this.next = null;
    }
}

相关文章

  • 大话数据结构—单链表(二)

    1.链式结构

  • 2018-12-01

    数据结构使用二级指针创建单链表以及单链表的各种操作 //单链表创建 #include #include #incl...

  • 数据结构与算法相关

    第二章 数据结构与算法相关 1.常用的数据结构有哪些? 数组、栈、队列、链表(单链表、双向链表、循环链表)、树、散...

  • 数据结构 | 其二 链表

    冰河winner - 数据结构之链表 2.1 单向链表 数据结构(一) 单链表的实现-JAVA 2.2 双端链表 ...

  • 数据结构与算法之链表(三)单链表的常用操作

    引言 在上篇文章数据结构与算法之链表(二)单链表的基本实现中我们学习了单链表的基本概念,本篇我们在此基础之上研究单...

  • 数据结构——Golang实现单链表

    转载请注明出处:数据结构——Golang实现单链表 1. 单链表 1.1. 定义 单向链表(单链表)是链表的一种,...

  • 算法与数据结构知识汇总(二、链表)

    1、概念 2、链表的数据结构 单向链表的数据结构如下图: 上图数据结构为单向链表,简称单链表,该数据结构由若干个节...

  • 数据结构--单链表

    数据结构--单链表 单链表:单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。链表中...

  • 数据结构笔记

    数据结构课程概览 ================== 1.顺序表 2.链表:单链表,单向循环链表,双链表...

  • 常见数据结构和算法

    常见数据结构 线性数据结构(按顺序具有数据元素的数据结构):数组,堆栈,链表(单链表 双链表),队列非线性数据结...

网友评论

      本文标题:大话数据结构—单链表(二)

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