[CS61B-Sp18] lists

Lists

SLList

Intro

  • Add a middle man that supervise the list
  • Sentinel pointer always points to the dummy start node.
  • Caching the size to avoid traversl when adding/removing.
  • Caching the last pointer to avoid traversal when adding the last.

Basic: SLList with sentinel and caching size

在这里插入图片描述

 /** An SLList is a list of integers, which hides the terrible truth
   * of the nakedness within. */
public class SLList {  
   private static class IntNode {
      public int item;
      public IntNode next;

      public IntNode(int i, IntNode n) {
         item = i;
         next = n;
         System.out.println(size);
      }
   } 

   /* The first item (if it exists) is at sentinel.next. */
   private IntNode sentinel;
   private int size;

   /** Creates an empty SLList. */
   public SLList() {
      sentinel = new IntNode(63, null); //do not worry about dummy 63
      size = 0;
   }

   public SLList(int x) {
      sentinel = new IntNode(63, null);
      sentinel.next = new IntNode(x, null);
      size = 1;
   }

   /** Adds x to the front of the list. */
   public void addFirst(int x) {
      sentinel.next = new IntNode(x, sentinel.next);
      size = size + 1;
   }

   /** Returns the first item in the list. */
   public int getFirst() {
      return sentinel.next.item;
   }

   /** Adds x to the end of the list. */
   public void addLast(int x) {
      size = size + 1;      
      IntNode p = sentinel;
      /* Advance p to the end of the list. */
      while (p.next != null) {
         p = p.next;
      }
      p.next = new IntNode(x, null);
   }
   
   /** Returns the size of the list. */
   public int size() {
      return size;
   }
}

Improvement1: SLList with sentinel, caching size and last

在这里插入图片描述

DLList

Intro

  • Need DLList to make removing last fast

Basic1: DLList with double sentinels

  • the double sentinels approach avoids special case
    在这里插入图片描述

Basic2: DLList with single circular sentinel

  • the circular sentinel approach avoids special case
    在这里插入图片描述

Array

Intro

  • Think of arrays as numbered sequence of memory, whereas lists as named memory.
  • Think of arrays as instance variables.
  • Default value is zero not garbage.
  • Garbage collection.

Creation

  • x = new int[3]; //initialize with all zeros
    
  • y = new int[]{1, 2, 3, 4, 5};
    
  • int[] z = {9, 10, 11, 12, 13};
    

Array of strings

在这里插入图片描述

/* Think of array of strings as 2-D array */
String[] s = new String[6];
s[4] = "ketchup";
s[2] = "muffins";

AList

Intro

在这里插入图片描述

  • Resizing by multipling by 2 is the fastest way.
  • Assigning to null after removing invokes garbage collection, which is helpful for large list objects.
  • Resize() should be private.
  • When adding item, check if resize is needed.

AList Implementation

/* Invariants:
 addLast: The next item we want to add, will go into position size
 getLast: The item we want to return is in position size - 1
 size: The number of items in the list should be size.
*/
public class AList {
    private int[] items;
    private int size;

    /** Creates an empty list. */
    public AList() {
        items = new int[100];
        size = 0;
    }

    /** Resizes the underlying array to the target capacity. */
    private void resize(int capacity) {
        int[] a = new int[capacity];
        System.arraycopy(items, 0, a, 0, size);
        items = a;
    }

    /** Inserts X into the back of the list. */
    public void addLast(int x) {
        if (size == items.length) {
            resize(size * 2);
        }

        items[size] = x;
        size = size + 1;
    }

    /** Returns the item from the back of the list. */
    public int getLast() {
        return items[size - 1];
    }
    /** Gets the ith item in the list (0 is the front). */
    public int get(int i) {
        return items[i];
    }

    /** Returns the number of items in the list. */
    public int size() {
        return size;
    }

    /** Deletes item from back of the list and
      * returns deleted item. */
    public int removeLast() {
        int x = getLast();
        size = size - 1;
        return x;
    }
} 

Generic AList Syntax

Glorp[] items = (Glorp []) new Object[8];
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值