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


相关推荐

  • 邮件服务器的架设方法「建议收藏」

    用Win2003架设邮件服务器很多企业局域网内都架设了邮件服务器,用于进行公文发送和工作交流。但使用专业的企业邮件系统软件需要大量的资金投入,这对于很多企业来说是无法承受的。其实我们可以通过WindowsServer2003提供的POP3服务和SMTP服务架设小型邮件服务器来满足我们的需要。一、安装POP3和SMTP服务组件WindowsServer2003…

    2022年4月8日
    187
  • UART和USART 有区别

    UART和USART 有区别UART:universalasynchronousreceiverandtransmitter通用异步收发器          [BusSignal]  TX ,RX USART:universalsynchronousasynchronousreceiverandtransmitter通用同步异步收发器          [BusSi

    2022年5月19日
    31
  • 详解clientHeight、offsetHeight、scrollHeight「建议收藏」

    详解clientHeight、offsetHeight、scrollHeight「建议收藏」关于clientHeight、offsetHeight、scrollHeight的详解

    2022年7月24日
    10
  • 理论基础 —— 排序 —— 直接选择排序

    理论基础 —— 排序 —— 直接选择排序概述 直接选择排序又称简单选择排序 是一种不稳定的排序方法 其是选择排序中最简单一种 其基本思想是 第 i 趟排序再待排序序列 a i a n 中选取关键码最小的记录 并和第 i 个记录交换作为有序序列的第 i 个记录 其实现利用双重循环 外层 i 控制当前序列最小值存放的数组元素位置 内层循环 j 控制从 i 1 到 n 序列中选择最小的元素所在位置 k 排序过程 1 排序

    2025年11月2日
    3
  • OpenCV——Canny边缘检测(cv2.Canny())

    OpenCV——Canny边缘检测(cv2.Canny())Canny边缘检测Canny边缘检测是一种使用多级边缘检测算法检测边缘的方法。1986年,JohnF.Canny发表了著名的论文AComputationalApproachtoEdgeDetection,在该论文中详述了如何进行边缘检测。Canny()边缘检测步骤Canny边缘检测分为如下几个步骤:步骤1:去噪。噪声会影响边缘检测的准确性,因此首先要将噪声过滤掉。步骤2:计算梯度的幅度与方向。步骤3:非极大值抑制,即适当地让边缘“变瘦”。步骤4:确定边缘。使

    2022年5月25日
    42
  • img 标签 访问图片 返回403 forbidden问题,meta标签的说明[通俗易懂]

    img 标签 访问图片 返回403 forbidden问题,meta标签的说明

    2022年2月12日
    41

发表回复

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

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