/**
* 单向链表
*/
public class SingleLink {
//定义一个首元结点
private Node head;
//定义一个size表示链表中结点的个数
private int size;
public Node getHead() {
return head;
}
public void setHead(Node head) {
this.head = head;
}
public void setSize(int size) {
this.size = size;
}
public SingleLink(Node head, int size) {
this.head = head;
this.size = size;
}
public SingleLink(Object data, Node next, int size) {
this.head = new Node(data, next);
this.size = size;
}
//向链表中的第i个位置插入一个元素
public void insert(int i, Object e) {
Node newNode = new Node(e, null);
//判断i是否下标越界
if (i < 0 || i > getSize()) {
throw new IndexOutOfBoundsException("下标越界");
}
//判断头节点是否为空
if (head == null) {
head = newNode;
} else {
//判断链表插入位置是否是首元节点之前
if (i == 0) {
newNode.next = head;
head = newNode;
} else {
Node pNode = head;
for (int j = 0; j < i - 1; j++) {
pNode = pNode.next;
}
//此时pNode是链表中的第i个节点
//我们选择在第i个节点之后插入
if (null == pNode.next) {
pNode.next = newNode;
} else {
newNode.next = pNode.next;
pNode.next = newNode;
}
}
}
size++;
}
//获取链表中节点的个数
public int getSize() {
return size;
}
//获取第一个数据域为e的结点的下标
private int getIndex(Object e) {
Node pNode = head;
for (int i = 0; i < size; i++) {
if (e == null && pNode.data == null) {
return i;
}
if (e != null && pNode.data.equals(e)) {
return i;
}
pNode = pNode.next;
}
return -1;
}
//判断链表是否为空
private boolean isEmpty() {
if (size > 0) {
return false;
} else {
return true;
}
}
//判断是否包含某个元素
private boolean isContains(Object e) {
if (getIndex(e) != -1) {
return true;
} else {
return false;
}
}
//移除data是e的第一个节点
private void delete(Object e) {
Node pNode = head;
Node pre = head;
for (int i = 0; i < size; i++) {
if (e == null && pNode.data == null) {
pre.next = pNode.next;
size--;
return;
}
if (e != null && pNode.data.equals(e)) {
pre.next = pNode.next;
size--;
return;
}
pre = pNode;
pNode = pre.next;
}
}
//移除下标是i的结点
private Object delete(int i) {
Node pNode = head;
if (i < 0 || i > size) {
throw new IndexOutOfBoundsException("下标越界");
}
if (i == 0) {
Object old = head.data;
head = head.next;
size--;
return old;
}
for (int j = 0; j < i - 1; j++) {
pNode = pNode.next;
}
//执行到这里,pNode是第i个节点的前一个节点
Object old = pNode.next;
pNode.next = pNode.next.next;
size--;
return old;
}
//获取下标是i的结点
private Node getI(int i) {
Node pNode = head;
if (i < 0 || i > size) {
throw new IndexOutOfBoundsException("下标越界");
}
if (i == 0) {
return pNode;
}
for (int j = 0; j < i; j++) {
pNode = pNode.next;
}
return pNode;
}
//修改(替换)下标为i的结点的data
public Object update(int i, Object e) {
Node pNode = getI(i);
Object old = pNode.data;
pNode.data = e;
return old;
}
//重写toString方法
@Override
public String toString() {
Node pNode = head;
StringBuffer sb = new StringBuffer();
sb.append("[");
while (pNode != null) {
sb.append(pNode.data);
if (pNode.next != null) {
sb.append(",");
}
pNode = pNode.next;
}
sb.append("]");
return sb.toString();
}
//链表的逆序
public Node reversed(){
if (isEmpty()){
return null;
}
return reversed(head.next);
}
public Node reversed(Node pNode){
if (pNode.next == null){
head.next = pNode;
pNode.next = null;
} else {
reversed(pNode.next).next = pNode;
pNode.next = null;
}
return pNode;
}
//定义一个内部类来表示链表中的结点
public static class Node {
public Object data;
public Node next;
public Node(Object data, Node next) {
this.data = data;
this.next = next;
}
}
}
数据结构之链表--单向链表
最新推荐文章于 2024-11-04 13:52:23 发布