【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 ...
随机推荐
- Ant简单工程的构建
1.在Ant的官方网站http://ant.apache.org/bindownload.cgi下载Ant最新版本(我下载的是apache-ant-1.8.2-bin.zip),Ant无需安装,直接解 ...
- JavaScript程序员必备的5个debug技巧
1. debugger:我以前也说过,你可以在JavaScript代码中加入一句debugger;来手工造成一个断点效果.需要带有条件的断点吗?你只需要用if语句包围它: if (somethingH ...
- java中引用的原理
转自:http://blog.163.com/xubin_3@126/blog/static/112987702200962211145825/ 在Java中的引用类型,是指除了基本的变量类型之外的所 ...
- 能力成熟度模型CMM
能力成熟度模型(Capability Maturity Model,英文缩写为CMM)[1]是 一种开发模型.Carnegie Mellon大学的研究人员从美国国防部合同承包方那里收集数据并加以研究, ...
- 第九章openwrt 703N 网口转串口+串口转网口TTL 数据传输
原生串口 1.WR703N 自带 TTL 电平串口,设备文件为/dev/ttyATH0, 波特率 115200.但是硬件串口 没有接出来,需要自己焊线.破壳. 图 1. 正面图.两根线 TP ...
- 6)Linux程序设计入门--消息管理
)Linux程序设计入门--消息管理 前言:Linux下的进程通信(IPC) Linux下的进程通信(IPC) POSIX无名信号量 System V信号量 System V消息队列 System V ...
- sql数据库出现可疑
USE master GO SP_CONFIGURE 'allow updates',1 RECONFIGURE WITH OVERRIDE GO UPDATE SYSDATABASES SET ST ...
- 给文件夹添加Everyone用户
DOC命令 C# code 1. cacls C:dming /g everyone:f /e /t 这样可以添加 2. cacls C:Program Files客友软件 /g everyone:f ...
- Exception in thread "RMI TCP Connection(idle)" java.lang.OutOfMemoryError: PermGen space
在Eclipse 调试 springside showcase项目中,tomcat报异常 Exception in thread "RMI TCP Connection(idle)" ...
- 纯CSS(无 JavaScript)实现的响应式图像显示
有许多方法可以实现web页面里图像的应答.然而,我碰到的所有方案都使用了JavaScript.这使我疑惑不用JavaScript实现图像响应是否可行. 我提出了下面纯CSS的方案. 它是如何工作的呢? ...