【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, int n, int x) {
		// preconditions: [0] <= ... <= a[n-1], and n <= a.length;
		// postconditions: a[0] <= ... <= a[n-2], and x is deleted;
		int i = 0; // find the first index i for which a[i] > x:
		while (i < n && a[i] <= x) {
			++i;
		}
		// shift {a[i],...,a[n-1]} into {a[i-1],...,a[n-2]}:
		if (i < n - 1) {
			System.arraycopy(a, i, a, i - 1, n - i);
		}
		a[n - 1] = 0;
	}

/**

* Gets the number of nodes in the specified list;

* Fox example, if list is {33,55,77,99}, the size(list) will be return 4;

*

* @param list

* @return

*/

static int size(Node list) {
		int size = 0;
		while (list != null) {
			++ size;
			list = list.next;
		}

		return size;
	}

/**

* Gets the sum of nodes in the specified list;

* Fox example, if list is {33,55,77,99}, the size(list) will be return 254;

* @param list

* @return

*/

static int sum(Node list){
		int sum = 0;
		while(list != null){
			sum += list.data;
			list = list.next;
		}

		return sum;
	}

/**

// precondition: the specified list has at least two nodes;

// postcondition: the last node in the list has been deleted;

For example, if list is {22, 44, 66, 88}, then removeLast(list)will change it to {22, 44,66}.

* @param list

*/

void removeLast(Node list){
		if(list == null || list.next == null)
		{
			//throw new IllegalStatException();
		}
		//{33,55,77,99} 

		while(list.next.next != null) {
			list = list.next;
		}

		list.next = null;
	} 

/**

*

* @param list

* @return

*/

static Node copy(Node list)
	{
		if(list == null)
		{
			return null;
		}

		Node clone = new Node(list.data);
		for (Node p = list, q = clone; p != null; p = p.next, q = q.next)
		{
			q.next = p.next;
		}

		return clone;
	}
	

/**

* Get a new list that contains copies of the p-q nodes of the specified list,

* starting with node number p(starting with 0).

* For example, if listis {22, 33, 44, 55, 66, 77, 88, 99}, then sublist(list, 2, 7)will

* return the new list {44, 55, 66, 77, 88}. Note that the two lists must be completely independent of each other.

* Changing one list should have no effect upon the other.

* @param list

* @param m

* @param n

* @return

*/

Node sublist(Node list, int m, int n) {
		if (m < 0 || n < m) {
			throw new IllegalArgumentException();
		} else if (n == m) {
			return null;
		}

		//55,22,11,33
		for (int i = 0; i < m; i++) {
			list = list.next;
		}
		Node clone = new Node(list.data);
		Node p = list, q = clone;
		for (int i = m + 1; i < n; i++) {
			if (p.next == null) {
				throw new IllegalArgumentException();
			}
			q.next = new Node(p.next.data);
			p = p.next;
			q = q.next;
		}
		return clone;
	}
时间: 2024-10-27 14:22:00

【DataStructure】Some useful methods about linkedList.的相关文章

【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 =

【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】Some useful methods for arrays

Last night it took me about two hours to learn arrays. For the sake of less time, I did not put emphaises on the practice question, just now when reading the book, I found that some methods referred to arrays are so beneficial to us. So in here make

【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

【DataStructure】The difference among methods addAll(),retainAll() and removeAll()

In the Java collection framework, there are three similar methods, addAll(),retainAll() and removeAll().  addAll(), the retainAll(), and the removeAll()methods are equivalent to the set theoretic union,  intersection, and complement operators, respec

【DataStructure】Description and usage of queue

[Description] A queue is a collection that implements the first-in-first-out protocal. This means that the only accessiable object in the collection in the first one that was inserted. The most common example of a queue is a waiting line. [Interface]

【DataStructure】Another usage of List: Polynomial

Statements: This blog was written by me, but most of content  is quoted from book[Data Structure with Java Hubbard] [Description] Apolynomialis a mathematical function of the form: p(x) = a0xn+ a1xn–1+a2xn–2+ ???+an–1x + an The greatest exponent, 

【DataStructure】Charming usage of Set in the java

In an attempt to remove duplicate elements from list, I go to the lengths to take advantage of  methods in the java api. After investiagting the document of java api, the result is so satisfying that I speak hightly of wisdom of developer of java lan

【DataStructure】A useful util class for reading and writing files

Faced with the upcoming exam, Some useful methods referred to file operation drew tremenous attention. Now I make a summary to reading file. [java] view plaincopy import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; impo