国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

什么時候選擇LinkedList?

2019-11-06 06:06:05
字體:
來源:轉載
供稿:網友

LinkedList即單鏈表,如下圖 這里寫圖片描述 LinkedList優點:

長度可變插入刪除簡單

LinkedList缺點:

不能隨機訪問,必須從head結點順序訪問,所以也就不能夠二分查找存儲pointer消耗內存

下面是LinkedList的簡單實現以及簡單操作(各個位置插入新節點,打印單鏈表):

class LinkedList{ Node head; class Node { int data; Node next; Node(int d) {data = d; next = null; } } /* Inserts a new Node at front of the list. */ public void push(int new_data) { Node new_node = new Node(new_data); new_node.next = head; head = new_node; } /* Inserts a new node after the given PRev_node. */ public void insertAfter(Node prev_node, int new_data) { if (prev_node == null) { System.out.println("The given previous node cannot be null"); return; } Node new_node = new Node(new_data); new_node.next = prev_node.next; prev_node.next = new_node; } /* Appends a new node at the end. This method is defined inside LinkedList class shown above */ public void append(int new_data) { Node new_node = new Node(new_data); if (head == null) { head = new Node(new_data); return; } new_node.next = null; Node last = head; while (last.next != null) last = last.next; last.next = new_node; return; } /* This function prints contents of linked list starting from the given node */ public void printList() { Node tnode = head; while (tnode != null) { System.out.print(tnode.data+" "); tnode = tnode.next; } } public static void main(String[] args) { /* Start with the empty list */ LinkedList llist = new LinkedList(); // Insert 6. So linked list becomes 6->NUllist llist.append(6); // Insert 7 at the beginning. So linked list becomes // 7->6->NUllist llist.push(7); // Insert 1 at the beginning. So linked list becomes // 1->7->6->NUllist llist.push(1); // Insert 4 at the end. So linked list becomes // 1->7->6->4->NUllist llist.append(4); // Insert 8, after 7. So linked list becomes // 1->7->8->6->4->NUllist llist.insertAfter(llist.head.next, 8); System.out.println("/nCreated Linked list is: "); llist.printList(); }}

供后續方便查閱


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 辽源市| 如东县| 星座| 定襄县| 宝山区| 二连浩特市| 财经| 池州市| 鄂尔多斯市| 海兴县| 大同市| 左贡县| 太谷县| 陆丰市| 永仁县| 印江| 威宁| 绥棱县| 桃园县| 屏山县| 榕江县| 石城县| 高唐县| 疏勒县| 封开县| 沙田区| 会泽县| 乐都县| 收藏| 乌拉特前旗| 遂昌县| 大田县| 光山县| 新蔡县| 长丰县| 渝中区| 宁城县| 湘西| 长春市| 新龙县| 波密县|