单链表的各种操作

来源:互联网 发布:win8数据恢复软件 编辑:程序博客网 时间:2024/05/25 13:33

首先定义一个节点的数据结构

package ListNode;//定义一个节点数据结构,包括指针和数据public class Node {    public Node next=null;    public int data;    public Node(int data){        this.data=data;    }    public void display(){        System.out.print(data+" ");    }}

链表的各种操作

package ListNode;//注意node.next指的也是一个节点,可以直接赋值给另一个节点public class LinkList {    public Node first=null;//定义一个头节点,初始为空,    //注意这个头结点只是在这里为空,后面凡是把这个first赋值给其他节点,都是正真的头结点,可能为空,也可能不是。    public int pos=0;//节点位置    //添加头结点,把数据data添加到头结点    public void addFirstNode(int data){        Node node=new Node(data);//创建一个数据区为data的节点        node.next=first;        first=node;    }    //删除头结点,并返回头结点    public Node deleteFirstNode(){        if(first==null){            System.out.println("链表为空!");        }        Node tempNode=first;//创建一个临时节点tempNode,将头结点赋值给tempNode        first=tempNode.next;//再把tempNode的下一个节点赋值给头结点        return tempNode;        }    //在位置index后面插入节点,节点的数据区为data,头结点位置为1    public void add(int data,int index){        Node node=new Node(data);        Node current=first;//指向头结点,头结点可能不为空        Node previous=first;//指向头结点,头结点可能不为空        while(pos!=index){            previous=current;            current=current.next;            pos++;        }        node.next=current;        previous.next=node;        pos=0;    }    //展示所有节点    public void displayAllNodes(){        Node current=first;//指向头结点,头结点可能不为空        while(current!=null){            current.display();            current=current.next;        }        System.out.println();       }    //查找数据区为data的节点,并返回该节点    public Node findByData(int data){        Node current=first;        while(current.data!=data){            if(current.next==null){                return null;            }            current=current.next;        }        return current;    }    //查找在index位置的节点,并返回该节点    public Node findByIndex(int index){        Node current=first;        while(pos!=index){            current=current.next;            pos++;        }        pos=0;        return current;    }    //删除在index位置的节点,并返回该节点,头结点的位置为0    public Node deleteByIndex(int index){        Node current=first;        Node previous=first;        while(pos!=index){            previous=current;            current=current.next;            pos++;        }        if(current==first){//如果删除的是头结点            first=first.next;        }else{            previous.next=current.next;            pos=0;        }        return current;     }    //删除数据区为data的节点    //注意删除的时候一定要判断删除的位置,是否是头结点,是否是尾节点,    public void deleteByData(int data){        Node current=first;        Node previous=first;        if(first.data==data){//删除的是头结点            first=first.next;        }        while(current.next!=null){//删除除头尾节点的一般节点            if(current.data==data){                previous.next=current.next;            }else{                previous=current;            }            current=current.next;        }        if(current.next==null){//删除尾节点            if(current.data==data){                previous.next=null;            }                   }    }}

测试

package ListNode;public class Test {    public static void main(String[] args) {        LinkList linkList=new LinkList();        linkList.addFirstNode(12);        linkList.addFirstNode(10);        linkList.addFirstNode(19);        linkList.addFirstNode(18);        linkList.addFirstNode(15);        linkList.addFirstNode(18);        linkList.addFirstNode(40);        linkList.addFirstNode(18);        linkList.displayAllNodes();        linkList.add(30,2);        linkList.displayAllNodes();        linkList.deleteFirstNode();        linkList.displayAllNodes();        linkList.deleteByIndex(5);        linkList.displayAllNodes();        linkList.deleteByIndex(3);        linkList.displayAllNodes();        Node resultNode1 = linkList.findByData(18);        System.out.println(resultNode1.data);        Node resultNode2=linkList.findByIndex(4);        System.out.println(resultNode2.data);        linkList.deleteByData(18);        linkList.displayAllNodes();         }}
0 0
原创粉丝点击