单链表和多链表

单链表插入:

下面插入元素

x.next = next;  -------------------------------》s->next=p->next; //把结点p的后继作为结点s的后继;                                          
next = x;                                                   p->next=s

删除元素:p->next=q.next【q就是p的下一个节点】

//链表结构
class Mylist{
    private int date;
    private Mylist next;

    public Mylist(int x){
        date = x;
    }

  //插入元素
    public void  add(Mylist x){

        x.next = next;  //先指向该
        next = x;
    }


//尾部追加元素
    public void append(Mylist x){
        Mylist p = this;
        while(p.next!=null){
            p = p.next;
        }
       p.next = x;
    }

猜你喜欢

转载自blog.csdn.net/SunsirY/article/details/81813560
今日推荐