当前位置:网站首页>CPT 102_LEC 13-14
CPT 102_LEC 13-14
2022-06-11 01:52:00 【NONE_WHY】
1. List using linked nodes with header
- LinkedList extends AbstractList
- Has fields for linked list of Nodes and count
- Has an inner class: Node, with public fields
- get(index), set(index, item)
- loop to index’th node, then get or set value
- add(index, item), remove(index)
- deal with special case of index == 0
- loop along list to node one before index’th node then add or remove
- check if go past end of list
- remove(item)
- deal with special case of item in first node (i.e. conversion into empty set after removal)
- loop along list to node one before node containing item then remove
- check if go past end of list
2. Code
import java.util.AbstractList; public class LINKLIST { } class LinkedList<E> extends AbstractList<E>{ private Node<E> data; private int count; public LinkedList(){ this.data = null; this.count = 0; } private static class Node<E>{ public E value; public Node<E> next; public Node(E val, Node<E> node){ value = val; next = node; } } //Get public E get(int index){ if (index < 0) { throw new IndexOutOfBoundsException(); } Node<E> node=data; int i = 0; // position of node while (node!=null && i++ < index) { node=node.next; } if (node==null) { throw new IndexOutOfBoundsException(); } return node.value; } //Set public E set(int index, E value){ if (index < 0) { throw new IndexOutOfBoundsException(); } Node<E> node=data; int i = 0; // position of node while (node!=null && i++ < index) { node=node.next; } if (node==null) { throw new IndexOutOfBoundsException(); } E ans = node.value; node.value = value; return ans; } public void add(int index, E item){ if (item == null) { throw new IllegalArgumentException(); } if (index==0){ // add at the front. data = new Node<E>(item, data); count++; return; } Node<E> node=data; int i = 1; // position of next node while (node!=null && i++ < index) { node=node.next; } if (node == null) { throw new IndexOutOfBoundsException(); } node.next = new Node<>(item, node.next); count++; } public boolean remove (Object item){ if (item==null || data==null) return false; if (item.equals(data.value)) { data = data.next; } else { Node<E> node = data; while (node.next!=null && !node.next.value.equals(item)) { node=node.next; } if (node.next==null) { return false; } node.next = node.next.next; } count--; return true; } @Override public int size() { return count; } }
边栏推荐
- 378. 有序矩阵中第 K 小的元素
- Jetpack compose scaffold and bottomappbar (bottom navigation)
- Bingbing learning notes: find the greatest common divisor and the least common multiple. Complex version reverse string
- Databinding escaping with presentation symbols
- Knowledge competition of safety production month -- how much do you know about new safety law
- Multilevel mesoporous organometallic framework material zif-8 loaded with lactic acid oxidase (LOD) / ferric oxide (Fe304) / doxorubicin / insulin /cas9 protein / metronidazole / emodin methyl ether
- 92. actual combat of completable future
- 2022 high altitude installation, maintenance and removal of simulated examination platform of theoretical question bank
- 【AI周报】AI与冷冻电镜揭示「原子级」NPC结构;清华、商汤提出「SIM」方法兼顾语义对齐与空间分辨能力
- SQL | 计算总和
猜你喜欢

【斐波那契数列】

Test questions and answers of 2022r1 quick opening pressure vessel operation certificate

The diligent is the laziest

378. 有序矩阵中第 K 小的元素

Unity HTC and Pico are the same

Jetpack compose box control

helm 部署traefik ingress

When a logical deletion encounters a unique index, what are the problems and solutions?

Jetpack compose scaffold and bottomappbar (bottom navigation)

C language principle explanation and code implementation of scalable / reduced thread pool
随机推荐
Colab reported an error: importerror: cannot import name '_ check_ savefig_ extra_ args‘ from ‘matplotlib. backend_ bases‘
MySQL backup and recovery
[untitled]
What is the relationship between precious metal silver and spot Silver
List 过滤、排序、校验等处理方法
Byte beating | the first batch of written examination for game R & D post (question solution)
SQL | 返回顾客名称和相关订单号以及每个订单的总价
A数位dp
Customized redistemplate in redis
13. numeric array
Introduction for i-Teams
Jetpack compose box control
The largest kth element in the array
378. 有序矩阵中第 K 小的元素
SQL | calculate sum
2022 safety officer-a certificate special operation certificate examination question bank and simulation examination
What can the enterprise exhibition hall design bring to the enterprise?
Optimized dispatching (thermal power, wind energy and energy storage) [matlab code implementation]
JS summary of math functions in math objects
A digit DP