Java算法学习1——链表、数组与哈希表

一、链表与哈希表的知识点讲解

1.链表

(1) 链表的实现方式

链表的实现方式有两种,分别为指针和数组,下面对这两部分的具体实现步骤做一个说明。

- 指针实现

①先要建立一个结点结构体
②然后建立一个头结点
③最后利用尾插法像头结点后面插入元素,注意插入时要建立一个工作指针,用来时刻指向最后一个链表元素。

import java.util.*;

class Node//相当于Node的结构体
{

    public int data;//相当于结构体
    public Node next;

    Node(int data,Node next)//构造函数作为分配空间
    {
        this.data=data;
        this.next=next;
    }

    public static void Insert (Node head,Node index,Node newIndex)//在以head为头结点的链表中的index元素后面插入newIndex
    {
        if(head==null){
            return;
        }
        else
        {
            while (head!=null)
            {
                if(head.next==index){
                    newIndex.next=index;
                    head.next=newIndex;
                    break;
                }
                else head=head.next;

            }
        }

    }

    public static void main(String arg[])//测试
    {
        Node head=new Node(4,null);//头结点放四个元素
        Node first=new Node (2,null);
        Node Second=new Node (3,null);
        Node Third=new Node (4,null);
        Node Forth=new Node(5,null);

        head.next= first;
        first.next=Second;
        Second.next=Third;
        Third.next=Forth;

        Node current=head.next;
        while(current!=null)
        {
            System.out.print(current.data+" ");
            current=current.next;
        }
    }
}

Java实现链表的各种操作详情请见:

https://zhuanlan.zhihu.com/p/115309579

~~ ~~

- 数组实现

①创建两个数组,一个是data,一个是next,data存数据,next存data的下标
②难点在于新建链表时,两者要同步考虑
加入在data的最后一个元素加入新元素m,并且插入到链表的x元素之后
③易错:在输出数组的时候不能够直接System.out.print(array)这样赎出来的是数组的首地址(list就可以,而应该用循环或者System.out.print(Arrays.toString(array));

data[len]=m;
int i=0;
int t=0;//指向链表的头结点
while(data[t]!=x)
{
  t=next[t];
}

//以下这两部是插入的重点步骤
next[len]=next[t];//将x元素之后的结点下标存在next的len位置中,原来这个位置为空
next[t]=len;//将待插入的元素x的下标插入进来
  

2.哈希表

(1)哈希表知识点
① https://blog.csdn.net/xiaoxik/article/details/74926090
② https://blog.csdn.net/Jack__Frost/article/details/69388422?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.control&dist_request_id=987c1905-dc60-4271-ae67-8922d408c007&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.control

(2)Java中哈希表的常用操作
https://blog.csdn.net/weixin_39394671/article/details/113886867

注意要先import java.util.HashMap;
然后 新建哈希表 HashMap<Integer,String> map= new HashMap<>();

另外,还有就是在检查哈希表元素是否存在的时候,可以检查键值也可以检查元素值
map.containKey(3);//检查键值是否存在
map.containValue(“IsEmpty?”)//检查哈希表中 元素是否存在

----------------------------------------2022.03补充更新------------------------------------

package Knowledgement;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.Scanner;

/*
 * 这一节知识点包括了链表数组的建立与使用,Int与String的相互转化,以及Arrays和Collections的sort函数
 */
public class thearray {
	public static void main(String[] args) {
		System.out.println(1);
		LinkedList<Integer> test= new LinkedList<>();
		String [] arraytest=new String [6];
		Scanner sc=new Scanner(System.in);
		int i=0;
//		int t=sc.nextInt();int b=sc.nextInt();int c=sc.nextInt();
//		System.out.print(t);System.out.print(b);System.out.print(c);
		String strs="18";
		int tests=Integer.parseInt(strs);
		System.out.print(tests);//注意在字符串转换为整型或者浮点型的时候,该字符串不能有字母,否则直接int=a+b+c即可;
		for(i=0;i<=5;i++) {
			int w=sc.nextInt();
			test.add(w);
			arraytest[i]=String.valueOf(w);//string->int: int=Integer.paseInt(s); int->string: string=String.valueOf(w)
			System.out.println(arraytest[i]);
		}
		//for循环实现连续输入
		Scanner cin=new Scanner (System.in);
		while(cin.hasNext()) //可以在条件中加入停止循环的条件
		{
			int testa=cin.nextInt(); int testb=cin.nextInt();
			System.out.print(testa+testb);
		}
		System.out.println(test);
		System.out.println(Arrays.toString(arraytest));
		Collections.sort(test,newcomparator);
		Arrays.sort(arraytest,arraycomparator);
		System.out.println(test);
		System.out.println(Arrays.toString(arraytest));
//		for(int k=0;k<test.size();k++) {
//			System.out.println(test.get(k));
//		}
	}
	static //设置新的比较器
	Comparator<Integer> newcomparator= new Comparator<Integer>() {
		public int compare(Integer s1,Integer s2) {
			if(s1>s2) {
				return -1;//当为1的时候为升序(因为1的时候说明要亮亮调换),为-1的时候为降序
			}else if(s1<s2) {
				return 1;//当为-1的时候为降序,为-1的时候为升序
			}else {
				return 0;
			}
		}
	};
	static Comparator<String> arraycomparator=new Comparator<String>() {
		public int compare(String s1,String s2) {
			return s1.compareTo(s2);//(当s1<s2时,compareto的返回的结果为1,而comare为1的时候就是升序)
		}
	};
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值