【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;
}
【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 { ...
- 【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 ...
- 【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 emph ...
- 【DataStructure】Description and usage of queue
[Description] A queue is a collection that implements the first-in-first-out protocal. This means th ...
- 【DataStructure】Description and Introduction of Tree
[Description] At ree is a nonlinear data structure that models a hierarchical organization. The char ...
- 【DataStructure】One of queue usage: Simulation System
Statements: This blog was written by me, but most of content is quoted from book[Data Structure wit ...
- 【DataStructure】The difference among methods addAll(),retainAll() and removeAll()
In the Java collection framework, there are three similar methods, addAll(),retainAll() and removeAl ...
- 【Scala】Scala之Methods
一.前言 前面学习了Scala的Class,下面接着学习Method(方法). 二.Method Scala的方法与Java的方法类似,都是添加至类中的行为,但是在具体的实现细节上差异很大,下面展示一 ...
- 【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 meth ...
随机推荐
- GOOGLE定位
GOOGLE定位 /// <author>cxg 2018-3-27</author> unit umap; interface uses System.SysUtils, S ...
- google支付回调验证
原文链接: https://my.oschina.net/lemonzone2010/blog/398736 Google支付问题 20150218,挂机的日本服务器出现google支付被刷单现象,虽 ...
- ASP.NET Core 1.0基础之应用启动
来源https://docs.asp.net/en/latest/fundamentals/startup.html ASP.NET 5 使得应用对每个http请求有完整的控制权.Startup类是程 ...
- EntityFramework:我想我需要和 Session.Save 语义一样的方法
背景 EntityFramework 中 DbSet.Add 方法不会导致立即执行 insert 语句,这在长事务中非常有用,不过多数用例都是短事务的,为何我需要一个立即执行 insert 语句的方法 ...
- 《iOS应用逆向工程:分析与实战》
<iOS应用逆向工程:分析与实战> 基本信息 作者: 沙梓社 吴航 刘瑾 丛书名: 信息安全技术丛书 出版社:机械工业出版社 ISBN:9787111450726 上架时间:2 ...
- hdu 1398
题目大意:输入是一个整数.输出他的拆分数(即拆分的方案数),本题与1028最大的不同之处就在于他的面额只能是整数的平方 代码如下: /* * 1398_1.cpp * * Created on: 20 ...
- Openwrt WIFI探针开发【一】
2017.9.26 公开源码(Apache2.0协议) https://github.com/769484623/WiFiProbe ————————————————————————————————— ...
- MFC中显示图像的放大、缩小、移动功能
StretchBlt函数直接对图片进行放大,缩小,显示位置变换. 这个函数有两种形态一种全局函数是这样的: BOOL StretchBlt(HDC hdcDest, int nXOriginDest ...
- http响应报文,如果响应的内容比较大,客户端怎么样判断接收完了呢?
1. http协议有正文大小说明的content-length 2. 或者分块传输chunked的话 读到0\r\n\r\n 就是读完了 ---------------------------- ...
- 当Intellij IDEA 遇到 Mac
当Intellij IDEA 遇到 Mac alt+insert变为control+N