Some useful methods about into list.

Source: Internet
Author: User
/**
* Method 1: Delete the input element x
* And meanwhile keep the length of array after deleted n
* @ Param a 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 {254, 55,}, the size (list) will be return;
* @ 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 shocould 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,33for (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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.