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

歡迎光臨散文網(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模擬雙向鏈表的評論 (共 條)

分享到微博請遵守國家法律
双牌县| 青铜峡市| 绥芬河市| 宣化县| 柞水县| 泗洪县| 武强县| 丰台区| 和政县| 鄱阳县| 五大连池市| 阿勒泰市| 封丘县| 山西省| 讷河市| 河西区| 许昌县| 咸阳市| 密山市| 奎屯市| 家居| 邵武市| 迁西县| 五家渠市| 美姑县| 彭山县| 东乌| 喀喇| 安顺市| 小金县| 吉林省| 金坛市| 娄底市| 阿鲁科尔沁旗| 镇坪县| 云霄县| 济南市| 榆社县| 夏津县| 荔波县| 凤台县|