单链表----定义、增加节点、删除节点、遍历输出(java)

单链表的结构比较简单,图示如下:
单链表----定义、增加节点、删除节点、遍历输出(java)_第1张图片

单链表有很多个节点构成。每个节点由指针域和数值域构成,指针指向下一个节点的数值域。最后一个节点的指针指向的是NULL。

java实现单链表的一些基本的操作:

package test;
import javax.xml.soap.Node;
/**
 * 单链表的创建和增删改查 
 * @author duola
 */
public class SingleLinkList {
    public  Listnode head;
    public  Listnode current;

    // 添加节点
    public   void addnode(int data) {
        if (head == null) {
            head = new Listnode(data);
            current=head;
        }
        else {
            current.next=new Listnode(data);
            current=current.next;//移位
        }
    }


    // 节点类
    public  class Listnode {
        int value;
        Listnode next;
        Listnode(int x) {
            this.value = x;
        }
    }


    //遍历循环,打印链表的元素
    public  void printList(Listnode l) {
        if(l==null) return;
        current=l;
        if(current!=null){
            System.out.print(current.value+"\t");
            current=current.next;
            printList(current);//递归调用
        }
    }

    // 根据节点的data删除节点(仅仅删除第一个)  
    public void deleteNode(int data) {
        Listnode cur = head;
        Listnode pre=cur;//保存位置

        //找到目标元素的节点位置,cur
        while(cur.value!=data){
            if(cur.next==null){
                return;
            }
            pre=cur;
            cur=cur.next;
        }

        //判断cur的位置与头结点的关系
        if(cur==head){
            head=head.next;}
        else {
            pre.next=cur.next;
        }
    }

    //主方法
    public static void main(String [] args) {

        int[] a={1,2,3,5,6,5,8};
        int[] b={6,4,3,2,1};

        SingleLinkList l1=new SingleLinkList();
        SingleLinkList l2=new SingleLinkList();

        //在链表中插入元素
        for(int i=0;ifor(int j=0;j//删除操作
        l1.deleteNode(5);

        //打印链表的元素
        l1.printList(l1.head);
        System.out.print("\n");
        l2.printList(l2.head);
    }

}

你可能感兴趣的:(数据结构和算法,编程语言)