/**

* 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. 解析天气预报JSON数据

    解析天气预报JSON数据 JSON字符串 constjson2 = '{' + #13#10 +'"error":0,' + #13#10 +'"status" ...

  2. Windows 配置 Apache Python CGI

    提示:安装Apache可参考 https://jingyan.baidu.com/article/0eb457e53c019f03f1a905c7.html 1.  打开URL: https://ww ...

  3. python使用游标访问数据

    游标是一种数据访问对象,可用于在表中迭代一组行或者向表中插入新行.游标有三种形式:搜索.插入或更新.游标通常用于读取现有几何和写入新几何. 每种类型的游标均由对应的 ArcPy 函数(SearchCu ...

  4. 深度学习教程Deep Learning Tutorials

    Deep Learning Tutorials Deep Learning is a new area of Machine Learning research, which has been int ...

  5. php 验证身份证号码

    身份证号码的结构 身份证号码是特征组合码,由17位数字本体码和一位校验码组成. 排列顺序从左至右依此为:六位数字地址码,八位数字出生日期码,三位数字顺序码和一位数字校验码. 地址码(前六位数) 表示编 ...

  6. 修改SharePoint 2013中Search Topology时遇到的一些问题以及一些Tips

    这次操作在笔者的场中涉及到5台服务器, CA, APP2, APP3, APP4, APP5. 原本只有CA运行着Search Service Applicaiton, 现在想让APP2-5运行这项服 ...

  7. javascript模拟post提交

    通过js模拟post提交1:请求需要的参数过长,超过get允许的最大长度2:想要隐藏地址栏的参数 //新创建一个form表单document.write('<form name=myForm&g ...

  8. android学习的网站收集

    1. http://mob.com/#/index 提供分享等统一解决方案 2. http://bbs.apkbus.com/explore/ 这个类似的quroa问答模块,覆盖不错.就是人气,稍差. ...

  9. Voice Commands (VCD) Cortana 微软小娜示例

    Cortana 样品 您可以创建自定义功能Cortana使用Cortana技能装备或遗留的声音命令(VCD)平台. 在这里,你可以找到相关的样品: Cortana技能装备 目前Cortana技巧是建立 ...

  10. C#基础视频教程1 背景知识和安装配置

    安装过程比较简单,用虚拟光驱工具加载ISO文件,然后打开EXE安装即可,主要使用VS2013(VS2015也出来了,但是用的还不算多)     建议设置为深色(比较容易看清)   建议显示行号,不要自 ...