博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【DataStructure】Some useful methods about linkedList.
阅读量:6543 次
发布时间:2019-06-24

本文共 2769 字,大约阅读时间需要 9 分钟。

/**
* 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;	}

转载地址:http://ejodo.baihongyu.com/

你可能感兴趣的文章
超时时间已到。在操作完成之前超时时间已过或服务器未响应。 (.Net SqlClient Data Provider)(转)...
查看>>
POJ2538 ZOJ1884 UVA10082 WERTYU【输入输出】
查看>>
HDU5620 KK's Steel(C++语言版)
查看>>
旋转卡壳
查看>>
2016/10/09
查看>>
Luogu_2061_[USACO07OPEN]城市的地平线City Horizon
查看>>
自定义HorizontalScrollView的scrollBar
查看>>
js移动端向左滑动出现删除按钮
查看>>
c++学习笔记和思考
查看>>
27.Docker集群部署
查看>>
DNS保存
查看>>
IOS 多线程02-pthread 、 NSThread 、GCD 、NSOperationQueue、NSRunLoop
查看>>
第一周冲刺第五天博客
查看>>
[LeetCode]Longest Increasing Path in a Matrix
查看>>
C++基础之适配器
查看>>
集合set-深入学习
查看>>
C#语言学习——面向对象的几大原则
查看>>
zk 常用资料整理(转)
查看>>
获取历史K线数据的几个方法
查看>>
第一篇、HTML标签
查看>>