标签:
public class SList { private SListNode head; private int size; /** * SList() constructs an empty list. **/ public SList() { size = 0; head = null; } /** * isEmpty() indicates whether the list is empty. * @return true if the list is empty, false otherwise. **/ public boolean isEmpty() { return size == 0; } /** * length() returns the length of this list. * @return the length of this list. **/ public int length() { return size; } /** * insertFront() inserts item "obj" at the beginning of this list. * @param obj the item to be inserted. **/ public void insertFront(Object obj) { head = new SListNode(obj, head); size++; } /** * insertEnd() inserts item "obj" at the end of this list. * @param obj the item to be inserted. **/ public void insertEnd(Object obj) { if (head == null) { head = new SListNode(obj); } else { SListNode node = head; while (node.next != null) { node = node.next; } node.next = new SListNode(obj); } size++; } /** * nth() returns the item at the specified position. If position < 1 or * position > this.length(), null is returned. Otherwise, the item at * position "position" is returned. The list does not change. * @param position the desired position, from 1 to length(), in the list. * @return the item at the given position in the list. **/ public Object nth(int position) { SListNode currentNode; if ((position < 1) || (head == null)) { return null; } else { currentNode = head; while (position > 1) { currentNode = currentNode.next; if (currentNode == null) { return null; } position--; } return currentNode.item; } }
要求实现如下两个方法:
/** * squish() takes this list and, wherever two or more consecutive items are * equals(), it removes duplicate nodes so that only one consecutive copy * remains. Hence, no two consecutive items in this list are equals() upon * completion of the procedure. * * After squish() executes, the list may well be shorter than when squish() * began. No extra items are added to make up for those removed. * * For example, if the input list is [ 0 0 0 0 1 1 0 0 0 3 3 3 1 1 0 ], the * output list is [ 0 1 0 3 1 0 ]. * * IMPORTANT: Be sure you use the equals() method, and not the "==" * operator, to compare items. **/ public void squish() { // Fill in your solution here. (Ours is eleven lines long.) } /** * twin() takes this list and doubles its length by replacing each node * with two consecutive nodes referencing the same item. * * For example, if the input list is [ 3 7 4 2 2 ], the * output list is [ 3 3 7 7 4 4 2 2 2 2 ]. * * IMPORTANT: Do not try to make new copies of the items themselves. * Make new SListNodes, but just copy the references to the items. **/ public void twin() { // Fill in your solution here. (Ours is seven lines long.) }
自己答案
public void squish() { if (size != 0 && size != 1) { SList newlist = new SList(); newlist.insertEnd(head.item); Object temp = head.item; SListNode cur = head.next; for (int i=2; i<=size; i++){ if (!(cur.item.equals(temp))){ temp = cur.item; newlist.insertEnd(temp); } cur = cur.next; } head = newlist.head; size = newlist.size; } } public void twin() { SList newlist = new SList(); SListNode curnode = head; int i = 1; while(i<=size){ newlist.insertEnd(curnode.item); newlist.insertEnd(curnode.item); curnode = curnode.next; i++; } head = newlist.head; size = newlist.size; }
参考答案
public void squish() { SListNode curr = head; while (curr != null) { while (curr.next != null && curr.item.equals(curr.next.item)) { curr.next = curr.next.next; } curr = curr.next; } } public void twin() { SListNode curr = head; while (curr != null) { curr.next = new SListNode(curr.item, curr.next); curr = curr.next.next; } }
注意理解参考答案, 参考答案对List的理解更彻底.
2015-07-23
标签:
原文地址:http://www.cnblogs.com/whuyt/p/4670129.html