数据结构--单链表

/**
 * 实现一个单链表: (1)查找--查找第index个节点;查找指定的元素 (2)插入--将指定的元素插入到第index个节点上
 * (3)删除--将第index个节点删除
 * 规律:删除和添加元素前务必保存两个元素的地址引用信息
 */
public class MyLinkedList<T> {
    // 定义一个内部类来表示链表节点的数据结构
    private class Node {
        // 数据段
        private T data;
        // 地址段
        private Node next;

        // 节点的的初始化
        public Node(T data, Node next) {
            this.data = data;
            this.next = next;
        }
    }

    // 记录链表结构的头结点地址引用
    private Node head;
    // 记录链表结构的尾节点地址引用
    private Node tail;
    // 记录链表结构中的节点数
    private int size;

    // 创建空链表
    public MyLinkedList() {
        this.head = null;
        this.tail = null;
    }

    // 以一个指定元素来初始化该链表
    public MyLinkedList(T data) {
        head = new Node(data, null);
        tail = head;
        size++;
    }

    // 返回链表的长度
    public int length() {
        return size;
    }

    // 获取链表索引为index处的数据
    public Node get(int index) {
        // 参数容错
        if (index < 0 || index > size) {
            throw new IndexOutOfBoundsException("链表索引越界");
        }
        // 单链表遍历
        Node current = head;
        // 统计已经遍历的节点的个数
        int count = 0;
        while (null != current) {
            if (count == index) {
                return current;
            }
            count = count + 1;
            current = current.next;
        }
        return null;
    }

    // 查找指定元素
    public int get(T element) {
        // 单链表遍历
        Node current = head;
        // 记录玩家已遍历的节点的个数
        int count = 0;
        while (current != null) {
            if (current.data.equals(element)) {
                return count;
            }
            count = count + 1;
            current = current.next;
        }
        return -1;
    }

    // 向指定位置插入一个元素
    public void insert(T element, int index) {
        // 参数容错
        if (index < 0 || index > size) {
            throw new IndexOutOfBoundsException("链表索引越界");
        }

        if (size == 0) {
            // 空链表情形
            head = new Node(element, null);
            tail = head;
        } else {
            // 非空链表情形
            if (index == 0) {
                // 从头部插入
                Node old = head;
                head = new Node(element, null);
                head.next = old;
            } else if (index == size) {
                // 从尾部插入
                Node old = tail;
                tail = new Node(element, null);
                old.next = tail;
            } else {
                // 从中部插入:遍历单链表
                Node current = head;
                // 记录玩家已经遍历的节点数
                int count = 0;
                while (current != null) {
                    // 保存插入节点的前一节点和后一节点的历史信息
                    if (count == index) {
                        Node pre = current;
                        Node next  = current.next;
                        current = new Node(element, next);
                        pre.next = next;
                    }
                    count = count + 1;
                    current = current.next;
                }                
            }
        }
        // 别忘了:size增1
        size = size + 1;
    }
    
    // 头插法插入一个节点
    public void addAtHead(T element){
        if (head == null) {
            // 空链表情形
            head = new Node(element, null);
            tail = head;
        } else {
            // 非空链表情形
            // 保存原头结点及第一个节点的信息
            Node pre = head;
            Node next = head.next;
            head = new Node(element, pre);
            pre.next =  next;            
        }        
        size = size + 1;
    }
    
    // 尾插法插入一个节点
    public void addAtTail(T element){
        if (head == null) {
            // 空链表情形
            head = new Node(element, null);
            tail = head;
        } else {
            // 非空链表情形
            // 保存尾结点及前一个节点的信息
            Node pre = tail;
            tail = new Node(element, null);
            pre.next = tail;            
        }        
        size = size + 1;
    }
    
    // 删除指定索引位置的元素
    public T remove(int index) {
        // 参数容错
        if (index < 0 || index > size) {
            throw new IndexOutOfBoundsException("链表索引越界");
        }
        if (head == null) {
            // 空链表
            return null;            
        } else {
            // 单链表的遍历
            Node current = head;
            // 记录玩家已经遍历的节点数
            int count = 0;
            while (current != null) {
                if (count == index - 1) {
                    // 只需找到要删除的节点的前一节点就好了,保存要删除的节点和前一节点的信息
                    Node old = current.next;
                    Node pre = current;
                    current.next = null;
                    pre.next = old.next;
                    return old.data;
                }
                count = count + 1;
                current = current.next;
            }
            return null;
        }        
    }
    
    // 删除指定元素
    public Node remove(T element) {
        if (head == null){
            // 空链表情形
            return null;
        } else {
            // 非空链表情形
            // 单链表的遍历
            Node current = head;
            // 记录玩家已经遍历的节点数
            int count = 0;
            while (current != null) {
                if (current.data.equals(element)) {
                    // 只需找到要删除的节点的前一节点就好了,保存要删除的节点和前一节点的信息
                    Node pre = this.get(count -1);
                    Node old = current;
                    current = null;
                    pre.next = old.next;
                    return old;
                }
                count = count + 1;
                current = current.next;
            }
        }
        return null;
        
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
单链表是一种常见的数据结构,它由一个或多个节点组成,每个节点包含一个数据域和一个指针域,指针域指向下一个节点。 实现单链表的查找位序算法函数需要以下步骤: 1. 定义一个指针p指向链表的头节点,并定义一个变量count,用于计数。 2. 从头节点开始,遍历链表,当p指向某个节点时,计数器count加1。 3. 如果p指向的节点的数据与目标数据相等,则返回当前的计数器count,即为目标数据的位序。 4. 如果p指向的节点不是目标数据,则将p指向下一个节点,重复步骤3。 5. 如果遍历完链表后仍未找到目标数据,则返回-1,表示未找到。 下面是C语言实现单链表查找位序算法函数的代码示例: ```c #include <stdio.h> #include <stdlib.h> // 定义单链表节点结构 typedef struct Node { int data; // 数据域 struct Node* next; // 指针域 } Node; // 查找位序的算法函数 int findPosition(Node* head, int target) { Node* p = head; // 指向头节点 int count = 0; // 计数器初始化为0 while (p != NULL) { count++; // 计数器加1 if (p->data == target) { return count; // 找到目标数据,返回当前计数器的值 } p = p->next; // 指向下一个节点 } return -1; // 遍历完链表未找到目标数据,返回-1 } int main() { // 创建链表 Node* head = (Node*)malloc(sizeof(Node)); head->data = 1; // 头节点数据为1 Node* node1 = (Node*)malloc(sizeof(Node)); node1->data = 2; Node* node2 = (Node*)malloc(sizeof(Node)); node2->data = 3; head->next = node1; node1->next = node2; node2->next = NULL; // 查找位序示例 int target = 3; // 目标数据为3 int position = findPosition(head, target); if (position != -1) { printf("目标数据 %d 的位序为 %d\n", target, position); } else { printf("未找到目标数据 %d\n", target); } // 释放链表内存 free(node2); free(node1); free(head); return 0; } ``` 在上述代码中,我们首先定义了一个指向头节点的指针p和一个计数器count,然后使用while循环遍历链表。当p指向某个节点时,计数器加1,并判断该节点的数据是否与目标数据相等。如果找到了目标数据,则返回当前计数器的值,即为目标数据的位序。如果遍历完链表仍未找到目标数据,则返回-1表示未找到。最后在主函数中演示了调用该算法函数的示例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值