Download presentation
Presentation is loading. Please wait.
Published byBrycen Wallington Modified over 10 years ago
1
Double linked list Lai Ah Fur
2
The structure of node class IntDLLNode { int info; IntDLLNode next = null, prev = null; IntDLLNode() { } IntDLLNode(int el) { info = el; } IntDLLNode(int el, IntDLLNode n, IntDLLNode p) { info = el; next = n; prev = p; } }
3
The class of double linked list public class IntDLList { private IntDLLNode head, tail; public IntDLList() { head = tail = null; } ………… }
4
Insert (1) public void addToDLListHead(int el) { if (!isEmpty()) { head = new IntDLLNode(el,head,null); head.next.prev = head; } Else head = tail = new IntDLLNode(el); }
5
Insert (2) public void addToDLListTail(int el) { if (!isEmpty()) { tail = new IntDLLNode(el,null,tail); tail.prev.next = tail; } else head = tail = new IntDLLNode(el); }
6
Delete (1) public int deleteFromDLListHead() { if (!isEmpty()) { // if at least one node in the list; int el = head.info; if (head == tail) // if only one node in the list; head = tail = null; else { // if more than one node in the list; head = head.next; head.prev = null; } return el; } //if (head==tail) else return 0; }
7
Delete (2) public int deleteFromDLListTail() { if (!isEmpty()) { int el = tail.info; if (head == tail) // if only one node on the list; head = tail = null; else { // if more than one node in the list; tail = tail.prev; tail.next = null; } return el; } else return 0; }
8
search public int find(int el) { IntDLLNode tmp; for (tmp = head; tmp != null && tmp.info != el; tmp = tmp.next); if (tmp == null) return 0; else return tmp.info; }
9
Other methods public boolean isEmpty() { return head == null; } public void setToNull() { head = tail = null; } public int firstEl() { if (!isEmpty()) return head.info; else return 0; }
10
Circular double linked list class CircularDLList{ CircularDLList(){ } boolean isEmpty(){ return head==null;} private IntDLLNode head=null;
11
void addToHead(int el){ if (isEmpty()){ head=new IntDLLNode(el); head.next=head.prev=head;} else { head=new IntDLLNode(el,head,head.prev); head.next.prev=head.prev.next=head;} } void addToTail(int el){ addToHead(el); head=head.next; }
12
int deleteFromHead(){ int el=head.info; if (head.next= =head) //only one node head=null; else { head.prev.next=head.next; head.next.prev=head.prev; head=head.next;} return el; }
13
int deleteFromTail(){ int el=head.prev.info; if (head.next= =head) //only one node head=null; else { head.prev.prev.next=head; head.prev=head.prev.prev; }
14
void deleteNode(int el) { if (!isempty()) if (el= =head.info) deleteFormHead(); else { IntDLLNode tmp; for(tmp=head.next; tmp!=head&&tmp.info!=el; tmp=tmp.next); if (tmp.info= =el){ tmp.next.prev=tmp.prev; tmp.prev.next=tmp.next; }
15
boolean isInList(int el){ if (isEmpty()) return false; else if (head.info= =el) return true; else { IntDLLNode tmp; for(tmp=head.next; tmp!=head && tmp.info!=el; tmp=tmp.next); return tmp.info= =el; }
16
void printAll(){ if (!isEmpty()) { IntDLLNode tmp; System.out.print(head.info+ ); for(tmp=head.next;tmp!=head;tmp=tmp.next) System.out.print(head.info+ ); }
17
Your homework Write a simple editor by means of double linked-list –I I ( row) I 2 ( 2 th row row) –New (clear all the content) –L cat (locate and list the row number) –R cat,dog,a (search all cat and replace it with dog) –R cat,dog (search the current row which contains cat and replace it with dog) –D D (delete current row) D 3 (delete 3 th row) D 3,5 (delete from 3 th to 5 th ) –M M 4 aaaaaaaaaaaaaa (modify row 4th, its new content is aaaaaa ) –S a1.txt (save file) –L a2.txt (load old file) –A pig.txt (load and append into the current content) –Print Print all Print (print the content of the current row)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.