头插法与尾插法建立单链表
生活随笔
收集整理的這篇文章主要介紹了
头插法与尾插法建立单链表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
頭插法與尾插法建立單鏈表
import java.util.*; class Node {int value;Node next;Node(int x) {value = x;} } public class 一個單鏈表 {public static void main(String[] args) {List<Node> list = new LinkedList<>();for (int i = 0; i < 10; i++) {Node node = new Node(i + 1);list.add(node);}Node head = new Node(0);head.next = null;for (int i = 0; i < 10; i++) {list.get(i).next = head.next; // 頭插法head.next = list.get(i);}/* Node rear = head;for (int i = 0; i < 10; i++) {rear.next = list.get(i); // 尾插法list.get(i).next = null;rear = list.get(i);}*/Node dummy = head;while (dummy.next != null) {dummy = dummy.next;System.out.println(dummy.value);}} }總結
以上是生活随笔為你收集整理的头插法与尾插法建立单链表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 项目管理-POC
- 下一篇: 基于Python的药店|药房管理系统的设