zoukankan      html  css  js  c++  java
  • 链表(四)

    假设你要遍历一张链表,然后需要的每个链接点上执行一些操作。例如一张链表存储职工表,需要提高拿最低工资员工的薪水,这样无法做出来。

    若是数组,这种操作很容易。可以用数组下标跟踪每个数据项,并进行操作,再指向下一个坐标。链表虽然可以用 find() 找出指定的链接点,但是这种需要频繁比较,而且多次遍历。

    1     for(int i = 0 ; i < emps.length; i++){
    2         Emp emp = emps[i];
    3         if(emp.getSalary() < 1800){
    4           emp.setSalary(1800);
    5         }
    6     }

    迭代器:迭代器类可以包含对数据结构中的数据项引用,并用来遍历这些结构的对象。

     1 public class Link {
     2 
     3     private int iData;
     4     private double dData;
     5     private Link next;
     6 
     7     public Link(int i, double d) {
     8       this.iData = i;
     9       this.dData = d;
    10     }
    11 
    12     public void displayLink() {
    13       System.out.println("{ " + iData + " , " + dData + " }");
    14     }
    15 
    16     public Link getNext() {
    17       return next;
    18     }
    19 
    20     public void setNext(Link next) {
    21       this.next = next;
    22     }
    23 
    24     public int getiData() {
    25       return iData;
    26     }
    27 
    28     public double getdData() {
    29       return dData;
    30     }
    31 
    32 }
     1 public class LinkList {
     2 
     3     private Link first;
     4 
     5     public boolean isEmpty() {
     6       return first == null;
     7     }
     8 
     9     public void insertFirst(int i, double d) {
    10       Link newLink = new Link(i, d);
    11       newLink.setNext(first);
    12       first = newLink;
    13     }
    14 
    15     public Link getFirst() {
    16       return first;
    17     }
    18 
    19     //这种方式比较危险
    20     public void setFirst(Link f) {
    21       first = f;
    22     }
    23 
    24     public ListIterator getIterator() {
    25       return new ListIterator(this);
    26     }
    27 
    28     public void displayList() {
    29       Link current = first;
    30       System.out.println("List (First --> Last)");
    31       while (current != null) {
    32           current.displayLink();
    33           current = current.getNext();
    34       }
    35     }
    36 
    37 }
     1 public class ListIterator {
     2 
     3     private Link current;
     4     private Link pervious;
     5     private LinkList ourList;
     6 
     7     public ListIterator(LinkList ourList) {
     8         this.ourList = ourList;
     9         reset();
    10     }
    11 
    12     public void reset() {
    13         current = ourList.getFirst();
    14         pervious = null;
    15     }
    16 
    17     public boolean hasNext() {
    18         return (current.getNext() == null);
    19     }
    20 
    21     public void next() {
    22         pervious = current;
    23         current = current.getNext();
    24     }
    25 
    26     public Link getCurrent() {
    27         return current;
    28     }
    29 
    30     public void insertAfter(int i, double d) {
    31         Link newLink = new Link(i, d);
    32         if (ourList.isEmpty()) {
    33             ourList.setFirst(newLink);
    34             current = newLink;
    35         } else {
    36             newLink.setNext(current.getNext());
    37             current.setNext(newLink);
    38             next();
    39         }
    40     }
    41 
    42     public void insertBefore(int i, double d) {
    43         Link newLink = new Link(i, d);
    44         if (ourList.isEmpty()) {
    45             ourList.setFirst(newLink);
    46             current = newLink;
    47         } else {
    48             pervious.setNext(newLink);
    49             newLink.setNext(current);
    50             current = newLink;
    51         }
    52     }
    53 
    54     public Link deleteCurrent() {
    55         if (pervious != null) {
    56             pervious.setNext(current.getNext());
    57             if (hasNext()) {
    58                 reset();
    59             } else {
    60                 current = current.getNext();
    61             }
    62         } else {
    63             ourList.setFirst(current.getNext());
    64             reset();
    65         }
    66         return current;
    67     }
    68 }
     1     public static void main(String[] args) {
     2       LinkList ll = new LinkList();
     3       ll.insertFirst(10, 10.5);
     4       ll.insertFirst(9, 9.5);
     5       ll.insertFirst(8, 8.5);
     6       ll.insertFirst(7, 7.5);
     7       ll.insertFirst(6, 6.5);
     8       ll.insertFirst(5, 5.5);
     9       ll.displayList();
    10       System.out.println("-------------------------");
    11       ListIterator iterator = ll.getIterator();
    12       while(iterator.hasNext()){
    13           iterator.next().displayLink();
    14       }
    15       System.out.println("-------------------------");
    16       iterator.reset();
    17       iterator.insertAfter(77, 77.5);
    18       iterator.insertBefore(88, 88.5);
    19       ll.displayList();
    20     }

    打印结果:
    List (First --> Last)
    { 5 , 5.5 }
    { 6 , 6.5 }
    { 7 , 7.5 }
    { 8 , 8.5 }
    { 9 , 9.5 }
    { 10 , 10.5 }
    -------------------------
    { 5 , 5.5 }
    { 6 , 6.5 }
    { 7 , 7.5 }
    { 8 , 8.5 }
    { 9 , 9.5 }
    { 10 , 10.5 }
    -------------------------
    List (First --> Last)
    { 5 , 5.5 }
    { 88 , 88.5 }
    { 77 , 77.5 }
    { 6 , 6.5 }
    { 7 , 7.5 }
    { 8 , 8.5 }
    { 9 , 9.5 }
    { 10 , 10.5 }

  • 相关阅读:
    top指令
    Trie
    最大公约数
    angular2 获取到的数据无法实时更新的问题
    npm install 的时候出现 write access 导致不能成功安装的问题
    angular 的 @Input、@Output 的一个用法
    windows 安装 apache 服务以及添加 php 解析
    php 性能优化之opcache
    intellij 插件结构(文件结构以及概念层面上的结构)
    jetBrains 插件开发第一课-- 在主菜单栏新增一个菜单
  • 原文地址:https://www.cnblogs.com/xuekyo/p/2775152.html
Copyright © 2011-2022 走看看