zoukankan      html  css  js  c++  java
  • 单链表

    public class Element {
    public Object object;
    public Element next = null;

    public Element(Object object){
    this.object = object;
    }
    }
    public class SingleLinkList {
    private Element element;

    public void init(){
    element = new Element(null);
    element.next = null;
    }

    public void add(int data){
    Element node = new Element(data);
    Element tmp = element;
    while (tmp.next != null){
    tmp=tmp.next;
    }
    tmp.next = node;
    }

    public void delete(Object value){
    Element tmp = element;
    //找到删除元素的上一个节点,在进行删除
    while (tmp.next.object != value){
    tmp = tmp.next;
    }
    tmp.next = tmp.next.next;
    //删除的是下一个节点
    /*while (tmp.next != null){
    if(tmp.object == value){
    tmp.next = tmp.next.next;
    return;
    }
    tmp = tmp.next;
    }*/
    }

    public int length(){
    Element tmp = element;
    int i=0;
    while (tmp.next != null){
    tmp = tmp.next;
    i++;
    }
    return i;
    }

    public void display(){
    Element tmp = element.next;
    while (tmp != null){
    System.out.println("链表的值:" + tmp.object);
    tmp = tmp.next;
    }
    }
    }
    public class Demo {
    public static void main(String[] args){
    SingleLinkList sll = new SingleLinkList();
    sll.init();
    sll.add(1);
    sll.add(2);
    sll.add(3);
    sll.add(4);
    sll.add(5);
    sll.display();
    System.out.println("链表的长度为:" + sll.length());
    System.out.println("---------------删除元素----------------------");
    sll.delete(2);
    sll.display();
    System.out.println("链表的长度为:" + sll.length());
    }
    }
  • 相关阅读:
    HDU4366 Successor 线段树+预处理
    POJ2823 Sliding Window 单调队列
    HDU寻找最大值 递推求连续区间
    UVA846 Steps 二分查找
    HDU3415 Max Sum of MaxKsubsequence 单调队列
    HDU时间挑战 树状数组
    UVA10168 Summation of Four Primes 哥德巴赫猜想
    UESTC我要长高 DP优化
    HDUChess 递推
    HDU4362 Dragon Ball DP+优化
  • 原文地址:https://www.cnblogs.com/mutong1228/p/10759752.html
Copyright © 2011-2022 走看看