带头结点的线性表的链式实现

来源:互联网 发布:2016茶叶种类消费数据 编辑:程序博客网 时间:2024/06/15 16:07

复习了顺序实现后,自己模仿着写了链式实现

/** * @author lirui 带头结点的线性表的链式实现 */public class MyLinkList<T> {// 内部类LNode,代表链表结点class LNode {private T data;private LNode next;public LNode() {}public LNode(T data, LNode next) {this.data = data;this.next = next;}}// 保存该链表头节点private LNode headerNode;// 保存该链表尾节点private LNode tailNode;// 链表的长度private int size;// 创建空的带表头的链表public MyLinkList() {headerNode = new LNode();headerNode.data = null;headerNode.next = null;tailNode = headerNode;}// 以指定元素创建链表public MyLinkList(T element) {this();LNode node = new LNode(element, null);headerNode.next = node;size++;tailNode = node;}// 返回链表的长度public int length() {return size;}// 返回索引为index的元素public T get(int index) {if (index < 0 || index > size - 1) {throw new IndexOutOfBoundsException("索引越界");}LNode temp = headerNode;for (int i = 0; i <= index; i++) {temp = temp.next;}return temp.data;}// 根据索引元素,返回索引值public int location(T elementT) {LNode temp = headerNode;int count = -1;while (temp.next != null) {temp = temp.next;count++;if (temp.data == elementT) {return count;}}return -1;}// 向链表中的指定位置插入一个元素public void insert(T elementT, int index) {if (index < 0 || index > size) {throw new IndexOutOfBoundsException("索引越界");}LNode node = new LNode(elementT, null);LNode temp = headerNode;for (int i = 0; i < index; i++) {temp = temp.next;}node.next = temp.next;temp.next = node;// 如果在最后添加,要把为节点变化if (index == size) {tailNode = node;}size++;}// 尾插法建表public void addTail(T elementT) {LNode node = new LNode(elementT, null);tailNode.next = node;tailNode = node;size++;}// 头插法建表public void addHead(T elementT) {LNode node = new LNode(elementT, null);node.next = headerNode.next;headerNode.next = node;size++;if (size == 1) {tailNode = node;}}// 删除链表指定索引处的元素public void delete(int index) {if (index < 0 || index > size - 1) {throw new IndexOutOfBoundsException("索引越界");}LNode temp = headerNode;for (int i = 0; i < index; i++) {temp = temp.next;}temp.next = temp.next.next;// 如果删除的是最后一个元素,尾节点也要变化。if (index == size - 1) {tailNode = temp;}size--;}// 删除最后一个元素public void deleteLast() {delete(size - 1);}// 判断是否为空public Boolean isEmpty() {return size == 0;}// 清空链表public void clear() {headerNode = null;tailNode = null;size = 0;}// 展现链表@Overridepublic String toString() {if (size == 0) {return "[]";} else {StringBuffer sBuffer = new StringBuffer();sBuffer.append("[");LNode temp = headerNode;while (temp.next != null) {temp = temp.next;sBuffer.append(temp.data + ", ");}sBuffer.delete(sBuffer.length() - 2, sBuffer.length());sBuffer.append("]");return sBuffer.toString();}}// 返回表中指定索引上的元素public static void main(String[] args) {MyLinkList<String> linkList = new MyLinkList<>();System.out.println(linkList.isEmpty());linkList.addHead("aaa");System.out.println(linkList.isEmpty());linkList.addHead("bbb");linkList.addHead("bbb");linkList.addTail("ccc");linkList.addTail("ddd");System.out.println(linkList);System.out.println(linkList.get(2));System.out.println(linkList.headerNode.data);System.out.println(linkList.tailNode.data);System.out.println(linkList.length());System.out.println(linkList.location("bbb"));System.out.println(linkList.location("fff"));linkList.insert("kkk", 2);System.out.println(linkList);System.out.println(linkList.size);linkList.insert("ttttt", 6);System.out.println(linkList);System.out.println(linkList.size);System.out.println(linkList.tailNode.data);linkList.delete(5);System.out.println(linkList);System.out.println(linkList.size);System.out.println(linkList.tailNode.data);linkList.deleteLast();System.out.println(linkList);System.out.println(linkList.size);System.out.println(linkList.tailNode.data);linkList.deleteLast();System.out.println(linkList);System.out.println(linkList.size);System.out.println(linkList.tailNode.data);System.out.println(linkList.isEmpty());linkList.clear();System.out.println(linkList);}}


0 0
原创粉丝点击