JAVA数据结构---单链表
2016-03-05 21:55
309 查看
节点类:
package 自定义链表;
public class Node {
}
链表类:
package 自定义链表;
public class LinkList {
}
测试类:
package 自定义链表;
public class Test {
}
package 自定义链表;
public class Node {
protected int data; protected Node nextNode; public Node(int data) { super(); this.data = data; } public void printData() { System.out.print(data + " "); } public int getData() { return data; } public void setData(int data) { this.data = data; } public Node getNextNode() { return nextNode; } public void setNextNode(Node nextNode) { this.nextNode = nextNode; }
}
链表类:
package 自定义链表;
public class LinkList {
private Node head; private int index = 0; private int length = 0; // 初始化链表为null public LinkList() { this.head = null; } // 链表的长度 public int length() { return length; } // 打印链表的元素 public void printAllNode() { Node newnode = head; while (newnode != null) { newnode.printData(); System.out.print(" "); newnode = newnode.nextNode; } System.out.println(); } // 设置链表的头部(前追加) public void setHead(int x) { Node newnode = new Node(x); newnode.nextNode = head; head = newnode; length++; } // 追加到链表 @SuppressWarnings("unused") public void append(int x) { Node newnode = new Node(x); Node node_past = head; Node node_now = head; while (node_now.nextNode != null) { node_past = node_now; node_now = node_now.nextNode; } node_now.nextNode = newnode; newnode.nextNode = null; length++; } // 插入操作,除了第一个位置外的位置 public void insert(int position, int x) throws Exception { if (position < 0 || position > length) { throw new Exception("插入位置错误."); } Node newnode = new Node(x); Node node_past = head; Node node_now = head; while (index != position) { node_past = node_now; node_now = node_now.nextNode; index++; } if (index == 0) { newnode.nextNode = head.nextNode; head = newnode; } else { node_past.nextNode = newnode; newnode.nextNode = node_now; } index = 0; length++; } // 删除头部 public void deleteHead() { head = head.nextNode; length--; } // 删除尾部 public void deleteRear() { Node node_temp = head; while (index != length - 2) { node_temp = node_temp.nextNode; index++; } index = 0; node_temp.nextNode = null; length--; } // 删除byindex @SuppressWarnings("unused") public void deleteByIndex(int position) throws Exception { if (position < 0 || position > length) { throw new Exception("插入位置错误."); } Node newnode = head; Node node_past = head; Node node_now = head; while (index != position) { index++; node_past = node_now; node_now = node_now.nextNode; } if (node_now == head) { head = head.nextNode; } else if (node_now == null) { node_past.nextNode = null; } else { node_past.nextNode = node_now.nextNode; index = 0; } length--; } // 改变byindex public void changeByIndex(int position, int x) throws Exception { if (position < 0 || position > length) { throw new Exception("插入位置错误."); } Node node_temp = head 4000 ; while (index != position) { node_temp = node_temp.nextNode; index++; } index = 0; node_temp.setData(x); } // 查询findbyData public int findByData(int x) { int ret = 0; Node node_temp = head; while (node_temp.getData() != x) { node_temp = node_temp.nextNode; index++; } ret = index; index = 0; return ret; } // 查询getbyIndex public int getByIndex(int position) { Node node_temp = head; while (index != position) { node_temp = node_temp.nextNode; index++; } index = 0; return node_temp.getData(); }
}
测试类:
package 自定义链表;
public class Test {
public static void main(String[] args) throws Exception { // TODO Auto-generated method stub LinkList newList = new LinkList(); newList.setHead(100); newList.setHead(1000); newList.append(10); newList.append(20); newList.insert(2, 999); newList.insert(1, 3434444); newList.printAllNode(); System.out.println(newList.length()); newList.insert(0, 999999); newList.printAllNode(); System.out.println(newList.length()); }
}
相关文章推荐
- JAVA数据结构---单链表
- JAVA数据结构---单链表
- JAVA数据结构---单链表
- 数据结构之堆栈与队列
- 《数据结构》一般线性表的合并
- [数据结构] 表达式求值(转化为后缀表达式再求值或直接求值)
- 数据结构之线性表
- c语言实现拓扑排序(《数据结构》算法7.12)
- 数据结构课程上机实验题(一)
- 数据结构和算法
- 数据结构——算法之(010)( 字符串的左旋转操作)
- 7-1-图、表的数组(邻接矩阵)表示法-图-第7章-《数据结构》课本源码-严蔚敏吴伟民版
- 树链剖分(详解)
- HDU3966(树链剖分)
- 《数据结构》双向链表的基本操作
- 大一学生数据结构与算法的先后取舍
- 网页爬虫及其用到的算法和数据结构
- 排序算法-堆排序
- 数据结构-栈和队列
- 常用的数据结构