单向链表的实现

13 篇文章 0 订阅
10 篇文章 0 订阅

实现一个单向无头非循环链表

1.创建一个链表并打印:
class Node {
    public int val;
    public Node next;

    public Node(int val) {
        this.val = val;
    }
}
public  class MyLinkedList {

    public Node head;//普通引用,目的是让head一直指向当前列表的头

    public void createLinked() {
        this.head = new Node(12);
        Node node2 = new Node(22);
        Node node3 = new Node(32);
        Node node4 = new Node(42);
        head.next = node2;
        node2.next = node3;
        node3.next = node4;
    }

    public void display() {
        Node cur = this.head;
        while (cur != null) {
            System.out.println(cur.val);
            cur = cur.next;
        }
    }

    public static void main(String[] args) {
        MyLinkedList myLinkedList = new MyLinkedList();
        myLinkedList.createLinked();
        myLinkedList.display();

    }

结果:
在这里插入图片描述

2.实现一个单链表

1.头插法
2.尾插法
3.任意位置插入,第一个数据节点为0号下标
4.查找是否包含关键字key是否在单链表当中
5.删除第一次出现关键字为key的节点
6.删除所有值为key的节点
7.得到单链表的长度
8.打印一个单链表
代码示例:

class Node {
    public int val;
    public Node next;

    public Node(int val) {
        this.val = val;
    }
}
public  class MyLinkedList {

    public Node head;//普通引用,目的是让head一直指向当前列表的头

    public void createLinked() {
        this.head = new Node(12);
        Node node2 = new Node(22);
        Node node3 = new Node(32);
        Node node4 = new Node(42);
        head.next = node2;
        node2.next = node3;
        node3.next = node4;
    }
//7.打印一个链表
    public void display() {
        Node cur = this.head;
        while (cur != null) {
            System.out.print (cur.val +" ");
            cur = cur.next;
        }
        System.out.println();
    }
  
    //8.得到单链表的长度
    public int size(){
        Node cur = this.head;
        int count =0;
        while (cur != null ) {
            cur = cur.next;
            count++;
        }
        return count;
    }
    //4.查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        Node cur= this.head;
        while (cur!= null){
            if (cur.val == key ){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }

    //1.头插法
    public void addFirst(int data) {
        Node node= new Node(data);//Node一个新的节点;
        if (this.head ==null){
           this.head= node;//链表为空,链表的头就等于要插入的节点;
        }else {
            node.next = this.head;//①(先绑定后面的)新节点的next=原来链表的head;
            this.head = node;//②链表的头等于要插入的节点;
        }
    }
    //2.尾插法
    public void addLast(int data){
        Node node= new Node(data);//Node一个新的节点;
        if (this.head == null){
            this.head =node;//节点为空,链表的头就是要插入的节点;
        }else{
            Node cur = this.head;
              while (cur.next!=null){
                  cur=cur.next;//遍历找到最后一个节点;
              }
              cur.next=node;//最后一个节点的next等于要插入的节点;
        }
    }
    public Node moveYiIndex (int index){//找到要插入节点的前一个
        Node cur = this.head;
        int count =0;
        while (count != index-1){
            cur= cur.next;
            count++;
        }
        return cur;
    }
    //3.任意位置插入,第一个数据节点为0号下标
   public void addIndex(int index,int data) {
       if (index < 0 || index > size()) {//不合法现象;
           System.out.println("index不合理");
           return;
       }
       if (index == 0) {//头插法
           addFirst(data);
           return;
       }
       if (index == size()){ //尾插法
           addLast(data);
           return;
       }
       Node cur =moveYiIndex(index);//调用moveYiIndex函数,找到index的前一个节点cur;
       Node node = new Node(data);//定义一个新的节点;
       node.next= cur.next;//插入节点的next等于原始cur的next;
       cur.next = node;//cur的next变成了node;
   }
    public Node searchCur(int key){//找到要删除节点的前驱
        Node cur = this.head;
        while (cur.next != null){
            if (cur.next.val == key){//找出key的节点的前一个
                return cur;
            }
            cur = cur.next;

        }
        return null;
    }
 //5.删除第一次出现关键字为key的节点
    public void remove(int key) {
        if (this.head == null) {
            return;
        }
            if (this.head.val == key){
                this.head = this.head.next;
                return;
            }
        Node cur = searchCur(key);//调用searchCur,找出删除节点的前驱cur
        if (cur == null){
            System.out.println("未找到要删除的节点");
        }else{
            Node del = cur.next;//定义要删除的节点为key;
            cur.next =del.next;//让cur的next等于删除节点的next;
        }
    }
 //6.删除所有值为key的节点
     public void removeAllKey(int key){
        if (this.head == null){
            System.out.println("链表为空");
            return;
        }
        Node prev = this.head;//定义一个prev指向链表的头;
        Node cur = prev.next;//定义一个cur指向prev的next;
        while (cur != null){//遍历完条件;
            if (cur.val == key){//找到要删除的节点;
                prev.next = cur.next;//把该节点删掉;
            }else {
                prev = cur;//未找到
            }
            cur = cur.next;//cur继续往后走;
        }
        if (this.head.val == key){//头节点就是要删除的节点
            this.head = this.head.next;//删除头,链表的头直接是head的next;
        }
    }

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
约瑟夫问题是一个经典的数学问题,它的具体描述是:有n个人围成一圈,从第k个人开始报数,报到m的人出列,然后从下一个人开始重新报数,直到所有人都出列。现在我来介绍一下如何使用C语言的单向链表实现约瑟夫问题。 首先,我们需要定义一个链表节点的结构体,包含两个成员变量:一个是保存人员编号的整型变量,另一个是指向下一个节点的指针。 ```c typedef struct Node { int data; struct Node* next; } Node; ``` 接下来,我们可以编写一个函数来创建一个包含n个节点的循环链表,并返回链表的头节点。 ```c Node* createCircularLinkedList(int n) { Node* head = NULL; Node* prev = NULL; for (int i = 1; i <= n; i++) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = i; if (head == NULL) { head = newNode; } else { prev->next = newNode; } prev = newNode; } prev->next = head; // 将最后一个节点的next指针指向头节点,形成循环链表 return head; } ``` 接下来,我们可以编写一个函数来模拟约瑟夫问题的求解过程。 ```c void josephusProblem(Node* head, int k, int m) { Node* current = head; Node* prev = NULL; // 找到从第k个人开始报数的节点 for (int i = 1; i < k; i++) { prev = current; current = current->next; } // 开始报数并出列,直到所有人都出列 while (current->next != current) { // 报数m次 for (int i = 1; i < m; i++) { prev = current; current = current->next; } // 出列 prev->next = current->next; Node* temp = current; current = current->next; free(temp); } // 输出最后一个出列的人员编号 printf("最后一个出列的人员编号:%d\n", current->data); // 释放头节点的内存 free(current); } ``` 最后,我们可以在主函数中调用上述函数来解决约瑟夫问题。 ```c int main() { int n, k, m; printf("请输入总人数n:"); scanf("%d", &n); printf("请输入从第k个人开始报数:"); scanf("%d", &k); printf("请输入报数m次出列:"); scanf("%d", &m); Node* head = createCircularLinkedList(n); josephusProblem(head, k, m); return 0; } ``` 这样,我们就可以通过C语言的单向链表实现约瑟夫问题了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值