【DataStructure】Some useful methods about linkedList(三)

【DataStructure】Some useful methods about linkedList(三)

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

Method 4: Gets the value of element number i

For example, if list is {22, 33, 44, 55, 66, 77, 88, 99}, then get(list, 2) will return 44.

Solution 1:
	static int get(Node list, int i) {
		if (i < 0) {
			throw new IllegalArgumentException();
		}
		for (int j = 0; j < i; j++) {
			if (list == null) {
				throw new IllegalStateException();
			}
			list = list.next;
		}
		return list.data;
	}
Solution 2:
	static int get(Node list, int i)
	{
		Node p = list;
		int j = 0;
		while (j < i && p != null) {
		++j;
		p = p.next;
		}
		if(p == null)
		{
			throw new java.util.NoSuchElementException();
		}
		return p.data;
	}

The output is as follows:

【DataStructure】Some useful methods about linkedList(三)

Method 5:inserts x as element number i;

For example, if list is {22, 33, 44, 55, 66, 77, 88, 99}, then put(list, 3, 50) will change List to {22, 33, 44, 50, 55, 66, 44, 88, 99}. 

Hint: if i= 0, replace the value of the first node With x, and insert a new node immediately after it that contains the previous fist value.

Solution 1:

	static void put(Node list, int i, int x) {
		if (list == null) {
			throw new java.util.NoSuchElementException("List is Empty");
		} else if (i ==0) {
			list.next = new Node(list.data,list);
			list.data = x;
		} else {
			Node p = list;
			int j = 1;
			while (j < i && p != null) {
				++j;
				p = p.next;
			}
			if (p == null)
			{
				String error = String.format("the list has only %d elements", j-1);;
				throw new java.util.NoSuchElementException(error);
			}
			p.next = new Node(x, p.next);
		}
	}

The output is as follows:

【DataStructure】Some useful methods about linkedList(三)

Method 6:Swap the i element with the j element 

For example, if list is {22, 33, 44, 55, 66, 77, 88, 99}, then swap(list, 2, 5) will change List to {22, 33, 77, 55, 66, 44, 88, 99}.

static void swap(Node list, int i, int j) {
		if (i < 0 || j < 0) {
			throw new IllegalArgumentException();
		} else if (i == j) {
			return;
		}
		Node p = list, q = list;
		for (int ii = 0; ii < i; ii++) {
			if (p == null) {
				throw new IllegalStateException();
			}
			p = p.next;
		}
		for (int jj = 0; jj < j; jj++) {
			if (q == null) {
				throw new IllegalStateException();
			}
			q = q.next;
		}
		int pdata = p.data, qdata = q.data;
		p.data = qdata;
		q.data = pdata;
		return;
	}

The output is as follows:

【DataStructure】Some useful methods about linkedList(三)

Method 7: Gets a new list that contains all the elements of list1 and list2 in ascending order. List1 and list2 are both in ascending order. 

For example, if list1is {22, 33, 55, 88} and  list2is {44, 66, 77, 99}, then merged(list1, list2)will return the new list {22, 33, 44, 55, 66, 77, 88, 99}. 

Note that the three lists should be completely independent of each other. Changing one list should have no effect upon the others.

static Node merged(Node list1, Node list2) {
		Node list = new Node(0);
		Node p = list, p1 = list1, p2 = list2;
		while (p1 != null && p2 != null) {
			if (p1.data < p2.data) {
				p = p.next = new Node(p1.data);
				p1 = p1.next;
			} else {
				p = p.next = new Node(p2.data);
				p2 = p2.next;
			}
		}
		while (p1 != null) {
			p = p.next = new Node(p1.data);
			p1 = p1.next;
		}
		while (p2 != null) {
			p = p.next = new Node(p2.data);
			p2 = p2.next;
		}
		return list.next;
	}

The output is as follows:

【DataStructure】Some useful methods about linkedList(三)

版权声明:本文博主原创文章,博客,未经同意不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116954.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • netstat 的10个基本用法

    netstat 的10个基本用法Netstat简介Netstat是一款命令行工具,可用于列出系统上所有的网络套接字连接情况,包括tcp,udp以及unix套接字,另外它还能列出处于监听状态(即等待接入请求)的套接字。如果你想确认系统上的Web服务有没有起来,你可以查看80端口有没有打开。以上功能使netstat成为网管和系统管理员的必备利器。在这篇教程中,我会列出几个例子,教大家如何使用netstat去…

    2022年7月23日
    17
  • 银行风控模型

    银行风控模型风控催生原因对于银行来说,现今互联网贷款和信用卡办理面临的主要难题是数据和风控。站在银行或金融机构角度,自然而然是想获得更多的信息和数据,但是在收集数据这方面又是比较无力的。加上当下的发展趋势,消费贷以及贷款审批速度都要求快。如何在快的的过程中对客户进行一个全面的审查,得出一个合理的结果呢?如果没有详细的数据对客户进行评估,这势必会提高放贷的风险。风控概述所谓风控,是指多银行贷款资金的…

    2022年6月13日
    42
  • 最大公约数和最小公倍数的关系

    最大公约数和最小公倍数的关系联系:最大公约数:指两个或多个整数共有的约数中最大的那个最小公倍数:指两个或多个整数共有的倍数中最小的那个以两个整数为例:最大公约数表示为:(a,b)最小公倍数表示为:[a,b]定理:(a,b)X[a,b]=ab(a,b均为整数)例题:#include<stdio.h>intmain(){ intm,n,min=0,max=0; scanf(“%d%d”,&m,&n); //求最大公约数 for(inti

    2022年5月17日
    69
  • macbook电脑字体乱码了_mac系统语言改中文

    macbook电脑字体乱码了_mac系统语言改中文按照步骤来亲测可解决①如果没有~/.zshrc这个文件,那么新建一个该文件②新建命令:touch~/.zshrc③打开文件文件编辑vim~/.zshrc④修改内容并保存exportLANG=en_US.UTF-8⑤重启终端即可发现中文可以显示了……

    2025年6月23日
    3
  • jdbctemplate查询为空报错_java空指针异常举例

    jdbctemplate查询为空报错_java空指针异常举例问题是在:Dao类是不能直接new出来的必须是通过ApplicationContextapplicationContext=newClassPathXMLApplicationContext(“springmvc.xml”)erDaodao=(UserDao)ac.getBean(“UserDaoId得到类UserDao的实例化从而JdbcTemplate的值才能获…

    2025年6月23日
    7
  • 使用wireshark分析tcp报文

    使用wireshark分析tcp报文前言 TCP 协议在网络过程中 是一个最常见不过的协议了 在分析 tcp 网络协议报文时 借助当前强力的工具 wireshark 可以起到很好的辅助作用 首先抓取了一个简单的 http 请求报文 选取其中的一次完整请求 追踪 tcp 流 可以在报文中看到 tcp 的 3 次握手 以及 http 的 request 和 response 还有 tcp 的 4 次断开 另外整个封包列表的面板中也有显示 编号 时间戳 源地址

    2025年11月4日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号