(8)链表 《java数据结构与算法》一书第五章读书笔记。
来源:互联网 发布:琴行软件 编辑:程序博客网 时间:2024/06/04 18:52
--------------------------------------------------------------------
链表
-----------------------------
在数组作为数据存储结构存在一定的缺陷。数组分为有序和无序两种。
无序数组中搜索是低效的。
有序数组中插入效率也是很低的。
不论在哪种数组中删除的效率都是很低的。且建完数组后其大小是不可改变的。
在数组中每一项占用一个特定的位置,这个位置可以用一个下标直接访问,像一排房子可以凭门牌号
直接找到其中特定的一间。
在链表中寻找一个特定元素的唯一方法就是沿着这个元素的链一直向下寻找。不能直接访问到数据项,
但是使用数据项之间的关系可以来定位它。
链节点(【date + next】)
在链表中,每个数据项都被包含在“链节点”中。一个链节点是某个类的对象,这个类可以叫做Link。
每个Link对象中 都包含一个对下一个链节点引用的字段(Next)
class Link{
public data iData;
public double ddate;
public Link next; //自引用
}
或者
class Link{
public OtherObj obj;
public Link next; //自引用
}
-----------------------------
单链表 双端链表 有序链表 双向链表 有迭代器的链表
-----------------------------
**单链表
插入、查找、删除 指定节点
-----------------------------
**双端链表《不同于 双向链表》
双端链表与传统的链表非常相似,但是他有一个新增的特性:即对最后一个链节点的引用
就像对第一个链节点的引用一样。第一个节点 有first和last两个引用。
-----------------------------
链表的效率:
在表头插入删除速度很快,仅需改变一两个的引用值所以花费O(1)时间
平均起来 查找 删除和在指定链节点后面插入 都需要搜索 需要O(N)此比较。
但是链表 不需要移动任何的元素。与数组相比增加效率是很明闲的。
另外的有事 就是 链表多少就可以用多少内存。
数组一开始创建的时候就固定了。
-----------------------------
ADT
基于链表实现的栈
基于链表实现的双端队列
-----------------------------
有序链表
在链表中还没要求数据有序存储,然而对于 某些应用来说,在链表中保持数据有序是有用的。
具有这个特性:在链表中保持数据是有序的。 就叫有序链表。数据按照关键字值有序排列。
有序链表 优于有序数组的地方是 插入的速度。(因为元素不需要移动)另外可有扩展到全部
有效的内存。而数组只能局限于固定大小。
有序链表效率
插入和删除需要O(N)此比较,O(1)时间删除。
表插入排序:
有序链表可用于一种高效的排序机制。假设一个无序数组。从这个数组中取出数据
插入到有序链表中,他们自动按照顺序排序。把它们从链表中删除重新放回数组,
此时数组就排好序了。这种排序比 插入排序 效率更高些。这种方式进行的复制次数少些。
-----------------------------
双向链表
基于双向链表的双端队列
-----------------------------
迭代器
迭代器类包含对数据结构中数据项的引用,并用来遍历这些数据结构的对象。
--------------------------------------------------------------------
########################################################
链表
-----------------------------
在数组作为数据存储结构存在一定的缺陷。数组分为有序和无序两种。
无序数组中搜索是低效的。
有序数组中插入效率也是很低的。
不论在哪种数组中删除的效率都是很低的。且建完数组后其大小是不可改变的。
在数组中每一项占用一个特定的位置,这个位置可以用一个下标直接访问,像一排房子可以凭门牌号
直接找到其中特定的一间。
在链表中寻找一个特定元素的唯一方法就是沿着这个元素的链一直向下寻找。不能直接访问到数据项,
但是使用数据项之间的关系可以来定位它。
链节点(【date + next】)
在链表中,每个数据项都被包含在“链节点”中。一个链节点是某个类的对象,这个类可以叫做Link。
每个Link对象中 都包含一个对下一个链节点引用的字段(Next)
class Link{
public data iData;
public double ddate;
public Link next; //自引用
}
或者
class Link{
public OtherObj obj;
public Link next; //自引用
}
-----------------------------
单链表 双端链表 有序链表 双向链表 有迭代器的链表
-----------------------------
**单链表
插入、查找、删除 指定节点
-----------------------------
**双端链表《不同于 双向链表》
双端链表与传统的链表非常相似,但是他有一个新增的特性:即对最后一个链节点的引用
就像对第一个链节点的引用一样。第一个节点 有first和last两个引用。
-----------------------------
链表的效率:
在表头插入删除速度很快,仅需改变一两个的引用值所以花费O(1)时间
平均起来 查找 删除和在指定链节点后面插入 都需要搜索 需要O(N)此比较。
但是链表 不需要移动任何的元素。与数组相比增加效率是很明闲的。
另外的有事 就是 链表多少就可以用多少内存。
数组一开始创建的时候就固定了。
-----------------------------
ADT
基于链表实现的栈
基于链表实现的双端队列
-----------------------------
有序链表
在链表中还没要求数据有序存储,然而对于 某些应用来说,在链表中保持数据有序是有用的。
具有这个特性:在链表中保持数据是有序的。 就叫有序链表。数据按照关键字值有序排列。
有序链表 优于有序数组的地方是 插入的速度。(因为元素不需要移动)另外可有扩展到全部
有效的内存。而数组只能局限于固定大小。
有序链表效率
插入和删除需要O(N)此比较,O(1)时间删除。
表插入排序:
有序链表可用于一种高效的排序机制。假设一个无序数组。从这个数组中取出数据
插入到有序链表中,他们自动按照顺序排序。把它们从链表中删除重新放回数组,
此时数组就排好序了。这种排序比 插入排序 效率更高些。这种方式进行的复制次数少些。
-----------------------------
双向链表
基于双向链表的双端队列
-----------------------------
迭代器
迭代器类包含对数据结构中数据项的引用,并用来遍历这些数据结构的对象。
--------------------------------------------------------------------
《1》linkList单链表代码// linkList.java// demonstrates linked list// to run this program: C>java LinkListApp////////////////////////////////////////////////////////////////class Link { public int iData; // data item public double dData; // data item public Link next; // next link in list// ------------------------------------------------------------- public Link(int id, double dd) // constructor { iData = id; // initialize data dData = dd; // ('next' is automatically } // set to null)// ------------------------------------------------------------- public void displayLink() // display ourself { System.out.print("{" + iData + ", " + dData + "} "); } } // end class Link////////////////////////////////////////////////////////////////class LinkList { private Link first; // ref to first link on list //总是指向链表的 第一个节点对象 //插入新节点时,first也改变了。// ------------------------------------------------------------- public LinkList() // constructor { first = null; // no links on list yet }// ------------------------------------------------------------- public boolean isEmpty() // true if list is empty { return (first==null); }// ------------------------------------------------------------- // insert at start of list public void insertFirst(int id, double dd) { // make new link Link newLink = new Link(id, dd); newLink.next = first; // newLink --> old first first = newLink; // first --> newLink }// ------------------------------------------------------------- public Link deleteFirst() // delete first item { // (assumes list not empty) Link temp = first; // save reference to link first = first.next; // delete it: first-->old next return temp; // return deleted link }// ------------------------------------------------------------- public void displayList() { System.out.print("List (first-->last): "); Link current = first; // start at beginning of list while(current != null) // until end of list, { current.displayLink(); // print data current = current.next; // move to next link } System.out.println(""); }// ------------------------------------------------------------- } // end class LinkList////////////////////////////////////////////////////////////////class LinkListApp { public static void main(String[] args) { LinkList theList = new LinkList(); // make new list theList.insertFirst(22, 2.99); // insert four items theList.insertFirst(44, 4.99); theList.insertFirst(66, 6.99); theList.insertFirst(88, 8.99); theList.displayList(); // display list while( !theList.isEmpty() ) // until it's empty, { Link aLink = theList.deleteFirst(); // delete link System.out.print("Deleted "); // display it aLink.displayLink(); System.out.println(""); } theList.displayList(); // display list } // end main() } // end class LinkListApp////////////////////////////////////////////////////////////////###################################################################《2》单链表 插入、查找、删除 指定节点// linkList2.java// demonstrates linked list// to run this program: C>java LinkList2App////////////////////////////////////////////////////////////////class Link { public int iData; // data item (key) public double dData; // data item public Link next; // next link in list// ------------------------------------------------------------- public Link(int id, double dd) // constructor { iData = id; dData = dd; }// ------------------------------------------------------------- public void displayLink() // display ourself { System.out.print("{" + iData + ", " + dData + "} "); } } // end class Link////////////////////////////////////////////////////////////////class LinkList { private Link first; // ref to first link on list// ------------------------------------------------------------- public LinkList() // constructor { first = null; // no links on list yet }// ------------------------------------------------------------- public void insertFirst(int id, double dd) { // make new link Link newLink = new Link(id, dd); newLink.next = first; // it points to old first link first = newLink; // now first points to this }// ------------------------------------------------------------- public Link find(int key) // find link with given key { // (assumes non-empty list) Link current = first; // start at 'first' while(current.iData != key) // while no match, { if(current.next == null) // if end of list, return null; // didn't find it else // not end of list, current = current.next; // go to next link } return current; // found it }// ------------------------------------------------------------- public Link delete(int key) // delete link with given key { // (assumes non-empty list) Link current = first; // search for link Link previous = first; while(current.iData != key) { if(current.next == null) return null; // didn't find it else { previous = current; // go to next link current = current.next; } } // found it if(current == first) // if first link, first = first.next; // change first else // otherwise, previous.next = current.next; // bypass it return current; }// ------------------------------------------------------------- public void displayList() // display the list { System.out.print("List (first-->last): "); Link current = first; // start at beginning of list while(current != null) // until end of list, { current.displayLink(); // print data current = current.next; // move to next link } System.out.println(""); }// ------------------------------------------------------------- } // end class LinkList////////////////////////////////////////////////////////////////class LinkList2App { public static void main(String[] args) { LinkList theList = new LinkList(); // make list theList.insertFirst(22, 2.99); // insert 4 items theList.insertFirst(44, 4.99); theList.insertFirst(66, 6.99); theList.insertFirst(88, 8.99); theList.displayList(); // display list Link f = theList.find(44); // find item if( f != null) System.out.println("Found link with key " + f.iData); else System.out.println("Can't find link"); Link d = theList.delete(66); // delete item if( d != null ) System.out.println("Deleted link with key " + d.iData); else System.out.println("Can't delete link"); theList.displayList(); // display list } // end main() } // end class LinkList2App////////////////////////////////////////////////////////////////#############################################################《3》 双端队列// firstLastList.java// demonstrates list with first and last references// to run this program: C>java FirstLastApp////////////////////////////////////////////////////////////////class Link { public long dData; // data item public Link next; // next link in list// ------------------------------------------------------------- public Link(long d) // constructor { dData = d; }// ------------------------------------------------------------- public void displayLink() // display this link { System.out.print(dData + " "); }// ------------------------------------------------------------- } // end class Link////////////////////////////////////////////////////////////////class FirstLastList { private Link first; // ref to first link private Link last; // ref to last link// ------------------------------------------------------------- public FirstLastList() // constructor { first = null; // no links on list yet last = null; }// ------------------------------------------------------------- public boolean isEmpty() // true if no links { return first==null; }// ------------------------------------------------------------- public void insertFirst(long dd) // insert at front of list { Link newLink = new Link(dd); // make new link if( isEmpty() ) // if empty list, last = newLink; // newLink <-- last newLink.next = first; // newLink --> old first first = newLink; // first --> newLink }// ------------------------------------------------------------- public void insertLast(long dd) // insert at end of list { Link newLink = new Link(dd); // make new link if( isEmpty() ) // if empty list, first = newLink; // first --> newLink else last.next = newLink; // old last --> newLink last = newLink; // newLink <-- last }// ------------------------------------------------------------- public long deleteFirst() // delete first link { // (assumes non-empty list) long temp = first.dData; if(first.next == null) // if only one item last = null; // null <-- last first = first.next; // first --> old next return temp; }// ------------------------------------------------------------- public void displayList() { System.out.print("List (first-->last): "); Link current = first; // start at beginning while(current != null) // until end of list, { current.displayLink(); // print data current = current.next; // move to next link } System.out.println(""); }// ------------------------------------------------------------- } // end class FirstLastList////////////////////////////////////////////////////////////////class FirstLastApp { public static void main(String[] args) { // make a new list FirstLastList theList = new FirstLastList(); theList.insertFirst(22); // insert at front theList.insertFirst(44); theList.insertFirst(66); theList.insertLast(11); // insert at rear theList.insertLast(33); theList.insertLast(55); theList.displayList(); // display the list theList.deleteFirst(); // delete first two items theList.deleteFirst(); theList.displayList(); // display again } // end main() } // end class FirstLastApp////////////////////////////////////////////////////////////////#########################################################################《4》基于链表实现的栈// linkStack.java// demonstrates a stack implemented as a list// to run this program: C>java LinkStackApp////////////////////////////////////////////////////////////////class Link { public long dData; // data item public Link next; // next link in list// ------------------------------------------------------------- public Link(long dd) // constructor { dData = dd; }// ------------------------------------------------------------- public void displayLink() // display ourself { System.out.print(dData + " "); } } // end class Link////////////////////////////////////////////////////////////////class LinkList { private Link first; // ref to first item on list// ------------------------------------------------------------- public LinkList() // constructor { first = null; } // no items on list yet// ------------------------------------------------------------- public boolean isEmpty() // true if list is empty { return (first==null); }// ------------------------------------------------------------- public void insertFirst(long dd) // insert at start of list { // make new link Link newLink = new Link(dd); newLink.next = first; // newLink --> old first first = newLink; // first --> newLink }// ------------------------------------------------------------- public long deleteFirst() // delete first item { // (assumes list not empty) Link temp = first; // save reference to link first = first.next; // delete it: first-->old next return temp.dData; // return deleted link }// ------------------------------------------------------------- public void displayList() { Link current = first; // start at beginning of list while(current != null) // until end of list, { current.displayLink(); // print data current = current.next; // move to next link } System.out.println(""); }// ------------------------------------------------------------- } // end class LinkList////////////////////////////////////////////////////////////////class LinkStack { private LinkList theList;//-------------------------------------------------------------- public LinkStack() // constructor { theList = new LinkList(); }//-------------------------------------------------------------- public void push(long j) // put item on top of stack { theList.insertFirst(j); }//-------------------------------------------------------------- public long pop() // take item from top of stack { return theList.deleteFirst(); }//-------------------------------------------------------------- public boolean isEmpty() // true if stack is empty { return ( theList.isEmpty() ); }//-------------------------------------------------------------- public void displayStack() { System.out.print("Stack (top-->bottom): "); theList.displayList(); }//-------------------------------------------------------------- } // end class LinkStack////////////////////////////////////////////////////////////////class LinkStackApp { public static void main(String[] args) { LinkStack theStack = new LinkStack(); // make stack theStack.push(20); // push items theStack.push(40); theStack.displayStack(); // display stack theStack.push(60); // push items theStack.push(80); theStack.displayStack(); // display stack theStack.pop(); // pop items theStack.pop(); theStack.displayStack(); // display stack } // end main() } // end class LinkStackApp////////////////////////////////////////////////////////////////################################################################################《5》基于链表实现的队列// linkQueue.java// demonstrates queue implemented as double-ended list// to run this program: C>java LinkQueueApp////////////////////////////////////////////////////////////////class Link { public long dData; // data item public Link next; // next link in list// ------------------------------------------------------------- public Link(long d) // constructor { dData = d; }// ------------------------------------------------------------- public void displayLink() // display this link { System.out.print(dData + " "); }// ------------------------------------------------------------- } // end class Link////////////////////////////////////////////////////////////////class FirstLastList { private Link first; // ref to first item private Link last; // ref to last item// ------------------------------------------------------------- public FirstLastList() // constructor { first = null; // no items on list yet last = null; }// ------------------------------------------------------------- public boolean isEmpty() // true if no links { return first==null; }// ------------------------------------------------------------- public void insertLast(long dd) // insert at end of list { Link newLink = new Link(dd); // make new link if( isEmpty() ) // if empty list, first = newLink; // first --> newLink else last.next = newLink; // old last --> newLink last = newLink; // newLink <-- last }// ------------------------------------------------------------- public long deleteFirst() // delete first link { // (assumes non-empty list) long temp = first.dData; if(first.next == null) // if only one item last = null; // null <-- last first = first.next; // first --> old next return temp; }// ------------------------------------------------------------- public void displayList() { Link current = first; // start at beginning while(current != null) // until end of list, { current.displayLink(); // print data current = current.next; // move to next link } System.out.println(""); }// ------------------------------------------------------------- } // end class FirstLastList////////////////////////////////////////////////////////////////class LinkQueue { private FirstLastList theList;//-------------------------------------------------------------- public LinkQueue() // constructor { theList = new FirstLastList(); } // make a 2-ended list//-------------------------------------------------------------- public boolean isEmpty() // true if queue is empty { return theList.isEmpty(); }//-------------------------------------------------------------- public void insert(long j) // insert, rear of queue { theList.insertLast(j); }//-------------------------------------------------------------- public long remove() // remove, front of queue { return theList.deleteFirst(); }//-------------------------------------------------------------- public void displayQueue() { System.out.print("Queue (front-->rear): "); theList.displayList(); }//-------------------------------------------------------------- } // end class LinkQueue////////////////////////////////////////////////////////////////class LinkQueueApp { public static void main(String[] args) { LinkQueue theQueue = new LinkQueue(); theQueue.insert(20); // insert items theQueue.insert(40); theQueue.displayQueue(); // display queue theQueue.insert(60); // insert items theQueue.insert(80); theQueue.displayQueue(); // display queue theQueue.remove(); // remove items theQueue.remove(); theQueue.displayQueue(); // display queue } // end main() } // end class LinkQueueApp////////////////////////////////////////////////////////////////################################################################################《6》有序队列// sortedList.java// demonstrates sorted list// to run this program: C>java SortedListApp////////////////////////////////////////////////////////////////class Link { public long dData; // data item public Link next; // next link in list// ------------------------------------------------------------- public Link(long dd) // constructor { dData = dd; }// ------------------------------------------------------------- public void displayLink() // display this link { System.out.print(dData + " "); } } // end class Link////////////////////////////////////////////////////////////////class SortedList { private Link first; // ref to first item// ------------------------------------------------------------- public SortedList() // constructor { first = null; }// ------------------------------------------------------------- public boolean isEmpty() // true if no links { return (first==null); }// ------------------------------------------------------------- public void insert(long key) // insert, in order { Link newLink = new Link(key); // make new link Link previous = null; // start at first 重要!!!!! Link current = first; // 重要!!!!!! // until end of list, while(current != null && key > current.dData) { // or key > current, previous = current; current = current.next; // go to next item } if(previous==null) // at beginning of list first = newLink; // first --> newLink else // not at beginning previous.next = newLink; // old prev --> newLink newLink.next = current; // newLink --> old currnt } // end insert()// ------------------------------------------------------------- public Link remove() // return & delete first link { // (assumes non-empty list) Link temp = first; // save first first = first.next; // delete first return temp; // return value }// ------------------------------------------------------------- public void displayList() { System.out.print("List (first-->last): "); Link current = first; // start at beginning of list while(current != null) // until end of list, { current.displayLink(); // print data current = current.next; // move to next link } System.out.println(""); } } // end class SortedList////////////////////////////////////////////////////////////////class SortedListApp { public static void main(String[] args) { // create new list SortedList theSortedList = new SortedList(); theSortedList.insert(20); // insert 2 items theSortedList.insert(40); theSortedList.displayList(); // display list theSortedList.insert(10); // insert 3 more items theSortedList.insert(30); theSortedList.insert(50); theSortedList.displayList(); // display list theSortedList.remove(); // remove an item theSortedList.displayList(); // display list } // end main() } // end class SortedListApp////////////////////////////////////////////////////////////////########################################################《7》表插入排序// listInsertionSort.java// demonstrates sorted list used for sorting// to run this program: C>java ListInsertionSortApp////////////////////////////////////////////////////////////////class Link { public long dData; // data item public Link next; // next link in list// ------------------------------------------------------------- public Link(long dd) // constructor { dData = dd; }// ------------------------------------------------------------- } // end class Link////////////////////////////////////////////////////////////////class SortedList { private Link first; // ref to first item on list// ------------------------------------------------------------- public SortedList() // constructor (no args) { first = null; } // initialize list// ------------------------------------------------------------- public SortedList(Link[] linkArr) // constructor (array { // as argument) first = null; // initialize list for(int j=0; j<linkArr.length; j++) // copy array insert( linkArr[j] ); // to list }// ------------------------------------------------------------- public void insert(Link k) // insert (in order) { Link previous = null; // start at first Link current = first; // until end of list, while(current != null && k.dData > current.dData) { // or key > current, previous = current; current = current.next; // go to next item } if(previous==null) // at beginning of list first = k; // first --> k else // not at beginning previous.next = k; // old prev --> k k.next = current; // k --> old currnt } // end insert()// ------------------------------------------------------------- public Link remove() // return & delete first link { // (assumes non-empty list) Link temp = first; // save first first = first.next; // delete first return temp; // return value }// ------------------------------------------------------------- } // end class SortedList////////////////////////////////////////////////////////////////class ListInsertionSortApp { public static void main(String[] args) { int size = 10; // create array of links Link[] linkArray = new Link[size]; for(int j=0; j<size; j++) // fill array with links { // random number int n = (int)(java.lang.Math.random()*99); Link newLink = new Link(n); // make link linkArray[j] = newLink; // put in array } // display array contents System.out.print("Unsorted array: "); for(int j=0; j<size; j++) System.out.print( linkArray[j].dData + " " ); System.out.println(""); // create new list SortedList theSortedList = new SortedList(linkArray); for(int j=0; j<size; j++) // links from list to array linkArray[j] = theSortedList.remove(); // display array contents System.out.print("Sorted Array: "); for(int j=0; j<size; j++) System.out.print(linkArray[j].dData + " "); System.out.println(""); } // end main() } // end class ListInsertionSortApp////////////////////////////////////////////////////////////////########################################################《8》双向链表// doublyLinked.java// demonstrates doubly-linked list// to run this program: C>java DoublyLinkedApp////////////////////////////////////////////////////////////////class Link { public long dData; // data item public Link next; // next link in list public Link previous; // previous link in list// ------------------------------------------------------------- public Link(long d) // constructor { dData = d; }// ------------------------------------------------------------- public void displayLink() // display this link { System.out.print(dData + " "); }// ------------------------------------------------------------- } // end class Link////////////////////////////////////////////////////////////////class DoublyLinkedList { private Link first; // ref to first item private Link last; // ref to last item// ------------------------------------------------------------- public DoublyLinkedList() // constructor { first = null; // no items on list yet last = null; }// ------------------------------------------------------------- public boolean isEmpty() // true if no links { return first==null; }// ------------------------------------------------------------- public void insertFirst(long dd) // insert at front of list { Link newLink = new Link(dd); // make new link if( isEmpty() ) // if empty list, last = newLink; // newLink <-- last else first.previous = newLink; // newLink <-- old first newLink.next = first; // newLink --> old first first = newLink; // first --> newLink }// ------------------------------------------------------------- public void insertLast(long dd) // insert at end of list { Link newLink = new Link(dd); // make new link if( isEmpty() ) // if empty list, first = newLink; // first --> newLink else { last.next = newLink; // old last --> newLink newLink.previous = last; // old last <-- newLink } last = newLink; // newLink <-- last }// ------------------------------------------------------------- public Link deleteFirst() // delete first link { // (assumes non-empty list) Link temp = first; if(first.next == null) // if only one item last = null; // null <-- last else first.next.previous = null; // null <-- old next first = first.next; // first --> old next return temp; }// ------------------------------------------------------------- public Link deleteLast() // delete last link { // (assumes non-empty list) Link temp = last; if(first.next == null) // if only one item first = null; // first --> null else last.previous.next = null; // old previous --> null last = last.previous; // old previous <-- last return temp; }// ------------------------------------------------------------- // insert dd just after key public boolean insertAfter(long key, long dd) { // (assumes non-empty list) Link current = first; // start at beginning while(current.dData != key) // until match is found, { current = current.next; // move to next link if(current == null) return false; // didn't find it } Link newLink = new Link(dd); // make new link if(current==last) // if last link, { newLink.next = null; // newLink --> null last = newLink; // newLink <-- last } else // not last link, { newLink.next = current.next; // newLink --> old next // newLink <-- old next current.next.previous = newLink; } newLink.previous = current; // old current <-- newLink current.next = newLink; // old current --> newLink return true; // found it, did insertion }// ------------------------------------------------------------- public Link deleteKey(long key) // delete item w/ given key { // (assumes non-empty list) Link current = first; // start at beginning while(current.dData != key) // until match is found, { current = current.next; // move to next link if(current == null) return null; // didn't find it } if(current==first) // found it; first item? first = current.next; // first --> old next else // not first // old previous --> old next current.previous.next = current.next; if(current==last) // last item? last = current.previous; // old previous <-- last else // not last // old previous <-- old next current.next.previous = current.previous; return current; // return value }// ------------------------------------------------------------- public void displayForward() { System.out.print("List (first-->last): "); Link current = first; // start at beginning while(current != null) // until end of list, { current.displayLink(); // display data current = current.next; // move to next link } System.out.println(""); }// ------------------------------------------------------------- public void displayBackward() { System.out.print("List (last-->first): "); Link current = last; // start at end while(current != null) // until start of list, { current.displayLink(); // display data current = current.previous; // move to previous link } System.out.println(""); }// ------------------------------------------------------------- } // end class DoublyLinkedList////////////////////////////////////////////////////////////////class DoublyLinkedApp { public static void main(String[] args) { // make a new list DoublyLinkedList theList = new DoublyLinkedList(); theList.insertFirst(22); // insert at front theList.insertFirst(44); theList.insertFirst(66); theList.insertLast(11); // insert at rear theList.insertLast(33); theList.insertLast(55); theList.displayForward(); // display list forward theList.displayBackward(); // display list backward theList.deleteFirst(); // delete first item theList.deleteLast(); // delete last item theList.deleteKey(11); // delete item with key 11 theList.displayForward(); // display list forward theList.insertAfter(22, 77); // insert 77 after 22 theList.insertAfter(33, 88); // insert 88 after 33 theList.displayForward(); // display list forward } // end main() } // end class DoublyLinkedApp////////////////////////////////////////////////////////////////########################################################《9》迭代器// interIterator.java// demonstrates iterators on a linked listListIterator// to run this program: C>java InterIterAppimport java.io.*; // for I/O////////////////////////////////////////////////////////////////class Link { public long dData; // data item public Link next; // next link in list// ------------------------------------------------------------- public Link(long dd) // constructor { dData = dd; }// ------------------------------------------------------------- public void displayLink() // display ourself { System.out.print(dData + " "); } } // end class Link////////////////////////////////////////////////////////////////class LinkList { private Link first; // ref to first item on list// ------------------------------------------------------------- public LinkList() // constructor { first = null; } // no items on list yet// ------------------------------------------------------------- public Link getFirst() // get value of first { return first; }// ------------------------------------------------------------- public void setFirst(Link f) // set first to new link { first = f; }// ------------------------------------------------------------- public boolean isEmpty() // true if list is empty { return first==null; }// ------------------------------------------------------------- public ListIterator getIterator() // return iterator { return new ListIterator(this); // initialized with } // this list// ------------------------------------------------------------- public void displayList() { Link current = first; // start at beginning of list while(current != null) // until end of list, { current.displayLink(); // print data current = current.next; // move to next link } System.out.println(""); }// ------------------------------------------------------------- } // end class LinkList////////////////////////////////////////////////////////////////class ListIterator { private Link current; // current link private Link previous; // previous link private LinkList ourList; // our linked list//-------------------------------------------------------------- public ListIterator(LinkList list) // constructor { ourList = list; reset(); }//-------------------------------------------------------------- public void reset() // start at 'first' { current = ourList.getFirst(); previous = null; }//-------------------------------------------------------------- public boolean atEnd() // true if last link { return (current.next==null); }//-------------------------------------------------------------- public void nextLink() // go to next link { previous = current; current = current.next; }//-------------------------------------------------------------- public Link getCurrent() // get current link { return current; }//-------------------------------------------------------------- public void insertAfter(long dd) // insert after { // current link Link newLink = new Link(dd); if( ourList.isEmpty() ) // empty list { ourList.setFirst(newLink); current = newLink; } else // not empty { newLink.next = current.next; current.next = newLink; nextLink(); // point to new link } }//-------------------------------------------------------------- public void insertBefore(long dd) // insert before { // current link Link newLink = new Link(dd); if(previous == null) // beginning of list { // (or empty list) newLink.next = ourList.getFirst(); ourList.setFirst(newLink); reset(); } else // not beginning { newLink.next = previous.next; previous.next = newLink; current = newLink; } }//-------------------------------------------------------------- public long deleteCurrent() // delete item at current { long value = current.dData; if(previous == null) // beginning of list { ourList.setFirst(current.next); reset(); } else // not beginning { previous.next = current.next; if( atEnd() ) reset(); else current = current.next; } return value; }//-------------------------------------------------------------- } // end class ListIterator////////////////////////////////////////////////////////////////class InterIterApp { public static void main(String[] args) throws IOException { LinkList theList = new LinkList(); // new list ListIterator iter1 = theList.getIterator(); // new iter long value; iter1.insertAfter(20); // insert items iter1.insertAfter(40); iter1.insertAfter(80); iter1.insertBefore(60); while(true) { System.out.print("Enter first letter of show, reset, "); System.out.print("next, get, before, after, delete: "); System.out.flush(); int choice = getChar(); // get user's option switch(choice) { case 's': // show list if( !theList.isEmpty() ) theList.displayList(); else System.out.println("List is empty"); break; case 'r': // reset (to first) iter1.reset(); break; case 'n': // advance to next item if( !theList.isEmpty() && !iter1.atEnd() ) iter1.nextLink(); else System.out.println("Can't go to next link"); break; case 'g': // get current item if( !theList.isEmpty() ) { value = iter1.getCurrent().dData; System.out.println("Returned " + value); } else System.out.println("List is empty"); break; case 'b': // insert before current System.out.print("Enter value to insert: "); System.out.flush(); value = getInt(); iter1.insertBefore(value); break; case 'a': // insert after current System.out.print("Enter value to insert: "); System.out.flush(); value = getInt(); iter1.insertAfter(value); break; case 'd': // delete current item if( !theList.isEmpty() ) { value = iter1.deleteCurrent(); System.out.println("Deleted " + value); } else System.out.println("Can't delete"); break; default: System.out.println("Invalid entry"); } // end switch } // end while } // end main()//-------------------------------------------------------------- public static String getString() throws IOException { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String s = br.readLine(); return s; }//------------------------------------------------------------- public static char getChar() throws IOException { String s = getString(); return s.charAt(0); }//------------------------------------------------------------- public static int getInt() throws IOException { String s = getString(); return Integer.parseInt(s); }//------------------------------------------------------------- } // end class InterIterApp////////////////////////////////////////////////////////////////
########################################################
0 0
- (8)链表 《java数据结构与算法》一书第五章读书笔记。
- (6)排序算法--- 冒泡 、选择、插入 《java数据结构与算法》一书第三章读书笔记。
- (7)栈 队列 优先级队列 《java数据结构与算法》一书第四章读书笔记。
- (9)递归 《java数据结构与算法》一书第六章读书笔记
- 一、数据结构 读书笔记(一)基本概念与算法
- 【排序算法】《Java数据结构与算法》读书笔记
- Python宝典第五章:数据结构与算法
- 数据结构与算法-- 第五章 树
- 数据结构与算法分析第二章读书笔记
- 数据结构与算法 第五课
- 算法导论读书笔记 第五章
- Java版数据结构与算法(一)
- <一>java数据结构与算法 数组
- java数据结构与算法(一)
- Java数据结构与算法《一》数组
- 大话数据结构 第五章 串 (读书笔记)
- 数据结构与算法分析课后习题第五章
- 算法与数据结构第五章树上机练习
- ndk知识
- libcurl的API详解
- 如何让Android下的hint多行时EditText焦点光标默认在第一行
- View对Touch事件的处理
- IOS调用系统联系人界面获取联系人信息
- (8)链表 《java数据结构与算法》一书第五章读书笔记。
- MvcPager分页控件的使用2
- hduoj3783(ZOJ)
- 解决onload函数的浏览器兼容性问题
- WPF Binding相关的一些常见方式总结(三)
- Redis集群方案,Codis安装测试
- (十二)ng-repeat指令中使用track by子语句解决重复数据遍历的错误
- 抓取csdn上的各类别的文章 (制作csdn app 二)
- RecyclerView中实现addHeaderView,addFooterView功能