【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 ...
随机推荐
- CocoaAsyncSocket 与 Java服务 交互
注意:向客户端写数据时最后需要加上\n,不然很久都不会得到服务端的返回. 上面为普通的socket服务端,最近项目采用apache mina框架建后台的socket服务端,采用上面的asyncSock ...
- C++入门级 一
如果您想学习电脑编程,却又不知从何入手,那么您不妨看看下面的几种学习方案,可能会给您一些启示吧! 方案一 Basic语言 & Visual Basic 优点 (1)Basic 简单易学,很容易 ...
- 【转载】Java并发编程:volatile关键字解析 by 海子
volatile这个关键字可能很多朋友都听说过,或许也都用过.在Java 5之前,它是一个备受争议的关键字,因为在程序中使用它往往会导致出人意料的结果.在Java 5之后,volatile关键字才得以 ...
- VS收集插件
有空再做细细解说,大部分童鞋应该都在用1.Achievements这个就是传说中的VS成就插件了,一边编程一边解锁成就 2.Spell Checker拼写检查插件,这个插件能够实时帮你检查注释或字符串 ...
- 融合libevent和protobuf
写了一个简单的例子,把libevent中的bufferevent网络收发服务和protobuf里面的序列反序列结合起来. protobuf文件message.proto: message PMessa ...
- PowerDesigner教程系列(三)概念数据模型
目标: 本文主要介绍属性的标准检查约束.如何定义属性的附加检查. 一.定义属性的标准检查约束 标准检查约束是一组确保属性有效的表达式.在实体属性的特性窗口,打开如图所示的检查选项卡. 在这个选项卡可以 ...
- Ubuntu 所需要的中文字体美化操作步骤
中文字体美化是个很讨厌的事情,无数初学者在这里面浪费了无数时间,做了无数没有意义的事情.但这也是不得不做的,我把 Debian/Ubuntu 所需要的中文字体美化操作步骤详细记录在这里,希望能节约大家 ...
- vnstat 查看服务器带宽统计命令
vnStat是一个Linux下的网络流量监控软件,它记录指定网卡每日的传输流量日志. 它并非基于网络包的过滤,而是分析文件系统- /proc, 所以vnStat无需root的权限就可使用. ,它还自带 ...
- 超链接的禁用属性Disabled了解
可以设置超链接的Disabled属性的true 和 false来确定超链接是不是能点击 例如: <a herf='http://www.baidu.com' onclick='return cl ...
- mysql5.0.x统计每秒增删改查替换数及系统每秒磁盘IO
转载自:http://blog.chinaunix.net/uid-9370128-id-393082.html 1. mysql 5.0.x 统计每秒增,删,改,查,替换数 mysql 的show ...