/**
基本数据结构之链表
链表:一种常见的基础数据结构,是一种线性表,但是并不会按线性的顺序存储数据;而是在每一个节点里存的是下一个节点的指针
链表与数据:(线性数据结构)
数据适合查找,遍历,固定长度
链表适合插入,删除,不宜过长,否则会导致遍历性能下降
*/
public class Test37 {
public static void main(String[] args) {
NodeMananger nm = new NodeMananger();
System.out.println("----------add----------");
nm.add(5);
nm.add(4);
nm.add(3);
nm.add(2);
nm.add(1);
nm.print();
System.out.println("----------del----------");
nm.del(2);
nm.print();
System.out.println("----------find----------");
System.out.println(nm.find(0));
System.out.println("----------update----------");
System.out.println(nm.update(1,10));
nm.print();
System.out.println("----------insert----------");
nm.insert(0,15);
nm.print();
}
}
class NodeMananger{
private Node root; //链表的根节点;这里root其实也可以表示为内部类的对象实例
//数据的方法是在内部类里面,所以外部类也要提供方法给外部的成员使用,故提供的方法其实是调用内部类的各个方法
public void add(int data){
if(root==null){
root = new Node(data);
}else{
root.addNode(data);
}
}
//打印所有
public void print(){
if(root!=null){ //判断root根节点对象不为空时才会打印啊
System.out.print(root.getData()+"->");
root.printNode();
System.out.println();
}
}
//删除
public void del(int data){
if(root!=null){
if(root.getData()==data){
root = root.next;
}else{
root.delNode(data);
}
}
}
//查找是否存在
public boolean find(int data){
if(root!=null){
if(root.getData()==data){
return true;
}else{
return root.findNode(data);
}
}
return false;
}
//更新
public boolean update(int oldData,int newData){
if(root==null)return false;
if(root.getData()==oldData){
root.setData(newData);
return true;
}else{
return root.updateNode(oldData,newData);
}
}
//插入
int current = 0;
public void insert(int index,int data){
if(index<0)return;
if(index==current){ //从第一个索引判断,
Node newdata = new Node(data);
newdata.next=root; //往前插入,如果插入后那么就是新数的下一个是root了,
root=newdata; //然后再恢复顺序,即第一个重新定为root
}else{
root.insertNode(index,data); //第一个索引不满足即调度用内部类方法执行递归
}
}
//成员内部类存储数据
//谁提供方法?原则:谁拥有数据谁就提供方法,所以是内部类提供方法
private class Node{
private int data;
private Node next; //把当前类型作为属性;所以next也可看作是对象,和上面root一个意思
public Node(int data){ //内部类的带参构造方法,初始化类属性
this.data=data;
}
public void setData(int data){
this.data=data;
}
public int getData(){
return data;
}
//添加方法
public void addNode(int data){
if(this.next==null){ //第一遍判断时,this代表的是root,判断root的下一个对象就是root.next,所以写成this.next
this.next=new Node(data);
}else{
this.next.addNode(data); //递归去判断,root的下一个节点this.next再判断此时的下一个节点所以再调用addNode()
}
}
//删除方法
public void delNode(int data){
if(this.next!=null){
if(this.next.getData()==data){ //也可以写成if(this.next.data==data)因为该delNode和getNode是同一个类中的方法,可以是自己调用属性
this.next=this.next.next;
}else{
this.next.delNode(data);
}
}
}
//输出所有节点
public void printNode(){
if(this.next!=null){ //因为root根节点(即第一个对象)在外部已经做了判断,所以再内部类里只从第二个对象开始判断即可
System.out.print(this.next.data+"->"); //不为空时才会有输出,直接输出的是data即类的属性,这里在前面的addNode里已经为data做了初始化的操作:this.next=new Node(data);所以可以直接调用
this.next.printNode();
}
}
//查找节点是否存在
public boolean findNode(int data){
if(this.next!=null){ //当查找的不存在时如何判断的?当最后一个节点对象经过循环后仍不满足下发的if条件,所以就又执行了一次this.next.findNode(data)
//此时再返回调用方法时this.next就表示最后一个的再下一个,则此时最后一个的再下一个没有了为Null,所以就return false了,也就满足当查找不存在的时候就返回false
if(this.next.data==data){
return true;
}else{
return this.next.findNode(data);
}
}
return false;
}
//更新节点
public boolean updateNode(int oldData,int newData){
if(this.next==null)return false;
if(this.next.data==oldData){
this.next.data=newData;
return true;
}else{
return this.next.updateNode(oldData,newData);
}
}
//插入节点
public void insertNode(int index,int data){
current++;
if(index==current){
Node newdata = new Node(data);
newdata.next=this.next;
this.next=newdata;
}else{
this.next.insertNode(index, data); //自己调用自己的方法实现递归
}
}
}
}
基本数据结构之链表
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 引言 创建顺序表时必须分配一块连续的内存存储空间,而当顺序表内部数组的容量不足时,则必须创建一个新的数组,然后把原...
- 一、首先介绍一下数据在内存中的存储(针对于c/c++): 1、栈区[stack]:由编译器自动分配释放,存放函数的...