数据结构之链表操作
Posted hello策
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据结构之链表操作相关的知识,希望对你有一定的参考价值。
对链表的增删改查简单实现
public class LinkedList<E> { private class Node{ public Node next; public E e; public Node(E e){ this.next = null; this.e = e; } public Node(E e, Node node){ this.next = node; this.e = e; } } private Node head; // 头节点 private int size; public void addFirst(E e){ // 先变node.next的值,然后把node的值赋给head. /*Node node = new Node(e); node.next = head; head = node;*/ //OR Node node = new Node(e,head); head = node; size++; } public void addLast(E e){ Node prev = head; Node node = new Node(e); while (prev.next != null){ prev = prev.next; } prev.next = node; size++; } public void add(int index ,E e){ if(index < 0 || index > size){ return; } Node node = new Node(e); if(index == 0){ node.next = head; head = node; size++; }else{ Node prev = head; for (int i = 0; i < index-1; i++) { prev = prev.next; } node.next = prev.next; prev.next = node; size++; } } public E remove(int index){ Node del = new Node(null,null); if(index < 0 || index > size){ return del.e; } Node prev = head; if(index == 0){ del = head; head = del.next; size--; return del.e; }else{ for (int i = 0; i < index - 1; i++) { prev = prev.next; } del = prev.next; prev.next = del.next; del.next = null; size--; return del.e; } } public String toString(){ StringBuffer sb = new StringBuffer(); Node curr = head; for (int i = 0; i < size; i++) { sb.append(curr.e).append(" --> "); curr = curr.next; } sb.append("NULL"); return sb.toString(); } public static void main(String[] args) { LinkedList list = new LinkedList(); list.addFirst(1); System.out.println(list); list.addFirst(2); System.out.println(list); list.addFirst(3); System.out.println(list); list.addLast(4); System.out.println(list); list.addLast(5); System.out.println(list); list.add(1,6); System.out.println(list); list.add(2,7); System.out.println(list); list.add(7,8); System.out.println(list); list.remove(2); System.out.println(list); list.remove(0); System.out.println(list); } }
对上面实现的改进,增加一个虚拟头结点,方便了增删改查的实现
public class LinkedList2<E> { private class Node{ public Node next; public E e; public Node(E e){ this.next = null; this.e = e; } public Node(){ this.next = null; this.e = e; } } private Node dummyHead; private int size; public LinkedList2(){ this.dummyHead = new Node(); this.size = 0; } public void addFist(E e){ /* Node node = new Node(e); node.next = dummyHead.next; dummyHead.next = node; size++;*/ add(0,e); } public void addLast(E e){ /*Node node = new Node(e); Node prev = dummyHead.next; while (prev.next!= null){ prev = prev.next; } prev.next = node; size++;*/ add(size,e); } /** * 添加元素到第index位置 * @param index * @param e */ public void add(int index,E e){ if(index < 0 || index > size){ return; } Node node = new Node(e); Node prev = dummyHead; for (int i = 0; i < index; i++) { prev = prev.next; } node.next = prev.next; prev.next = node; size++; } /** * 得到第index的值 * @param index * @return */ public E get(int index){ if(index < 0 || index > size){ return null; } Node curr = dummyHead.next; for (int i = 0; i < index; i++) { curr = curr.next; } return curr.e; } public E getFirst(){ return get(0); } /** * 移出链表中第index * @param index * @return */ public E remove(int index){ if(index < 0 || index > size){ return null; } Node del = null; Node prev = dummyHead; for (int i = 0; i < index; i++) { prev = prev.next; } del = prev.next; prev.next = del.next; del.next = null; size--; return del.e; } /** * 更改链表中第index个的值 * @param index * @param e */ public void set(int index,E e){ if(index < 0 || index > size){ return; } Node curr = dummyHead.next; for (int i = 0; i < index; i++) { curr = curr.next; } curr.e = e; } public String toString(){ StringBuffer sb = new StringBuffer(); Node curr = dummyHead.next; while (curr!= null){ sb.append(curr.e).append(" --> "); curr = curr.next; } sb.append("NULL"); return sb.toString(); } public static void main(String[] args) { LinkedList2 list = new LinkedList2(); list.addFist(1); System.out.println(list); list.addFist(2); System.out.println(list); list.addFist(3); System.out.println(list); list.addLast(4); System.out.println(list); list.addLast(5); System.out.println(list); list.add(0,6); System.out.println(list); list.add(6,7); System.out.println(list); System.out.println(list.get(1)); System.out.println(list.get(5)); list.remove(1); System.out.println(list); list.remove(5); System.out.println(list); list.addLast(8); System.out.println(list); list.set(0,9); System.out.println(list); } }
以上是关于数据结构之链表操作的主要内容,如果未能解决你的问题,请参考以下文章