zoukankan      html  css  js  c++  java
  • 单向列表(有哨兵结点)

    在链表类中实现这些功能:

    get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1。
    addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
    addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
    addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val  的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
    deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点

    public class ListNode {
      int val;
      ListNode next;
      ListNode(int x) { val = x; }
    }
    
    class MyLinkedList {
      int size;
      ListNode head;  // sentinel node as pseudo-head
      public MyLinkedList() {
        size = 0;
        head = new ListNode(0);
      }
    
      /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
      public int get(int index) {
        // if index is invalid
        if (index < 0 || index >= size) return -1;
    
        ListNode curr = head;
        // index steps needed 
        // to move from sentinel node to wanted index
        for(int i = 0; i < index + 1; ++i) curr = curr.next;
        return curr.val;
      }
    
      /** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
      public void addAtHead(int val) {
        addAtIndex(0, val);
      }
    
      /** Append a node of value val to the last element of the linked list. */
      public void addAtTail(int val) {
        addAtIndex(size, val);
      }
    
      /** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
      public void addAtIndex(int index, int val) {
        // If index is greater than the length, 
        // the node will not be inserted.
        if (index > size) return;
    
        // [so weird] If index is negative, 
        // the node will be inserted at the head of the list.
        if (index < 0) index = 0;
    
        ++size;
        // find predecessor of the node to be added
        ListNode pred = head;
        for(int i = 0; i < index; ++i) pred = pred.next;
    
        // node to be added
        ListNode toAdd = new ListNode(val);
        // insertion itself
        toAdd.next = pred.next;
        pred.next = toAdd;
      }
    
      /** Delete the index-th node in the linked list, if the index is valid. */
      public void deleteAtIndex(int index) {
        // if the index is invalid, do nothing
        if (index < 0 || index >= size) return;
    
        size--;
        // find predecessor of the node to be deleted
        ListNode pred = head;
        for(int i = 0; i < index; ++i) pred = pred.next;
    
        // delete pred.next 
        pred.next = pred.next.next;
      }
    }

     

    哨兵节点:

    哨兵节点在树和链表中被广泛用作伪头、伪尾等,通常不保存任何数据。

    我们将使用伪头来简化我们简化插入和删除。在接下来的两种方法中应用此方法

  • 相关阅读:
    Webbrowser中模拟连接点击(非鼠标模拟)
    用DDE控制Word
    禁止用键盘左右箭头,去切换PageControl页签
    Delphi实现全局鼠标钩子
    Delphi实现软件中登录用户的操作权限
    根据数据库结构生成TreeView
    根据字符串找到函数并执行
    用DLL实现插件的简单演示
    Delphi:窗体的扩展样式GWL_EXSTYLE用于SetWindowLong
    FastReport问题整理(http://129.sqdj.gov.cn/?p=77)
  • 原文地址:https://www.cnblogs.com/focusonoutput/p/13499839.html
Copyright © 2011-2022 走看看