最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

java模擬雙向鏈表

2022-08-05 15:12 作者:虛云幻仙  | 我要投稿

/**
* 自定義簡易雙向鏈表
*/

public class CustomizeLinkedList<E> {
? ?private class Node<E>{
? ? ? ?Node<E> prev;
? ? ? ?E item;
? ? ? ?Node<E> next;

? ? ? ?public Node(Node<E> prev, E item, Node<E> next) {
? ? ? ? ? ?this.prev = prev;
? ? ? ? ? ?this.item = item;
? ? ? ? ? ?this.next = next;
? ? ? ?}
? ?}
? ?private int size;
? ?Node<E> first;
? ?Node<E> last;

? ?public int size(){
? ? ? ?return size;
? ?}
? ?public boolean add(E e){
? ? ? ?linkLast(e);
? ? ? ?return true;
? ?}
? ?private void linkLast(E e){
? ? ? ?Node<E> n = new Node<>(last,e,null);
? ? ? ?if (isEmpty()){
? ? ? ? ? ?first = n;
? ? ? ?}else {
? ? ? ? ? ?last.next = n;
? ? ? ?}
? ? ? ?last = n;
? ? ? ?size++;
? ?}
? ?public boolean isEmpty(){
? ? ? ?return size==0;
? ?}
? ?public E remove(int index){
? ? ? ?checkIndex(index);
? ? ? ?Node<E> n = getNode(index);
? ? ? ?Node<E> pr = n.prev;
? ? ? ?Node<E> ne = n.next;
? ? ? ?if (pr == null){
? ? ? ? ? ?first = ne;
? ? ? ?}else {
? ? ? ? ? ?pr.next = ne;
? ? ? ?}
? ? ? ?if (ne == null){
? ? ? ? ? ?last = pr;
? ? ? ?}else {
? ? ? ? ? ?ne.prev = pr;
? ? ? ?}
? ? ? ?E e = n.item;
? ? ? ?n.prev = null;
? ? ? ?n.next = null;
? ? ? ?n.item = null;
? ? ? ?size--;
? ? ? ?return e;
? ?}
? ?private void checkIndex(int index){
? ? ? ?if (index<0||index>=size)throw new IndexOutOfBoundsException();
? ?}
? ?private Node<E> getNode(int index){
? ? ? ?Node<E> n;
? ? ? ?if (index < size>>1){
? ? ? ? ? ?n = first;
? ? ? ? ? ?for (int i = 0;i<index;i++){
? ? ? ? ? ? ? ?n = n.next;
? ? ? ? ? ?}
? ? ? ?}else {
? ? ? ? ? ?n = last;
? ? ? ? ? ?for (int i = size-1;i>index;i--){
? ? ? ? ? ? ? ?n = n.prev;
? ? ? ? ? ?}
? ? ? ?}
? ? ? ?return n;
? ?}
? ?public E set(int index,E e){
? ? ? ?checkIndex(index);
? ? ? ?Node<E> n = getNode(index);
? ? ? ?E oldE = n.item;
? ? ? ?n.item = e;
? ? ? ?return oldE;
? ?}
? ?public E get(int index){
? ? ? ?checkIndex(index);
? ? ? ?return getNode(index).item;
? ?}
? ?public void addFirst(E e){
? ? ? ?Node<E> n = new Node<>(null,e,first);
? ? ? ?if (isEmpty()){
? ? ? ? ? ?last = n;
? ? ? ?}else {
? ? ? ? ? ?first.prev = n;
? ? ? ?}
? ? ? ?first = n;
? ? ? ?size++;
? ?}
? ?public void addLast(E e){
? ? ? ?linkLast(e);
? ?}

? ?public static void main(String[] args) {
? ? ? ?CustomizeLinkedList<String> ll1 = new CustomizeLinkedList<>();
? ? ? ?//測試略
? ?}
}

java模擬雙向鏈表的評論 (共 條)

分享到微博請遵守國家法律
吉隆县| 永泰县| 崇阳县| 柏乡县| 富阳市| 昌邑市| 长沙县| 阳朔县| 若尔盖县| 三台县| 独山县| 申扎县| 贵定县| 宝兴县| 高州市| 涟源市| 汉川市| 莱芜市| 上犹县| 嵊泗县| 象山县| 香河县| 隆林| 白山市| 屯门区| 岑巩县| 景德镇市| 富锦市| 城步| 南岸区| 宿迁市| 关岭| 天峨县| 凤阳县| 喀什市| 安丘市| 保靖县| SHOW| 耒阳市| 花莲县| 余江县|