【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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • pytest指定用例_测试用例怎么编写

    pytest指定用例_测试用例怎么编写前言测试用例在设计的时候,我们一般要求不要有先后顺序,用例是可以打乱了执行的,这样才能达到测试的效果.有些同学在写用例的时候,用例写了先后顺序,有先后顺序后,后面还会有新的问题(如:上个用例返回

    2022年7月31日
    4
  • 十、模板方法模式—制作更多好喝的饮品! #和设计模式一起旅行#

    无规矩不成方圆!故事背景在安装了酷炫了碉堡了的灯之后,我饿设计模式MM经营的奶茶店生意更加火爆,有不同国家的顾客,那么为了满足更多消费者的口味,我们推出了中国龙井苹果茶和叶门摩卡牛奶咖啡。其中制作的过程:把水烧开将咖啡/茶叶放到杯子里用沸水冲泡加入牛奶/苹果汁下面用代码来表示茶和咖啡的制作过程!//龙井苹果茶制作public class LongJi…

    2022年2月27日
    39
  • 使用VMware安装centos7并配置网络

    使用VMware安装centos7并配置网络准备工作:1.VM虚拟机(本文使用VMware14.1.2)2.Centos7安装包(https://www.centos.org/download/)centos官网提供多个版本下载,一般使用标准的DVD版安装版,大约4.2G。也可以使用mini版,后续自己安装第三方软件。各版本区别:https://wiki.centos.org/Manuals/ReleaseNotes/Cent…

    2022年6月6日
    32
  • stm32 SWD调试接口的使用

    stm32 SWD调试接口的使用SWD和传统的调试方式区别   1.SWD模式比JTAG在高速模式下面更加可靠。在大数据量的情况下面JTAG下载程序会失败,但是SWD发生的几率会小很多。基本使用JTAG仿真模式的情况下是可以直接使用SWD模式的,只要你的仿真器支持。所以推荐大家使用这个模式。   2.在大家GPIO刚好缺一个的时候,可以使用SWD仿真,这种模式支持更少的…

    2022年5月8日
    157
  • IOS框架概览

    IOS框架概览

    2021年12月4日
    42
  • AngularJS自己定义标签加入回调函数eval()

    AngularJS自己定义标签加入回调函数eval()

    2022年3月3日
    45

发表回复

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

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