package 链表.单向链表;
import java.util.NoSuchElementException;
/**
* @author 芜情
* 带头结点的单向链表
*/
public class SLinkedList{
//定义链表节点
private static class Node{
private int data;
private Node next;
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
Node(){
}
Node(int e){
data = e;
}
}
//头结点
private Node head;
//链表长度
private int size;
//初始化单向链表
public SLinkedList() {
head = new Node();
head.setNext(null);
size = 0;
}
//判断链表是否为空
public boolean isEmpty(){
return head.getNext() == null;
}
//清空链表
public void clear(){
head.setNext(null);
}
//链表长度
public int size(){
return size;
}
/*============================================*/
/*内部方法,为了好理解,从名字能看出来代码要干什么*/
//判断下标是不是有效
private boolean isValidIndex(int index) {
return index >= 0 && index < size;
}
//判断某个操作的位置是否有效,如执行插入操作时
private boolean isValidPosition(int position) {
return position >= 0 && position <= size;
}
//定义报错信息
private String outOfBoundsMsg(int index) {
return "Index: "+index+", Size: "+size;
}
private void checkIndex(int index) {
if (!isValidIndex(index))
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private void checkPosition(int position) {
if (!isValidPosition(position))
throw new IndexOutOfBoundsException(outOfBoundsMsg(position));
}
//返回指定下标节点
private Node node(int index){
Node currentNode = head;
int i = 0;
while(i <= index){
currentNode = currentNode.getNext();
++i;
}
return currentNode;
}
/*============================================*/
/**
* 增加节点
*/
//链接首节点
private void linkFirst(Node node) {
final Node temp = head.getNext();
head.setNext(node);
node.setNext(temp);
++size;
}
//由于单向链表,得知道要插入位置节点的前节点,进行链接
private void link(Node prev, Node newNode){
newNode.setNext(prev.getNext());
prev.setNext(newNode);
++size;
}
//在中间插入节点
public void add(int position, int e){
checkPosition(position);
Node newNode = new Node(e);
if(position == 0){
linkFirst(newNode);
}else{
link(node(position - 1), newNode);
}
}
//插入首节点
public void addFirst(int e){
Node newNode = new Node(e);
linkFirst(newNode);
}
//插入尾节点
public void addLast(int e){
Node newNode = new Node(e);
link(node(size - 1), newNode);
}
public void add(int e){
addLast(e);
}
/**
* 删除节点
*/
//解除首节点链接
private int unlinkFirst(Node f) {
final int element = f.getData();
head.setNext(f.getNext());
--size;
return element;
}
//解除中间结点链接
private int unlink(Node prev, Node node) {
final int element = node.getData();
prev.setNext(node.getNext());
--size;
return element;
}
//删除中间的一个节点
public int remove(int index){
checkIndex(index);
if(index == 0){
return unlinkFirst(node(0));
}else{
return unlink(node(index - 1),node(index));
}
}
//删除首节点,返回被删除的元素
public int removeFirst(){
final Node f = head.getNext();
if(f == null){
throw new NoSuchElementException();
}
return unlinkFirst(f);
}
public int remove(){
return removeFirst();
}
//删除尾节点
public int removeLast(){
final Node f = head.getNext();
if(f == null){
throw new NoSuchElementException();
}
if(f.getNext() == null){
return unlinkFirst(f);
}else{
return unlink(node(size - 2), node(size - 1));
}
}
/*链表主要以增删节点的操作比较重要,改和查就不写了*/
}
【数据结构】单向链表
最新推荐文章于 2024-08-28 21:00:54 发布