`
杨杨和花花
  • 浏览: 21731 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

关于链表的一些基本操作

阅读更多
今天匆忙之中就快速展示我的链表一些基本操作,包括增加,删除,插入,查找,求长度。
public class ListTest {
//定义几个全局变量
public static  Node front=null;//第一个结点
public static  Node last=null;//最后一个结点
public static void main(String args[]){
ListTest test=new ListTest();
//调用链表里的方法
for(int i=0;i<10;i++){
test.add("结点"+i);
}
  
//调用第一个递归方法
test.NodePrint1(front);

//调用插入的方法
Node node=new Node();
node.obj="插入的新9结点";
test.Nodecharu(node,3);

  
//调用第一个递归方法
test.NodePrint1(front);

//调用删除方法
test.DeletNode(2);

    //调用第一个递归方法
        test.NodePrint1(front);

     //获取当前链表的长度
int t=test.GetLength();
System.out.println("当前链表的长度为:"+t);

Node node2=new Node();
node2.obj="插入的新9结点";
int k=test.LookNode(node2);
System.out.println("找到该结点的位置为"+k);
}
/*
* 增加的方法
* obj为你添加的数
*/
public void add(Object obj){
//创建一个新的结点
Node fristnode=new Node();
fristnode.obj=obj;
if(front==null){
front=fristnode;//把创建的新结点赋给第一个结点
last=front;//第一个结点和最后一个结点相等
}else{
//增加肯定在链表尾增加
             last.child=fristnode;//最后一个结点指向下一个新结点
             fristnode.parent=last;
             last=fristnode;//把最后一个结点记为新的last
}
}
/**
* 打印的方法,这个为从左到右遍历
*/
public void NodePrint1(Node root){
if(root!=null){
//取出该结点元素
Object data=root.obj;
//取出下一个结点进行递归
System.out.println("结点元素为:"+data);
Node node1=new Node();
    node1=root.child;
NodePrint1(node1);
}
}
/**
* 这个为从右到左的遍历
*/
public void NodePrint2(Node root){
if(root!=null){
//取出该结点元素
Object data=root.obj;
//取出该结点上一个结点进行递归
System.out.println("结点元素为"+data);
Node node2=new Node();
    node2=root.parent;
NodePrint2(node2);
}

}
/**
* 获得当前链表的长度
*/
public int GetLength(){
int count=0;
     Node node3=new Node();
     node3=front;
while(node3!=null){
node3=node3.child;
count++;
   }
return count;
}
/**
* 插入的方法
* node 为你插入的结点,i为你插入的位置
*/
public void Nodecharu(Node node,int i){
if(i<0||i>this.GetLength()){
throw new java.lang.RuntimeException("该位置不存在");
}else{
if(i==0){
front=node;
}else{
    //创建一个新的结点来记录插入的结点
Node fristnode=new Node();
        fristnode=front;
//找的要插入位置的结点
for(int j=1;j<i;j++){
     fristnode=fristnode.child;
     }
//创建一个新的结点来记录插入结点的上一个结点
Node secondnode=new Node();
secondnode=fristnode.parent;

//建立关系
node.child=fristnode;
fristnode.parent=node;

node.parent=secondnode;
secondnode.child=node;
}
}
}
/**
* 删除的方法
* i为你删除的位置
*/
public void DeletNode(int i){
if(i<1||i>this.GetLength()){
throw new java.lang.RuntimeException("该位置不存在");
}else{
//创建一个新的结点来记录插入的结点
Node DeletNode=new Node();
DeletNode=front;
if(i==1){
front=DeletNode.child;
}else{
    //找的要删除位置的结点
for(int j=1;j<i;j++){
    DeletNode= DeletNode.child;
   }
//找到父结点
Node DeletParent=new Node();
DeletParent=DeletNode.parent;
if(i==this.GetLength()){
DeletParent.child=null;
}else {
//找到子结点
Node DeletChild=new Node();
DeletChild=DeletNode.child;

//建立相互关系
DeletParent.child=DeletChild;
DeletParent=DeletChild.parent;

}

    }
}
}

/**
* 查找的方法
*/
public int LookNode(Node node){
//创建一个整数来记录你查到的位置
int k=1;
//创建一个结点
Node renode=new Node();
renode=front;
while(renode.obj!=node.obj){
k++;
renode=renode.child;
}
return k;
}
}
代码仅供参考,其实尚且有些毛病,还需改进,细心的人会发现的。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics