首页 > 学院 > 开发设计 > 正文

什么时候选择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(); }}

供后续方便查阅


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表