美文网首页
单向链表实现(java | swift)

单向链表实现(java | swift)

作者: Mr_Bob_ | 来源:发表于2020-07-03 16:23 被阅读0次
    image.png
    Java实现思路
    public class SingleLinkList<E> {
        // 元素数量
        private int size;
        // 头节点
        private Node<E> first;
        static final int ELEMENT_NOT_FOUND = -1;
    
        private static class Node<E> {
            E element;
            Node<E> next;
    
            public Node(E element, Node<E> next) {
                this.element = element;
                this.next = next;
            }
        }
    
        /**
         * 清空
         */
        public void clear() {
            first = null;
            size = 0;
        }
    
        /**
         * 是否为空
         */
        public boolean isEmpty() {
            return first == null;
        }
    
        public int size() {
            return size;
        }
    
        /**
         * get
         * @param index
         * @return
         */
        public E get(int index) {
            return node(index).element;
        }
    
        /**
         * set 返回旧值
         * @param index
         * @param element
         */
        public E set(int index, E element) {
            Node<E> node = node(index);
            E oldElement = node.element;
            node.element = element;
            return oldElement;
        }
    
        /**
         * 尾部添加
         * @param index
         * @param element
         */
        public void add(int index, E element) {
            rangeCheckForAdd(index);
            if (index == 0) {
                first = new Node<E>(element, first);
            } else {
                Node<E> pre = node(index-1);
                pre.next = new Node<E>(element, pre.next);
            }
            size++;
        }
    
        /**
         * 指定索引删除
         */
        public E remove(int index) {
            rangeCheck(index);
            Node<E> node = first;
            if (index == 0) {
                first = first.next;
            } else {
                Node<E> pre = node(index-1);
                node = pre.next;
                pre.next = node.next;
             }
            size --;
            return node.element;
        }
    
        /**
         * 元素返回索引值
         */
        public int indexOf(E element) {
            if (element == null) {
                Node<E> node = first;
                for (int i = 0; i < size; i++) {
                    if (node.element == null) return i;
                    node = node.next;
                }
            } else {
                Node<E> node = first;
                for (int i = 0; i < size; i++) {
                    if (element.equals(node.element)) return i;
                    node = node.next;
                }
            }
            return ELEMENT_NOT_FOUND;
        }
    
        /***
         * 获取index位置的节点对象
         */
        private Node<E> node(int index) {
            rangeCheck(index);
    
            Node<E> node = first;
            for (int i = 0; i < index; i++) {
                node = node.next;
            }
            return node;
        }
    
        /**
         * 检查索引
         */
        private void rangeCheck(int index) {
            if (index < 0 || index >= size) {
                outOfBounds(index);
            }
        }
    
        private void outOfBounds(int index) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
        }
    
        private void rangeCheckForAdd(int index) {
            if (index < 0 || index > size) {
                outOfBounds(index);
            }
        }
    
        @Override
        public String toString() {
    
            StringBuilder string = new StringBuilder();
            string.append("size=").append(size).append(",[");
            Node<E> node = first;
            for (int i = 0; i < size; i++) {
                if (i != 0) string.append(",");
                string.append(node.element);
                node = node.next;
            }
            string.append("]");
            return string.toString();
        }
    }
    
    

    Swift

    import Foundation
    
    class Node<E> {
        var element: E
        var next: Node?
        init(element: E, next: Node?) {
            self.element = element
            self.next = next
        }
    }
    
    struct SingleLinkList<E: Comparable> {
        // 元素数量
        private var size: Int?
        // 头节点
        private var first: Node<E>?
    
        let ELEMENT_NOT_FOUND: Int = -1
        
        var isEmpty: Bool {
            return first == nil
        }
        
        /**
         get
         */
        public func get(index: Int) -> E {
            return node(index: index).element
        }
        
        /**
         set
         */
        public func set(index: Int, newElement: E) -> E {
            let currNode = node(index: index)
            let oldElement: E = currNode.element
            currNode.element = newElement
            return oldElement
        }
        /**
         清空
         */
        mutating public func clear() {
            first = nil
            size = 0
        }
        /**
         尾部添加元素
         */
        mutating public func add(index: Int, newElement: E) {
            rangCheckForAdd(index: index)
            if index == 0 {
                first = Node(element: newElement, next: first)
            } else {
                let pre = node(index: index-1)
                pre.next = Node(element: newElement, next: pre.next)
            }
            size = (size ?? 0) + 1
        }
        /**
         按索引删除
         */
        mutating public func remove(index: Int) -> E {
            rangCheck(index: index)
            let currNode = first!
            if index == 0 {
                first = currNode.next
            } else {
                let preNode = node(index: index-1)
                let tempNode = preNode.next
                preNode.next = tempNode!.next
            }
            size = size! - 1
            return currNode.element
        }
        /**
         按元素返回索引值
         */
        public func indexOf(element: E) -> Int {
            var curNode = first;
            var i = 0
            while i < size! {
                if curNode?.element == element {
                    return i
                }
                curNode = curNode?.next
                i = i + 1
            }
            return ELEMENT_NOT_FOUND
        }
        /**
         获取index位置的节点对象
         */
        private func node(index: Int) -> Node<E> {
            rangCheck(index: index)
            var node = first;
            var i: Int = 0
            while i < index {
                node = node?.next
                i = i + 1
            }
            return node!
        }
        
        private func rangCheck(index: Int) {
            if index < 0 || index >= size! {
                outOfBounds(index: index)
            }
        }
        
        private func rangCheckForAdd(index: Int) {
            if index < 0 || index > size ?? 0 {
                outOfBounds(index: index)
            }
        }
        
        private func outOfBounds(index: Int) {
            print("Index: \(index) , Size = \(String(describing: size))")
        }    
    }
    

    相关文章

      网友评论

          本文标题:单向链表实现(java | swift)

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