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.

  1. Solution 1:
  2. static int get(Node list, int i) {
  3. if (i < 0) {
  4. throw new IllegalArgumentException();
  5. }
  6. for (int j = 0; j < i; j++) {
  7. if (list == null) {
  8. throw new IllegalStateException();
  9. }
  10. list = list.next;
  11. }
  12. return list.data;
  13. }
  14. Solution 2:
  15. static int get(Node list, int i)
  16. {
  17. Node p = list;
  18. int j = 0;
  19. while (j < i && p != null) {
  20. ++j;
  21. p = p.next;
  22. }
  23. if(p == null)
  24. {
  25. throw new java.util.NoSuchElementException();
  26. }
  27. return p.data;
  28. }

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.

  1. Solution 1:
  2.  
  3. static void put(Node list, int i, int x) {
  4. if (list == null) {
  5. throw new java.util.NoSuchElementException("List is Empty");
  6. } else if (i ==0) {
  7. list.next = new Node(list.data,list);
  8. list.data = x;
  9. } else {
  10. Node p = list;
  11. int j = 1;
  12. while (j < i && p != null) {
  13. ++j;
  14. p = p.next;
  15. }
  16. if (p == null)
  17. {
  18. String error = String.format("the list has only %d elements", j-1);;
  19. throw new java.util.NoSuchElementException(error);
  20. }
  21. p.next = new Node(x, p.next);
  22. }
  23. }

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}.

  1. static void swap(Node list, int i, int j) {
  2. if (i < 0 || j < 0) {
  3. throw new IllegalArgumentException();
  4. } else if (i == j) {
  5. return;
  6. }
  7. Node p = list, q = list;
  8. for (int ii = 0; ii < i; ii++) {
  9. if (p == null) {
  10. throw new IllegalStateException();
  11. }
  12. p = p.next;
  13. }
  14. for (int jj = 0; jj < j; jj++) {
  15. if (q == null) {
  16. throw new IllegalStateException();
  17. }
  18. q = q.next;
  19. }
  20. int pdata = p.data, qdata = q.data;
  21. p.data = qdata;
  22. q.data = pdata;
  23. return;
  24. }

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.

  1. static Node merged(Node list1, Node list2) {
  2. Node list = new Node(0);
  3. Node p = list, p1 = list1, p2 = list2;
  4. while (p1 != null && p2 != null) {
  5. if (p1.data < p2.data) {
  6. p = p.next = new Node(p1.data);
  7. p1 = p1.next;
  8. } else {
  9. p = p.next = new Node(p2.data);
  10. p2 = p2.next;
  11. }
  12. }
  13. while (p1 != null) {
  14. p = p.next = new Node(p1.data);
  15. p1 = p1.next;
  16. }
  17. while (p2 != null) {
  18. p = p.next = new Node(p2.data);
  19. p2 = p2.next;
  20. }
  21. return list.next;
  22. }

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. Nginx 进程间通信

    Linux下的IPC非常多,nginx的进程都是有亲缘关系的进程,对于他们的通信我们选择TCP socket进行通信.   TCP socket 用来做进程通信的优点有,   1.socket是文件描 ...

  2. linux编程进阶书推荐APUE,UNP

    编程进阶这里强烈推荐<unix环境高级编程>(简称APUE)和<unix网络编程>(简称UNP),这两本书可是经典中的经典啊,作 者是大名鼎鼎的 W.Richard Steve ...

  3. hdu4553(线段树)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4553 线段树功能:update:区间替换 query:询问满足条件的最左断点 分析:poj3667的加 ...

  4. Java学习之道:空指针错误求解救????????????

    运行下面这个主方法红色代码的地方就报如下错: https://api.weibo.com/2/users/show.json?access_token=2.00szM9cCc4R_EC6ebcf150 ...

  5. WebSocket API

    WebSocket API 这一章介绍如何用WebSocket API来控制协议和创建应用,运用http://websocket.org 提供的现有WebSocket服务器,我们可以收发消息.创建一些 ...

  6. 在Java中如何使用jdbc连接Sql2008数据库(转)

    我们在javaEE的开发中,肯定是要用到数据库的,那么在javaEE的开发中,是如何使用代码实现和SQL2008的连接的呢?在这一篇文章中,我将讲解如何最简单的使用jdbc进行SQL2008的数据库的 ...

  7. 后台使用oracle前台使用easyui分页机制

    前台easyui 的datagrid中设置分页属性: pagination:true,//显示分页 pagePosition:'bottom',//分页栏位置 both 上下 bottom.top p ...

  8. 如何让HTML在手机上实现直接拨打电话以及发送短信?

    拨打电话的HTML实现方式: <a href="tel:134289210xx″>拨打电话</a> 上面是比较常用的方式,但是有可能在某些场景下是支持不太好,可以试用 ...

  9. java该HashTable,HashMap和HashSet

    同一时候我们也对HashSet和HashMap的核心方法hashcode进行了具体解释,见<探索equals()和hashCode()方法>. 万事俱备,那么以下我们就对基于hash算法的 ...

  10. PHP计算中文字符串长度 、截取相应中文字符串

    PHP计算字符串长度     及其   截取相应中文字符串 计算字符长度: $gouWu = '美日汇http://www.hnzyxok.com/'; echo mb_strlen($gouWu,' ...