数据结构之链表

数据结构之链表

不在头节点的链表操作

public class LinkedList<E> {

//内部类,结点
private class Node{
    //定义成员变量
    public E e;
    public Node next;

    /**
     * 构造函数
     */
    public Node(E e,Node next){
        this.e=e;
        this.next=next;
    }
    public Node(E e){
        this(e,null);
    }
    public Node(){
        this(null,null);
    }

    @Override
    public String toString(){
        return e.toString();
    }
}

private Node head;//头结点
private int size;

/**
 * 构造函数
 */
public LinkedList(){
    head=null;
    size=0;
}

/**
 * 获取链表元素个数
 */
public int getSize(){
    return size;
}

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

/**
 * 在链表头部添加新元素
 */
public void addFirst(E e){
//        Node node=new Node(e);
//        node.next=head;
//        head=node;

    head=new Node(e,head);
    size++;
}

/**
 * 在链表的index位置添加新的元素e(练习用,一般不使用)
 */
public void add(int index,E e){
    //先判断index位置是否满足条件
    if(index<0 || index>size){
        new IllegalArgumentException("add failed");
    }

    if(index == 0){//在链表头部添加
        addFirst(e);
    }else{
        Node pre=new Node();//创建一个结点
        for(int i=0;i<index-1;i++){//循环遍历到index的前一个节点
            pre=pre.next;
        }

//            Node node=new Node(e);
//            node=pre.next;
//            pre.next=node;
        pre.next=new Node(e,pre.next);

        size++;

    }
}

/**
 * 在链表尾部添加新元素
 * @param e
 */
public void addLast(E e){
    add(size,e );
}
}

带虚拟头结点的链表操作

/**
 * 带虚拟头结点
 */

public class LinkedListDummyHead<E> {

//内部类,结点
private class Node{
    //定义成员变量
    public E e;
    public Node next;

    /**
     * 构造函数
     */
    public Node(E e,Node next){
        this.e=e;
        this.next=next;
    }
    public Node(E e){
        this(e,null);
    }
    public Node(){
        this(null,null);
    }

    @Override
    public String toString(){
        return e.toString();
    }
}

private Node DummyHead;//虚拟头结点
private int size;

/**
 * 构造函数
 */
public LinkedListDummyHead(){
    DummyHead=new Node(null,null);
    size=0;
}

/**
 * 获取链表元素个数
 */
public int getSize(){
    return size;
}

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

/**
 * 在链表的index位置添加新的元素e(练习用,一般不使用)
 */
public void add(int index,E e) {
    //先判断index位置是否满足条件
    if (index < 0 || index > size) {
        new IllegalArgumentException("add failed");
    }

    Node pre = DummyHead;
    for (int i = 0; i < index ; i++) {//循环遍历到index  注意:区别没有虚拟头结点时,循环遍历到index-1
        pre = pre.next;
    }

//        Node node = new Node(e);
//        node = pre.next;
//        pre.next = node;
    pre.next = new Node(e, pre.next);
    size++;
}

/**
 * 在链表头部添加新元素
 */
public void addFirst(E e){
    add(0,e );
}

/**
 * 在链表尾部添加新元素
 * @param e
 */
public void addLast(E e){
    add(size,e );
}

/**
 * 获取链表index位置的元素
 */
public E get(int index){
    //先判断是否满足条件
    if(index<0 || index>=size){
        new IllegalArgumentException("get failed");
    }

    Node cur=DummyHead.next;//当前结点
    //循环遍历到index位置
    for(int i=0;i<index;i++){
        cur=cur.next;
    }
    return cur.e;//返回index位置的元素
}

/**
 * 获取链表第一个元素
 */
public E getFirst(){
    return get(0);
}

/**
 * 获取链表最后一个元素
 */
public E getLast(){
    return get(size-1);
}

/**
 * 修改链表index位置元素为e
 */
public void set(int index,E e ){
    //先判断index位置是否满足条件
    if (index < 0 || index > size) {
        new IllegalArgumentException("add failed");
    }

    Node cur=DummyHead.next;//当前结点
    for(int i=0;i<index;i++){//循环遍历到index
        cur=cur.next;
    }
    cur.e=e;
}

/**
 * 查找链表中是否有元素e
 */
public boolean contains(E e){
    Node cur=DummyHead.next;//当前结点
    //循环遍历整个链表
    while(cur!=null){
        if(cur.e.equals(e)){
            return true;
        }
        cur=cur.next;
    }
    return false;
}

/**
 * 删除index位置的结点,并返回该结点的值
 */
public E remove(int index){
    //先判断是否满足条件
    if(index<0 || index>=size){
        new IllegalArgumentException("remove failed");
    }

    Node pre=DummyHead;//前一个结点
    for(int i=0;i<index;i++){//循环遍历到index位置
        pre=pre.next;
    }

    Node retNode=pre.next;//retNode保存要删除的结点
    pre.next=retNode.next;
    retNode.next=null;//设置要删除结点指向空

    size--;

    return retNode.e;//返回删除结点的值
}

/**
 * 删除第一个元素
 */
public E removeFirst(){
    return remove(0);
}

/**
 * 删除最后一个元素
 */
public E removeLast(){
    return remove(size-1);
}

/**
 * 设置输出格式
 */
@Override
public String toString(){
    StringBuilder res=new StringBuilder();
//        Node cur=DummyHead.next;//从当前元素开始遍历
//        while(cur!=null){
//            res.append(cur+"->");
//            cur=cur.next;
//        }

    for(Node cur=DummyHead.next;cur!=null;cur=cur.next){
        res.append(cur+"->");
    }

    res.append("NULL");
    return res.toString();
}
}

测试函数

public class Main {
public static void main(String[] args) {
    LinkedListDummyHead<Integer> linkedListDummyHead=new LinkedListDummyHead<>();
    //依次添加元素
    for(int i=0;i<5;i++){
        linkedListDummyHead.addFirst(i);
        System.out.println(linkedListDummyHead);
    }

    //在位置2添加666
    linkedListDummyHead.add(2,666 );
    System.out.println(linkedListDummyHead);

    //移除最后一个元素
    linkedListDummyHead.removeLast();
    System.out.println(linkedListDummyHead);
}
}

测试结果

0->NULL
1->0->NULL
2->1->0->NULL
3->2->1->0->NULL
4->3->2->1->0->NULL
4->3->666->2->1->0->NULL
4->3->666->2->1->NULL
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值