单链表的方法和测试

单链表的测试

/**
 * 单链表的使用:
 * 单链表由节点组成
 * @author wgsstart
 * @creat 2021-03-04 16:24
 */
public class Test {
    public static void main(String[] args) {
        MyLinkedList myLinkedList = new MyLinkedList();
        myLinkedList.addLast(10);
        myLinkedList.addLast(12);
        myLinkedList.addLast(12);
        myLinkedList.addLast(13);
        myLinkedList.addLast(16);
        myLinkedList.display();
        myLinkedList.clear();
        System.out.println("hhhhhhhhhhh");
       /* boolean flag = myLinkedList.contains(11);
        System.out.println(flag);
        System.out.println(myLinkedList.size())*/
      /* myLinkedList.display();
        System.out.println();
       myLinkedList.addIndex(0,12);
       myLinkedList.display();
        System.out.println();*/
    /*   myLinkedList.remove(12);
       myLinkedList.display();
       */
    /*myLinkedList.removeAllKey(12);
    myLinkedList.display();*/
    }
}


单链表的方法

/**
 * this代表当前对象的引用。
 * 引用:属性/方法
 *  实例成员变量
 *  静态成员变量
 *  this()
 *  this.data
 *  this.func()
 * @author wgsstart
 * @creat 2021-03-04 17:09
 */
class Node{
    public int data;//两个属性默认值为0
    public Node next;//默认值null
    public Node(int data){//构造方法
        this.data = data;
        this.next = null;//先置为空,可以方便下面放在任意处,可以灵活。
    }
}
public class MyLinkedList {
    public Node head;//保存单链表的头节点的引用
    //头插法
    public void addFirst(int data){
        Node node = new Node(data);//这一行代码之后,我们就已经有了这么一个节点了
        if(this.head == null){
            //第一次插入节点
            this.head = node;
            return;
        }
        node.next = this.head;
        this.head = node;
    }
    //打印单链表
    public void display(){
        Node cur = this.head;
        while(cur!=null){
            System.out.print(cur.data+" ");
            cur = cur.next;
        }
    }
    //尾插法
    public void addLast(int data){
        Node node = new Node(data);
        if(this.head == null){//如果是第一次插入的话
            this.head = node;
            return;
        }
        Node cur = this.head;
        while(cur.next!=null ){
            cur = cur.next;
        }
       cur.next = node;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        Node cur = this.head;
        while(cur!=null){
            if(cur.data == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //得到单链表的长度
    public int size(){
        Node cur = this.head;
        int count = 0;
           while(cur!=null){
            count++;
            cur = cur.next;
       }
        return count;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        if(index == 0){//头插法
            addFirst(data);
            return;
        }
        if(index == this.size()){//尾插法
            addLast(data);
            return;
        }
        //先找到 index位置的前一个节点的地址
       Node cur =  searchIndex(index);
        //进行插入
        Node node = new Node(data);
        node.next = cur.next;
        cur.next = node;
    }
    private Node searchIndex(int index){
        //1.对index进行合法性检查
        if(index < 0||index > this.size()){
            throw new RuntimeException("index位置不合法!");
        }
        Node cur = this.head;//index-1
       while(index-1 !=0){
           cur = cur.next;
           index--;
       }
       return cur;
    }
    private Node searchPrev(int key){//找前驱,如果找到了返回前驱,找不到返回null;
        Node prev = this.head;
        while(prev.next!=null){
            if(prev.next.data == key){
                return prev;
            }else {
                prev = prev.next;
            }
        }
        return null;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(this.head == null){
            return;
        }
        if(this.head.data == key){//这种情况直接将head往后挪
            this.head = this.head.next;
            return;
        }
        Node prev = searchPrev(key);//来找当前key的前驱
        if(prev == null){
            System.out.println("根本没有这个节点!");
            return;
        }
        Node del = prev.next;
        prev.next = del.next;
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        Node prev = this.head;
        Node cur = this.head.next;//
        while(cur!=null){
            if(cur.data == key){
                prev.next = cur.next;
                cur = cur.next;
            }else{
                prev = cur;
                cur = cur.next;
            }
        }
        if(this.head.data == key){
            this.head = this.head.next;
        }
    }

    public void clear(){
        this.head = null;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C语言中,单链表的反转是一个常见的数据结构操作,通常通过迭代或递归的方式来实现。这里我会介绍一种常见的迭代方法: **迭代法(Node指针):** ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构体 typedef struct Node { int data; struct Node* next; } Node; // 反转链表函数 Node* reverseList(Node* head) { if (head == NULL || head->next == NULL) { return head; } Node* prev = NULL; // 初始化前驱指针 Node* curr = head; // 初始化当前指针 // 遍历链表,每次将当前节点的next指向前驱节点 while (curr != NULL) { Node* temp = curr->next; // 保存当前节点的下一个节点 curr->next = prev; // 将当前节点的next指向前驱 prev = curr; // 前驱指针前进 curr = temp; // 当前指针前进到下一个节点 } return prev; // 最终返回新的头节点 } // 测试反转操作 void printList(Node* head) { while (head != NULL) { printf("%d -> ", head->data); head = head->next; } printf("NULL\n"); } int main() { // 创建一个测试链表 Node* head = createLinkedList(); // 假设createLinkedList()是创建链表的函数 printList(head); // 打印原始链表 head = reverseList(head); // 反转链表 printList(head); // 打印反转后的链表 return 0; } ``` **相关问题:** 1. 迭代法和递归法在链表反转中的区别是什么? 2. 如果链表中有环,上述代码还能正确反转吗?为什么? 3. 除了迭代法,还有哪些方法可以实现单链表的反转?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值