【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)
上一篇 2022年1月12日 下午8:00
下一篇 2022年1月12日 下午9:00


相关推荐

  • DatabaseMetaData的简单使用

    DatabaseMetaData的简单使用在看大佬写的一个导出数据库建标脚本的接口的时候,发现频频用到DataBaseMetaData这个类,之前也没有用过这个类下的API,记录一下心得用法。DatabaseMetaData是java.sql包中的接口,利用它可以获取我们连接到的数据库的结构、存储等很多信息;先上个API文档:这英文看的是心力憔悴,直接来看这个接口…

    2022年6月19日
    34
  • 零门槛!GPT5接入Claude Code教程

    零门槛!GPT5接入Claude Code教程

    2026年3月15日
    2
  • 腾讯元宝

    腾讯元宝

    2026年3月12日
    2
  • 超详细LinkedHashMap解析

    超详细LinkedHashMap解析文章目录 LinkedHashMa 概述 LinkedHashMa 原理主要元素构造函数维护链表的操作 afterNodeRem 操作 put 操作 HashMap putVal Remove 操作 HashMap removeNode LinkedHashMa 用作实现 LRU 总结 LinkedHashMa 概述 pub

    2026年3月19日
    2
  • css 背景渐变详解

    css 背景渐变详解一 什么是渐变多种颜色变化的一个过程分类 1 线性渐变 2 径向渐变 3 重复渐变 线性 径向 特点 1 渐变都有填充方向 线性渐变 2 色标 颜色 位置 二 语法属性 backg

    2026年3月19日
    3
  • 数据挖掘之时间序列分析[通俗易懂]

    数据挖掘之时间序列分析[通俗易懂]按时间顺序排列的一组随机变量X1,X2,…,Xt表示一个随机事件的时间序列。时间序列分析的目的是给定一个已被观测了的时间序列,预测该序列的未来值。表1常用的时间序列模型 模型名称 描述 平滑法 常用于趋势分析和预测,利用修匀技术,削弱短期随机波动对序列的影响,使序列平滑化。 根据所用平滑技术的不同,可分为移动平均法和指数平滑法。 趋势拟合法…

    2022年6月22日
    32

发表回复

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

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