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


相关推荐

  • STL algorithm算法lower_bound和upper_bound(31)

    STL algorithm算法lower_bound和upper_bound(31)

    2021年12月8日
    44
  • webstorm2021激活码【最新永久激活】

    (webstorm2021激活码)本文适用于JetBrains家族所有ide,包括IntelliJidea,phpstorm,webstorm,pycharm,datagrip等。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月28日
    4.5K
  • 安卓编程用什么软件_手机上能安装PLC编程软件吗?为什么?[通俗易懂]

    安卓编程用什么软件_手机上能安装PLC编程软件吗?为什么?[通俗易懂]时常会有初学者来问,手机是否可以安装PLC编程软件?在这里明确的告诉大家,手机不可以安装PLC编程软件。手机不支持安装PLC编程软件,因为PLC编程软件是你所用的品牌厂家开发出来的,如三菱PLC、西门子PLC、欧姆龙PLC等,厂家开发出软件并上线,供我们下载安装,我们才有的用,目前没有任何厂家有开发手机版的PLC编程软件。其实想用手机版的PLC编程软件,就目前来看,非常的不现实,存在一些问题,如安…

    2022年5月10日
    86
  • 东芝笔记本电脑重装系统按F几(戴尔笔记本重装系统)

    东芝笔记本电脑怎么重装系统?其实笔记本东芝要怎么重装系统的方法很简单,具体要怎么给东芝笔记本电脑重装系统呢?其实笔记本重装系统是非常简单的,那笔记本东芝如何重装系统呢?那下面就让小白小编给大家介绍笔记本东芝如何重装系统的解决方法吧。大家赶紧学习东芝笔记本重装系统吧。东芝笔记本重装系统方法1、去网站下载win7旗舰版镜像文件。2、使用软碟通软件把镜像文件里面的gho.win7提取到已经制…

    2022年4月13日
    141
  • 初识行为识别

    初识行为识别随着互联网的不断发展,各种应用的不断推广。数据无论从存储,格式,形式,类型等方面都趋向于多样化,丰富化,指数化。数据就是价值,为何这么说呢?在机器学习,深度学习推动下,训练数据需求很大。对于分类模型,训练数据越多,分类器的准确度会在一定程度上更精确。行为识别可以说就是在这基础上演变出来的一个研究分支。那么什么是行为识别呢?我的理解是这样的,比如对于某个图片或者视频中的某个信息进行捕获,我们可以使用…

    2022年6月21日
    23
  • 数据库去重有几种方法_数据库去重有几种方法

    数据库去重有几种方法_数据库去重有几种方法MySQL数据库去重的方法​数据库最近有很多重复的数据,数据量还有点大,本想着用代码解决,后来发现用SQL就能解决,这里记录一下看这条SQLDELETEconsum_recordFROMconsum_record,(SELECTmin(id)id,user_id,monetary,consume_timeFROMconsum_recordGROUPBYuser_id,monetary,co…

    2022年10月1日
    0

发表回复

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

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