数据结构单链表的模拟实现


import java.util.Stack;

public class SingleLinkedList {
     static class Node{
        public int value;
        Node next;
        public Node(int value) {
            this.value = value;
        }

    }
    public Node head;
    public int size(){
        int count=0;
        Node current =head;
        while(current!=null){
            current=current.next;
            count++;
        }
        return count;
    }
    public void addFirst(int data) {
            Node node = new Node(data);
            node.next = head;
            head = node;

    }
    public void addLast(int data){
        Node node=new Node(data);
        if(head==null){
            addFirst(data);
        }
        else{
            Node current=head;
            while(current.next!=null){
                current=current.next;
            }
            current.next=node;
        }
    }
    public void addIndex(int index,int data){
        if(index==0){
            addFirst(data);
            return;
        }
        if(index==size()){
            addLast(data);
            return;
        }
        check(index);
        Node previousNode=FindPreviousNode(index);
        Node node=new Node(data);
        node.next=previousNode.next;
        previousNode.next=node;

    }
    private void check(int index){
        if(index<0||index>size())
            throw new ArrayIndexOutOfBoundsException("index位置不合法");
    }
    private Node FindPreviousNode(int index){
        Node current =head;
        for(int i=0;i<index-1;i++){
            current=current.next;
        }
        return current;
    }
    public boolean contains(int key){
        Node current =head;
        while(current!=null){
            if(current.value==key) {
                return true;
            }
            current=current.next;
        }
        return false;
    }
public void remove(int key){
        if(head.value==key){
            head=head.next;
            return;//r如果删除的是首元素
        }
        //如果删除的是中间元素
        Node current=head;
        while(current.next!=null){
            if(current.next.value==key){
                current.next=current.next.next;
                break;//不退出会报空指针异常
            }
            current=current.next;
        }
}
public void removeAllKey(int key){
        if(head==null){
            return;
        }
        Node previousNode=head;
        Node current=head.next;
        while(current!=null){
            if(current.value==key){
                previousNode.next=current.next;
            }else{
                previousNode=current;
            }
            current=current.next;
        }
        if(head.value==key){
            head=head.next;
        }
}

public void clear(){
        Node current=head;
        while(current!=null){
            Node nextNode=current.next;
            current.next=null;
            current=nextNode;
        }
      head=null;

}
   
   
    public  void display(){
        Node current=head;
        while(current!=null){
            System.out.print(current.value+" ");
            current=current.next;
        }
        System.out.println();
    }
    public static void display(Node head){
        Node current=head;
        while(current!=null){
            System.out.print(current.value+" ");
            current=current.next;
        }
        System.out.println();
    }
    public void printList(){
        //倒序遍历递归方法
        if(head==null){
            return ;
        }
        Node current=head;
        if(current.next!=null){
            printList(current.next);
        }
        System.out.println(current.value);
    }
    public void printList (Node head) {
        //倒序遍历将链表压入栈中依次弹出
        if (head == null) {
            System.out.println("");
            return;
        }
        Node current=head;
        Stack<Node> stack=new Stack<>();
        while(current!=null){
            stack.push(current);
            current=current.next;
        }
          while(!stack.isEmpty()){
              System.out.print(stack.pop().value+" ");
          }

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
数据结构算法的代码实现涉及到很多不同的问题和挑战。以下是其中一些常见的实现示例: 1. 数组:实现一个支持动态扩容的数组,可以使用动态分配内存的方式来实现数组的动态扩容。当数组空间不足时,可以创建一个更大的数组,将原有的数据复制到新数组中。 2. 链表:可以实现单链表、循环链表和双向链表。单链表包含一个指向下一个节点的指针,循环链表的尾节点指向头节点,双向链表每个节点有一个指向前一个节点和后一个节点的指针。 3. 栈:可以使用数组或链表来实现顺序栈和链式栈。顺序栈使用数组来保存数据,链式栈使用链表来保存数据。在模拟浏览器的前进、后退功能时,可以使用两个栈来实现。 4. 队列:可以使用数组或链表来实现顺序队列和链式队列。顺序队列使用数组来保存数据,链式队列使用链表来保存数据。循环队列则在顺序队列的基础上增加了循环利用空间的功能。 5. 递归:递归是一种函数自己调用自己的方法。可以使用递归来实现斐波那契数列的求值、阶乘的计算以及一组数据集合的全排列等。 6. 排序:可以实现归并排序、快速排序、插入排序、冒泡排序和选择排序等。这些排序算法实现方式各不相同,但都能实现对一组数据的排序。 7. 二分查找:可以实现对有序数组的二分查找算法。二分查找是一种高效的查找方法,可以在对数时间复杂度内找到目标元素。 8. 散列表:可以实现基于链表法解决冲突问题的散列表。散列表是一种根据关键字直接访问数据的数据结构,可以使用散列函数将关键字映射到对应的数组下标。 9. 字符串:可以实现字符集的Trie树和朴素的字符串匹配算法。Trie树是一种用于快速检索和匹配字符串的数据结构,而朴素的字符串匹配算法则是一种简单但效率较低的算法。 10. 二叉树:可以实现二叉查找树,并支持插入、删除和查找操作。还可以实现查找二叉查找树中某个节点的后继、前驱节点以及二叉树的前、中、后序遍历和按层遍历。 11. 堆:可以实现小顶堆、大顶堆和优先级队列。堆是一种特殊的完全二叉树,可以用来高效地找到最大或最小元素。 12. 图:可以实现有向图和无向图的邻接矩阵和邻接表表示方式。还可以实现图的深度优先搜索、广度优先搜索以及Dijkstra算法和A*算法等。 13. 回溯:可以利用回溯算法求解八皇后问题和0-1背包问题等。回溯算法是一种通过不断回退和尝试来搜索解空间的方法。 14. 分治:可以利用分治算法求解一组数据的逆序对个数。分治算法是一种将问题分解为子问题并分别求解的方法。 15. 动态规划:可以用动态规划解决0-1背包问题、最小路径和、莱文斯坦最短编辑距离和最长公共子序列等问题。动态规划是一种通过保存子问题的解来避免重复计算的方法。 以上是一些常见的数据结构算法的代码实现示例,具体的实现方式和代码细节可能会有所不同。在实际应用中,根据具体的问题和需求,选择合适的数据结构算法实现

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jhpan666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值