เมื่อเทศกาลฤดูใบไม้ผลิเข้าใกล้โครงการก็สิ้นสุดลงและเราทุกคนกำลังรอที่จะกลับบ้านในปีใหม่ ด้านล่างนี้เป็นความรู้ที่เกี่ยวข้องของบรรณาธิการที่ศึกษาโครงสร้างข้อมูลสำหรับคุณ รายการที่เชื่อมโยงเป็นโครงสร้างข้อมูลที่มักใช้ ตอนนี้ฉันจะแสดงการใช้งานของตัวเองดังนี้ ยินดีต้อนรับสู่ The Great Master เพื่อขอคำแนะนำ
เวอร์ชันแรกไม่มีโหนดล่าสุดและเริ่มข้ามจากโหนดรูททุกครั้ง
LinkedList ระดับสาธารณะ <E> {หัวโหนดส่วนตัว; LinkedList สาธารณะ () {} สาธารณะ e getFirst () {ถ้า (head == null) {return null;} return head.value;} public LinkedList <e> addfirst (e e) {head.pre = new node ( addNode (e e) {node lst = head; if (lst == null) {this.head = โหนดใหม่ (e, null, null); return;} else {ในขณะที่ (จริง) {ถ้า (lst.next == null) {break; this;}} Public LinkedList <e> ลบ (e e) {node lst = head; if (lst == null) {โยน nullpointerexception ใหม่ ("LinkedList ว่างเปล่า");} else {ในขณะที่ (จริง) {ถ้า (e.equals (lst.value)) if (lst.pre! = null) {lst.pre.next = lst.next;} ถ้า (lst.next! = null) {lst.next.pre = lst.pre;} lst = null; break;} lst = lst.next; StringBuffer ("["); node lst = this.head; ในขณะที่ (lst! = null) {buff.append (lst.value+","); lst = lst.next;} return buff.substring (0, buff.length ()-1)+"]"; ค่าโหนดก่อนโหนดถัดไป) {this.value = value; this.pre = pre; this.next = next;}}}}เวอร์ชันที่สองมีโหนดล่าสุด
LinkedList คลาสสาธารณะ <e> {หัวโหนดส่วนตัวโหนดส่วนตัวสุดท้าย; LinkedList สาธารณะ () {} สาธารณะ e getfirst () {ถ้า (head == null) {return null;} return head.value;} public e getlast () {ถ้า (สุดท้าย == null) {return null; e) {head.pre = new node (e, null, head); head = head.pre; return this;} public linkedList <e> addNode (e e) {node lst = สุดท้าย; ถ้า (lst == null) {// ถ้าโหนดสุดท้ายว่างเปล่า this.head = this.last; return this;} else {ในขณะที่ (true) {ถ้า (lst.next == null) {// break;} else {lst = lst.next;}} lst.next = node node (e, lst, null); last = lst.next; lst = head; if (lst == null) {โยน nullpointerexception ใหม่ ("linkedList ว่างเปล่า");} else {ในขณะที่ (จริง) {ถ้า (e.equals (lst.value)) {// ลบองค์ประกอบนี้ if (lst.pre! = null) {lst.pre.next = lst.next;} ถ้า (lst.next! = null) {lst.next.pre = lst.pre;} lst = null; break;} lst = lst.next; StringBuffer ("["); node lst = this.head; ในขณะที่ (lst! = null) {buff.append (lst.value+","); lst = lst.next;} return buff.substring (0, buff.length ()-1)+"]"; ค่าโหนดก่อนโหนดถัดไป) {this.value = value; this.pre = pre; this.next = next;}}}}หมายเหตุ: ทั้งสองรุ่นข้างต้นไม่พิจารณาการใช้งานภายใต้มัลติเธรด
ข้างต้นคือความรู้ที่เกี่ยวข้องเกี่ยวกับการใช้รายการที่เชื่อมโยงแบบสองทิศทาง (สองเวอร์ชัน) แนะนำให้คุณรู้จักโดยบรรณาธิการ ฉันหวังว่ามันจะเป็นประโยชน์กับคุณ