package first;
public class LinkTable {
private LinkNode head = null;
private int counts = 0;
/**
* 在链头给链表增加结点
**/
public void insertFront(int d) {
if (head == null) {
head = new LinkNode();
}
LinkNode n = new LinkNode(); //定义新的链表结点,并将数据赋给新结点
n.setData(d);
if (head.getNext() == null) { //如果头结点的后继无结点,注意头结点中无数据
head.setNext(n);
} else {
n.setNext(head.getNext()); //如果头结点的后继结点存在
head.setNext(n);
}
counts++; //结点数据增加1
}
/**
* 在链中问给链表增加结点
**/
public void insertMidle(int position, int d) {
if (position < 0 || position > counts) {
System.out.println("不存在该结点!!!");
}
LinkNode temp = searchByPosition(position); //查并返回结点
LinkNode n = new LinkNode();
n.setData(d);
n.setNext(temp.getNext());
temp.setNext(n);
counts++;
}
/**
* 在链尾给链表增加结点
**/
public void insertback(int d) {
LinkTable的简单实现
最新推荐文章于 2023-04-01 16:05:32 发布
本文介绍了一个简单的链表实现,包括在链头、链中和链尾插入结点,按位置删除结点,以及遍历链表的方法。通过示例展示了链表操作的具体步骤和运行结果。
摘要由CSDN通过智能技术生成