1 / 18

Single linked list

Single linked list. The structure of node. public class IntNode { public int info; public IntNode next; public IntNode(int i) { this(i,null); } public IntNode(int i, IntNode n) { info = i; next = n; } }. The class of single linked list. public class IntSLList {

Download Presentation

Single linked list

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Single linked list

  2. The structure of node public class IntNode { public int info; public IntNode next; public IntNode(int i) { this(i,null); } public IntNode(int i, IntNode n) { info = i; next = n; } }

  3. The class of single linked list public class IntSLList { private IntNode head, tail; public IntSLList() { head = tail = null; } public boolean isEmpty() { return head == null; }

  4. Insert (1) • public void addToHead(int el) { • head = new IntNode(el,head); • if (tail == null) • tail = head; • }

  5. Insert (2) public void addToTail(int el) { if (!isEmpty()) { tail.next = new IntNode(el); tail = tail.next; } else head = tail = new IntNode(el); }

  6. Delete (1) public int deleteFromHead() { // delete the head and return its info; int el = head.info; if (head == tail) // if only one node on the list; head = tail = null; else head = head.next; return el; } //gc負責garbage collection, 在c語言需free();

  7. Delete (2) public int deleteFromTail() { // delete the tail and return its info; int el = tail.info; if (head == tail) // if only one node in the list; head = tail = null; else { // if more than one node in the list, IntNode tmp; // find the predecessor of tail; for (tmp = head; tmp.next != tail; tmp = tmp.next); tail = tmp; // the predecessor of tail becomes tail; tail.next = null; } return el; }

  8. Delete (3) public void delete(int el) { // delete the node with an element el; if (!isEmpty()) if (head == tail && el == head.info) head = tail = null; // if only one node on the list; else if (el == head.info) // if more than one node on the list; head = head.next; // and el is in the head node; else { // if more than one node in the list IntNode pred, tmp; // and el is in a non-head node; for (pred = head, tmp = head.next; tmp != null && tmp.info != el; pred = pred.next, tmp = tmp.next); if (tmp != null) { // if el was found; pred.next = tmp.next; if (tmp == tail) // if el is in the last node; tail = pred; } }

  9. isInList public boolean isInList(int el) { IntNode tmp; for(tmp = head; tmp != null && tmp.info != el; tmp = tmp.next); return tmp != null; }

  10. Binary search Node binaryListSearch(int key) { Node left=head, mid=head; int m; for(m=0;mid!=null;m++,mid=mid.next); //count nodes While (m!=0){ m=m/2; mid=left; for(int I;i<=m;i++) mid=mid.next; if (key>mid.info) if (m&1)==1) //if m is odd left=mid.next else left=mid; else if (key==mid.info) return mid; } return null; } Binary search之先決條件為何?

  11. Your homework • Circular single linked list? • Reverse the single linked list?

  12. 兩長整數相加-Linked List之應用

  13. 321 654 987 N -321 -654 -987 N 兩長整數相加-Linked List之應用 假設一個鏈結節點所儲存的最大值是999,則長整數N=987654321可用N=987×10002+654×10001+321×10000來表示,而其鏈結串列是: 若N=-987654321,則

  14. 假設兩長整數N1, N2 其中m=1000,而K為N1與 N2中節點數較多者的節點數,而兩者的和為S,則相加時會有五種情形。 ,

  15. 790 468 35 2 N1 N2 258 38 S • N1= 0 或 N2 = 0 若 N1= 0 ,則S = N2 。反之,若 N2= 0 ,則S = N1 。 • N1 > 0 ,且 N2 > 0 C0= 0 Si=(ai+bi+Ci-1)%m Ci=(ai+bi+Ci-1)∕ m ( Ci是整數) 假設N1= 35790,而N2= 2468 則 相加後: S1= (790 + 468 + 0)% 1000 = 258 C1= (790 + 468 + 0)∕ 1000 = 1 S2= (35 + 2 + 1)% 1000  = 38 C2= (35 + 2 + 1)∕ 1000  = 0 所以 S =38258

  16. -760 430 -10 25 N1 N2 • N1< 0 ,且 N2 < 0 只要計算 -(A+B)即可 • N1, N2 其中只有一個小於 0 ,但相加後大於 0 因相加後大於 0 ,所以和第二種情況相同。假設 N1 = -10760, N2 = 25430,則 S1= (-760 + 430 + 0)% 1000 =-330%1000 =670(以10’s complement 表示) C1= (-760 + 430 + 0)∕ 1000 =-330 ∕ 1000 =-1 S2= (-10 + 25 - 1)% 1000  = 14 C2= (-10 + 25 - 1)∕ 1000  = 0

  17. N1, N2其中只有一數小於 0 ,但相加後小於 0 。 首先以第四種情況處理,但是最後會產生一進位-1,然後再加以處理。 假設 N1= -48320 , N2= 24850 S1= (-320+850+0)% 1000 = 530 C1= (-320+850+0) ∕ 1000 = 0 S2= (-48+24+0)% 1000 = -24 % 1000 = 976 (10’s) C2= -24 ∕ 1000 = -1 因為C2= -1,因此再用-1000000 + 976530 = -23470

  18. Homework 11 • 兩長整數加、減 • Using single linked-list • Two long numbers : text boxes • Design an applet

More Related