数据结构之链表
2016-05-03 23:34
274 查看
一、链表ADT定义:
二、链表ADT实现:
package com.java.datastructure.linklist; /** * 链表ADT定义 * @author Ryan Xu */ public interface ILinearList { public int length(); public boolean isEmpty(); public Object get(int index); public Object get(); public Object set(int index, Object obj); public Object set(Object obj); public boolean insert(int index , Object obj); public boolean insert(Object obj); public boolean remove(int index); public boolean remove(); public int indexOf(Object obj); public Object search(Object obj); public Object current(); public Object next(); public void seek(int index); public void seek(int index , int mode); public String toString(); }
二、链表ADT实现:
package com.java.datastructure.linklist; /** * 链表ADT实现 * @author Ryan Xu */ public class MyLinkList implements ILinearList { protected LNode head, current; private int size; // 链表长度,即链表中元素的个数 /** * 外部类构造方法:初始化一个空的单向链表 */ public MyLinkList() { head = current = null; size = 0; } /** * 内部类:用来存放节点 * * @author xuhuanchao */ private static class LNode { private Object element; // 存储节点中的元素 private LNode next; // 在链表中引用下一个节点 /** * 内部类LNode构造方法 * * @param data */ public LNode(Object data) { this.element = data; this.next = null; } } /** * 根据下标,删除指定元素 * * @param index */ public boolean remove(int index) { if (isEmpty()) { // 空表删除,错误返回 return false; } if (index < 0 || index > size) { throw new IndexOutOfBoundsException("Index : " + index + " Size: " + size); } if (index == 1) {// 删除第一个元素节点 head = head.next; size--; return true; } LNode pNode = head; for (int i = 1; i < index - 1; i++) { pNode = pNode.next; } if (pNode.next == null) { return false;// 如果第index个元素不存在,返回false } pNode.next = pNode.next.next; size--; return true; } /** * 删除当前位置元素,当前指针后移,若删除节点是最后节点,则第一个节点成为当前节点 * * @return true/false */ public boolean remove() { if (current == null) { return false; } if (current == head) { // 当前元素为第一个元素 head = head.next; current = head; } else { LNode pNode = head; while (pNode != current) { pNode = pNode.next; } pNode.next = current.next; current = pNode.next; if (current == null) { current = head; } } size--; return true; } /** * 指定位置index之前,插入obj */ public boolean insert(int index, Object obj) { LNode insertNode = new LNode(obj); if (isEmpty()) { head = insertNode; // 空表插入 size++; return true; } if (index < 1 || index > length() + 1) { throw new IndexOutOfBoundsException("Index:" + index + " Size:" + length()); } if (index == 1) { // 插入到头结点之前 insertNode.next = head; head = insertNode; size++; return true; } if (index > length()) { index = length() + 1; } int temp = 0; LNode pNode = head; while (pNode != null && temp < index - 1) { pNode = pNode.next; } if (pNode == null) { return false; // 第index个元素不存在 } insertNode.next = pNode.next; // 中间插入(包含在尾部插入) pNode.next = insertNode; size++; return true; } /** * 将obj插入到表尾 * * @param obj */ public boolean insert(Object obj) { LNode insertNode = new LNode(obj); if (isEmpty()) { head = insertNode;// 空表插入 size++; return true; } LNode pNode = head; while (pNode.next != null) { pNode = pNode.next; } pNode.next = insertNode; size++; return true; } /** * 替换指定位置的数据元素为obj * * @param index * @param obj */ public Object set(int index, Object obj) { if (isEmpty()) { return null; } if (index < 1 || index > length()) { throw new IndexOutOfBoundsException("Index:" + index + " Size:" + length()); } LNode pNode = head; // for(int i=0; i<=index; i++){ // pNode = pNode.next; // } int j = 1; while (pNode.next != null && j < index) { pNode = pNode.next; } Object temp = pNode.element; pNode.element = obj; return temp; } /** * 替换当前位置数据元素为obj,并返回当前位置被替换的元素对象 * * @param obj */ public Object set(Object obj) { if (this.current == null) { return null; } Object temp = this.current.element; this.current.element = obj; return temp; } /** * 得到当前的位置的数据元素,并将当前位置加1 */ public Object get() { if (this.current == null) { return null; } Object object = this.current.element; this.current = current.next; // 当前位置加1 return object; } /** * 得到指定位置(index)的数据元素 * * @param index */ public Object get(int index) { if (isEmpty()) { return null; } if (index < 1 || index > length()) { throw new IndexOutOfBoundsException("Index:" + index + " Size:" + size); } LNode p = head; for (int i = 1; i < index; i++) { p = p.next; } return p.element; } /** * 判断链表是否为空 */ public boolean isEmpty() { return head == null; } /** * 求链表长度 */ public int length() { return size; // 空表,size=0 } /** * 返回对象obj第一次出现的位置,查找不到时,返回-1; * * @param obj */ public int indexOf(Object obj) { if (isEmpty()) { // 空表 return -1; } LNode pNode = head; int j = 1; while (pNode != null) { if (pNode.element.equals(obj)) { return j; } pNode = pNode.next; j++; } return -1; } /** * 返回与对象obj相同关键字的对象,查找不到时返回null * * @param obj */ public Object search(Object obj) { if (isEmpty() || obj == null) { return null; } LNode pNode = head; while (pNode != null) { if (pNode.element.equals(obj)) { return pNode.element; } pNode = pNode.next; } return null; } /** * 返回当前位置的数据元素 */ public Object current() { if (isEmpty() || current == null) { return null; } return current.element; } /** * 返回当前位置的下一个数据元素,并使其成为当前节点 */ public Object next() { if (isEmpty() || current == null || current.next == null) { return null; } current = current.next; return current.element; } /** * 设置index为当前位置 * * @param index */ public void seek(int index) { if (!isEmpty()) { if (0 < index && size > index) { LNode pNode = head; int j = 1; while (pNode != null && j < index) { pNode = pNode.next; j++; } if (pNode != null) { this.current = pNode; } } } } /** * 当mode为0时,同seek(int index)方法;当mode为1时,current相对当前位置移动index个元素 * index为正时,向表尾方向移动,index为负时,向表头方向移动 * * @param index * @param mode */ public void seek(int index, int mode) { if (!isEmpty()) { if (mode == 0) { seek(index); } if (mode == 1 && current != null) { LNode pNode = head; int j = 1; if (pNode != null) { pNode = pNode.next; j++; } seek(index + j); } } } /** * 转换链表为字符串 */ public String toString() { if (isEmpty()) { return ""; } String string = ""; LNode pNode = head; while (pNode != null) { string = string + pNode.element.toString() + ","; pNode = pNode.next; } return string; } }
相关文章推荐