【DataStructure】Some useful methods about linkedList(三)

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:

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:

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:

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(三)

时间: 2024-07-31 04:36:19

【DataStructure】Some useful methods about linkedList(三)的相关文章

【DataStructure】Some useful methods about linkedList(二)

Method 1: Add one list into the other list. For example, if list1is {22, 33, 44, 55} and  list2 is {66, 77, 88, 99},then append(list1, list2)will change list1to {22, 33, 44, 55, 44, 66, 77, 88, 99}. static void append(Node list1, Node list2) { if (li

【DataStructure】 Five methods to init the List in java

Do you know how to init list in other way except for new object? The following will give you serveral tips. If having other great idea, you are welcome to share. [java] view plaincopy import java.util.ArrayList; import java.util.Arrays; import java.u

Some useful methods about linkedList.

/** * Method 1: Delete the input element x * and meanwhile keep the length of array after deleted n * @param a  the array * @param n  the length of array after deleted. * @param x  the element that need to be deleted. */ static void delete(int[] a, i

【DataStructure】Some useful methods about linkedList.

/** * Method 1: Delete the input element x * and meanwhile keep the length of array after deleted n * @param a  the array * @param n  the length of array after deleted. * @param x  the element that need to be deleted. */ static void delete(int[] a, i

数据结构之链表(LinkedList)(三)

数据结构之链表(LinkedList)(二) 环形链表 顾名思义 环形列表是一个首尾相连的环形链表 示意图 循环链表的特点是无须增加存储量,仅对表的链接方式稍作改变,即可使得表处理更加方便灵活. 看一样著名的应用场景 我们就可以用环形单链表解决这个问题. 首先我们怎么构建一个环形链表 分析: 1. 先创建第一个节点, 让 first 指向该节点,并形成环形 2. 后面当我们每创建一个新的节点,就把该节点,加入到已有的环形链表中即可. 示意图: 代码: // 创建一个Boy类,表示一个节点 cla

ArrayList 和 LinkedList的执行效率比较

一.概念: 一般我们都知道ArrayList* 由一个数组后推得到的 List.作为一个常规用途的对象容器使用,用于替换原先的 Vector.允许我们快速访问元素,但在从列表中部插入和删除元素时,速度却嫌稍慢.一般只应该用ListIterator 对一个 ArrayList 进行向前和向后遍历,不要用它删除和插入元素:与 LinkedList 相比,它的效率要低许多LinkedList 提供优化的顺序访问性能,同时可以高效率地在列表中部进行插入和删除操作.但在进行随机访问时,速度却相当慢,此时应

Android ArrayList LinkedList Set HashMap的介绍.

在Android开发中我们经常需要对数据进行分类和操作,对于轻量级的数据存储我们可能不需要动用SQLite或效率以及类库不完善的XML,由于 SharedPreferences不具备数据枚举方法,如果仅仅是一个String或Int数组可以通过一个标记分割设计外,我们还是主要来看看 Android或者说Java提供的基础数据类型辅助类ArrayList LinkedList Set HashMap的介绍. 在Java中提供了Collection和Map接口.其中List和Set继承了Collect

SuperSocket入门(四)-命令行协议

前面已经了解了supersocket的一些基本的属性及相关的方法,下面就进入重点的学习内容,通信协议.在没有看官方的文档之前,对于协议的理解首先想到的是TCP和UDP协议.TCP 和 UDP 是传输层协议.在Socket程序中仅仅定义了传输层协议是不能让网络的两端进行通信的.我们需要定义应用层通信协议把我们接收到的二进制数据转化成程序能理解的请求. 命令行协议是一种被广泛应用的协议.一些成熟的协议如 Telnet, SMTP, POP3 和 FTP 都是基于命令行协议的. 在SuperSocke

Java——容器类(集合类)

在编程的过程中,多数据的存储及应用都是比较麻烦的事,以前我就只知道用数组和自己写封装类来解决,但是这两种方法在一些功能中并不适用,比如我们要根据数据库中其中一个表的数据弄一个下拉菜单的内容,这个时候我们需要从数据库获取显示给用户看的内容及对应的id.在这种情况中,因为在数据库中的这种一对的数据一般都是有好几个的,所以封装类并不适用,而数组只能存储单一的一种数据类型,并且只能通过游标获取对应的值,还有最麻烦的是数组在初始化的时候必须初始化长度,所以也数组也不太适用,这个时候我们可以使用java官方