/**

* 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.的更多相关文章

  1. 【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 { ...

  2. 【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 ...

  3. 【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 ...

  4. 【DataStructure】Description and usage of queue

    [Description] A queue is a collection that implements the first-in-first-out protocal. This means th ...

  5. 【DataStructure】Description and Introduction of Tree

    [Description] At ree is a nonlinear data structure that models a hierarchical organization. The char ...

  6. 【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 ...

  7. 【DataStructure】The difference among methods addAll(),retainAll() and removeAll()

    In the Java collection framework, there are three similar methods, addAll(),retainAll() and removeAl ...

  8. 【Scala】Scala之Methods

    一.前言 前面学习了Scala的Class,下面接着学习Method(方法). 二.Method Scala的方法与Java的方法类似,都是添加至类中的行为,但是在具体的实现细节上差异很大,下面展示一 ...

  9. 【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 ...

随机推荐

  1. 调用 jdbcTemplate.queryForList 时出现错误 spring-org.springframework.jdbc.IncorrectResultSetColumnCountException

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  2. python接口自动化9-https请求(SSL)

    前言 本来最新的requests库V2.13.0是支持https请求的,但是一般写脚本时候,我们会用抓包工具fiddler,这时候会报:requests.exceptions.SSLError: [S ...

  3. Mac配置PHP开发环境

    安装环境如下: Mac OS 10.10.1 Apache 2.4.9 PHP 5.5.14 MySQL 5.6.22 Apache配置 在Mac OS 10.10.1中是自带Apache软件的,我们 ...

  4. .a 库文件信息查看

    在Linux 下经常需要链接一些 *.a的库文件,那怎么查看这些*.a 中包 含哪些文件.函数.变量: 1. 查看文件:ar -t *.a 2. 查看函数.变量:nm *.a

  5. LaTex 制作表格 合并行\multirow 合并列\multicolumn

    在latex文件最前面用这个包\usepackage{multirow} multirow 宏包提供了 \multirow 命令可以在表格中排版横跨两行以上的文本.命令的格式如下: \multirow ...

  6. TYVJ 2002 扑克牌 题解

    P2002 扑克牌 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 Admin生日那天,Rainbow来找Admin玩扑克牌……玩着玩着Rainbow觉得太没 ...

  7. go语言基础之switch语句 和 fallthrough 用途

    Go里面switch默认相当于每个case最后带有break,匹配成功后不会自动向下执行其他case,而是跳出整个switch, 但是可以使用fallthrough强制执行后面的case代码: 示例1 ...

  8. go语言基础之格式化输出

    1.fmt包的格式化输出输入 格式说明 格式 含义 %% 一个%字面量 %b 一个二进制整数值(基数为2),或者是一个(高级的)用科学计数法表示的指数为2的浮点数 %c 字符型.可以把输入的数字按照A ...

  9. C++实现委托机制(一)

    1.引言: 如果你接触过C#,你就会觉得C#中的delegate(委托)十分灵巧,它的用法上和C\C++的函数指针很像,但是却又比C\C++的函数指针更加灵活.并且委托可以一对多,也就是可以注册多个函 ...

  10. jQuery几个经典表单应用整理回想

    1.文本框获得(失去)焦点 当文本框获得输入焦点时,将该文本框高亮显示,算不得一个应用.仅仅是一个小技巧,能够提高用户体验. [html] view plaincopy <span style= ...