Method 4: Gets the value of element number i

For example, if list is {22, 33, 44, 55, 66, 77, 88, 99}, then get(list, 2) will return 44.

Solution 1:
static int get(Node list, int i) {
if (i < 0) {
throw new IllegalArgumentException();
}
for (int j = 0; j < i; j++) {
if (list == null) {
throw new IllegalStateException();
}
list = list.next;
}
return list.data;
}
Solution 2:
static int get(Node list, int i)
{
Node p = list;
int j = 0;
while (j < i && p != null) {
++j;
p = p.next;
}
if(p == null)
{
throw new java.util.NoSuchElementException();
}
return p.data;
}

The output is as follows:

Method 5:inserts x as element number i;

For example, if list is {22, 33, 44, 55, 66, 77, 88, 99}, then put(list, 3, 50) will change List to {22, 33, 44, 50, 55, 66, 44, 88, 99}.

Hint: if i= 0, replace the value of the first node With x, and insert a new node immediately after it that contains the previous fist value.

Solution 1:

	static void put(Node list, int i, int x) {
if (list == null) {
throw new java.util.NoSuchElementException("List is Empty");
} else if (i ==0) {
list.next = new Node(list.data,list);
list.data = x;
} else {
Node p = list;
int j = 1;
while (j < i && p != null) {
++j;
p = p.next;
}
if (p == null)
{
String error = String.format("the list has only %d elements", j-1);;
throw new java.util.NoSuchElementException(error);
}
p.next = new Node(x, p.next);
}
}

The output is as follows:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3hiMDg0MTkwMTExNg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

Method 6:Swap the i element with the j element

For example, if list is {22, 33, 44, 55, 66, 77, 88, 99}, then swap(list, 2, 5) will change List to {22, 33, 77, 55, 66, 44, 88, 99}.

static void swap(Node list, int i, int j) {
if (i < 0 || j < 0) {
throw new IllegalArgumentException();
} else if (i == j) {
return;
}
Node p = list, q = list;
for (int ii = 0; ii < i; ii++) {
if (p == null) {
throw new IllegalStateException();
}
p = p.next;
}
for (int jj = 0; jj < j; jj++) {
if (q == null) {
throw new IllegalStateException();
}
q = q.next;
}
int pdata = p.data, qdata = q.data;
p.data = qdata;
q.data = pdata;
return;
}

The output is as follows:

Method 7: Gets a new list that contains all the elements of list1 and list2 in ascending order. List1 and list2 are both in ascending order.

For example, if list1is {22, 33, 55, 88} and  list2is {44, 66, 77, 99}, then merged(list1, list2)will return the new list {22, 33, 44, 55, 66, 77, 88, 99}.

Note that the three lists should be completely independent of each other. Changing one list should have no effect upon the others.

static Node merged(Node list1, Node list2) {
Node list = new Node(0);
Node p = list, p1 = list1, p2 = list2;
while (p1 != null && p2 != null) {
if (p1.data < p2.data) {
p = p.next = new Node(p1.data);
p1 = p1.next;
} else {
p = p.next = new Node(p2.data);
p2 = p2.next;
}
}
while (p1 != null) {
p = p.next = new Node(p1.data);
p1 = p1.next;
}
while (p2 != null) {
p = p.next = new Node(p2.data);
p2 = p2.next;
}
return list.next;
}

The output is as follows:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3hiMDg0MTkwMTExNg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

版权声明:本文博主原创文章,博客,未经同意不得转载。

【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 1: Delete the input element x  * and meanwhile keep the length of array after deleted n ...

  3. 数据结构之链表(LinkedList)(三)

    数据结构之链表(LinkedList)(二) 环形链表 顾名思义 环形列表是一个首尾相连的环形链表 示意图 循环链表的特点是无须增加存储量,仅对表的链接方式稍作改变,即可使得表处理更加方便灵活. 看一 ...

  4. ArrayList 和 LinkedList的执行效率比较

    一.概念: 一般我们都知道ArrayList* 由一个数组后推得到的 List.作为一个常规用途的对象容器使用,用于替换原先的 Vector.允许我们快速访问元素,但在从列表中部插入和删除元素时,速度 ...

  5. vue系列---理解Vue中的computed,watch,methods的区别及源码实现(六)

    _ 阅读目录 一. 理解Vue中的computed用法 二:computed 和 methods的区别? 三:Vue中的watch的用法 四:computed的基本原理及源码实现 回到顶部 一. 理解 ...

  6. LInkedList总结及部分底层源码分析

    LInkedList总结及部分底层源码分析 1. LinkedList的实现与继承关系 继承:AbstractSequentialList 抽象类 实现:List 接口 实现:Deque 接口 实现: ...

  7. ASM字节码框架学习之动态代理

    ASM字节码操纵框架,可以直接以二进制的形式来来修改已经存在的类或者创建新的类.ASM封装了操作字节码的大部分细节,并提供了非常方便的接口来对字节码进行操作.ASM框架是全功能的,使用ASM字节码框架 ...

  8. Java注解与自己定义注解处理器

    动机 近期在看ButterKnife源代码的时候.竟然发现有一个类叫做AbstractProcessor,并且ButterKnife的View绑定不是依靠反射来实现的,而是使用了编译时的注解,自己主动 ...

  9. Thinking in Java——笔记(17)

    Containers in Depth Full container taxonomy You can usually ignore any class that begins with " ...

随机推荐

  1. gem5 设定checkpiont以及从checkpoint开始运行

    同spec2006中间bzip2一个例子,如何设置checkpoint .以及从checkpoint继续以启动运行.这样做的目的是为了,采纳automic运行N指令,然后detailed运行M指令. ...

  2. SWT中的Tree中 添加右键弹出菜单

    先看一下效果: 如图:在树上单击鼠标右键会弹出 弹出式菜单.做法其实很简单,先做一个树: final TreeViewer treeViewer = new TreeViewer(group, SWT ...

  3. java模拟浏览器包selenium整合了htmlunit,火狐浏览器,IE浏览器,opare浏览器驱

    //如果网页源码中有些内容是js渲染过来的,那你通过HttpClient直接取肯定取不到,但是这些数据一般都是通过异步请求传过来的(一般都是通过ajax的get或者post方式).那么你可以通过火狐浏 ...

  4. 最近纠结致死的一个java报错java.net.SocketException: Connection reset 终于得到解决

    自从SEOTcs系统11月份24日更新了一下SEO得分算法以来,一直困扰我的一个问题出现了,java的数据job任务,在执行过程中会经常报以下的错误: “2011-12-03 18:00:32 Def ...

  5. CSS计数器妙用

    做web的经常会遇到类似排行榜的需求, 特别是要求前n名的样式和后面人不一样. 通常大多数人对于这个需求的做法都是在后端处理好排名名次, 在前端填入内容, 然后针对前n名做特殊的样式处理. 但是这样有 ...

  6. Radmin远程控制软件

    Radmin远程控制软件 日期:2015-08-20     作者:lujl   Radmin是一款快速的远程控制软件,可以用来远程管理公司或个人计算机来实现远程办公.你可以通过鼠标和键盘来控制远程的 ...

  7. Windows Phone开发(31):画刷

    原文:Windows Phone开发(31):画刷 画刷是啥玩意儿?哈,其实画刷是用来涂鸦,真的,没骗你,至于你信不信,反正我信了. 本文通过价绍几个典型的画刷,使你明白画刷就是用来涂鸦的. 一.纯色 ...

  8. HDU ACM 2845 Beans-&gt;动态规划

    意甲冠军: 1. 对于每一行是,对不相邻的同一时间数取: 2.它是相同的列,相邻行不能同时服用: 3.因此,我们可以得到状态方程:dp[i]=dp[i-1]>(dp[i-2]+a[i])?dp[ ...

  9. 三层架构与MVC

    三层简介 先说说Web三层架构这个古老话题.地球人都知道web三层架构是指: • >用户接口层(UI Layer) • >业务逻辑层(Bussiness Layer) • >持久化层 ...

  10. AC automation 模板

    /* 1.对n个字符串构造tire树 insertWord(node *root, char *word); 2.bfs构造fail指针 makeFail(node *root); 3.基于以上两点的 ...