栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java 实现双向链表

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java 实现双向链表

class linkedList {
    public int val;
    public linkedList prev;
    public linkedList next;
    public linkedList(int val) {
        this.val = val;
    }
}
public class DoublelinkedList {
    public linkedList head;
    public linkedList last;

    //return the length of the DoublelinkedList
    public int size() {
        linkedList cur = this.head;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    //Print the value of every node in the DoublelinkedList
    public void display() {
        linkedList cur = this.head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //Check whether the DoublelinkedList contains the given value
    public boolean containsValue(int val) {
        linkedList cur = this.head;
        while (cur != null) {
            if (cur.val == val) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //Push a node at the first of the DoublelinkedList
    public void push_font(int val) {
        linkedList node = new linkedList(val);
        if (head == null) {
            this.head = node;
            this.last = node;
        } else {
            this.head.prev = node;
            node.next = this.head;
            this.head = node;
        }
    }

    //Push a node at the last of the DoublelinkedList
    public void push_back(int val) {
        linkedList node = new linkedList(val);
        if (this.last == null) {
            this.head = node;
            this.last = node;
        } else {
            this.last.next = node;
            node.prev = this.last;
            this.last = node;
        }
    }

    //Pop the last node of the DoublelinkedList
    public void pop_back() {
        if (this.head == null) {
            System.out.println("链表为空!无法删除!");
        } else {
            int lastValue = this.last.val;
            if (this.head == this.last) {
                this.head = null;
                this.last = null;
            } else {
                linkedList tmp = this.last.prev;
                this.last.prev.next = null;
                this.last.prev = null;
                this.last = tmp;
            }
        }
    }

    //Pop the first node of the DoublelinkedList
    public void pop_font() {
        if (this.head == null) {
            System.out.println("链表为空!无法删除!");
        } else {
            int firstValue = this.head.val;
            if (this.head == this.last) {
                this.head = null;
                this.last = null;
            } else {
                linkedList tmp = this.head.next;
                this.head.next.prev = null;
                this.head.next = null;
                this.head = tmp;
            }
        }
    }

    //Return the value of the node at the specific position
    public int searchByIndex(int index) {
        if (this.head == null) {
            System.out.println("链表为空!");
            return -1;
        }
        if (index < 0 || index >= this.size()) {
            System.out.println("index 位置不合法!");
            return -1;
        } else {
            linkedList cur = this.head;
            while (index > 0) {
                cur = cur.next;
                index--;
            }
            return cur.val;
        }
    }

    //Insert a node at the DoublelinkedList
    public void Insert(int index, int val) {
        if (index < 0 || index > this.size()) {
            System.out.println("index 位置不合法!");
            return;
        }
        linkedList node = new linkedList(val);
        linkedList cur = this.head;
        if (index == 0) {
            this.push_font(val);
        } else if (index == this.size() - 1) {
            this.push_back(val);
        } else {
            while (index > 0) {
                cur = cur.next;
                index--;
            }
            node.next = cur.next;
            cur.next.prev = node;
            node.prev = cur;
            cur.next = node;
        }
    }

    //Remove the first node with the specific value of the DoublelinkedList
    public void removeByValue(int val) {
        linkedList cur = this.head;
        while (cur != null) {
            if (cur.val == val) {
                if (cur == head) {
                    this.head = this.head.next;
                    if (this.head != null) {
                        this.head.prev = null;
                    } else {
                        this.last = null;
                    }
                    return;
                } else {
                    cur.prev.next = cur.next;
                    if (cur == this.last) {
                        this.last = cur.prev;
                    } else {
                        cur.next.prev = cur.prev;
                    }
                    return;
                }
            } else {
                cur = cur.next;
            }
        }
    }

    //Remove all nodes with the specific value of the DoublelinkedList
    public void removeAllByValue(int val) {
        linkedList cur = this.head;
        while (cur != null) {
            if (cur.val == val) {
                if (cur == head) {
                    this.head = this.head.next;
                    if (this.head != null) {
                        this.head.prev = null;
                    } else {
                        this.last = null;
                    }
                    cur = cur.next;
                } else {
                    cur.prev.next = cur.next;
                    if (cur == this.last) {
                        this.last = cur.prev;
                        this.last.next = null;
                    } else {
                        cur.next.prev = cur.prev;
                    }
                    cur = cur.next;
                }
            } else {
                cur = cur.next;
            }
        }
    }

    //Clear the DoublelinkedList
    public void clear() {
        linkedList curNext = this.head;
        while (head != null) {
            curNext = curNext.next;
            this.head.next = null;
            this.head.prev = null;
            this.head = curNext;
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/763694.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号