<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://www.lensovet.net/~sysadmin/w/index.php?action=history&amp;feed=atom&amp;title=Computer_Science%2F61b%2FHomework%2Fhw3%2FSList.java</id>
	<title>Computer Science/61b/Homework/hw3/SList.java - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://www.lensovet.net/~sysadmin/w/index.php?action=history&amp;feed=atom&amp;title=Computer_Science%2F61b%2FHomework%2Fhw3%2FSList.java"/>
	<link rel="alternate" type="text/html" href="http://www.lensovet.net/~sysadmin/w/index.php?title=Computer_Science/61b/Homework/hw3/SList.java&amp;action=history"/>
	<updated>2026-05-03T06:03:59Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.31.16</generator>
	<entry>
		<id>http://www.lensovet.net/~sysadmin/w/index.php?title=Computer_Science/61b/Homework/hw3/SList.java&amp;diff=24307&amp;oldid=prev</id>
		<title>Lensovet: Lensovet moved page CS/61b/Homework/hw3/SList.java to Computer Science/61b/Homework/hw3/SList.java</title>
		<link rel="alternate" type="text/html" href="http://www.lensovet.net/~sysadmin/w/index.php?title=Computer_Science/61b/Homework/hw3/SList.java&amp;diff=24307&amp;oldid=prev"/>
		<updated>2023-02-20T03:51:34Z</updated>

		<summary type="html">&lt;p&gt;Lensovet moved page &lt;a href=&quot;/~sysadmin/w/CS/61b/Homework/hw3/SList.java&quot; class=&quot;mw-redirect&quot; title=&quot;CS/61b/Homework/hw3/SList.java&quot;&gt;CS/61b/Homework/hw3/SList.java&lt;/a&gt; to &lt;a href=&quot;/~sysadmin/w/Computer_Science/61b/Homework/hw3/SList.java&quot; title=&quot;Computer Science/61b/Homework/hw3/SList.java&quot;&gt;Computer Science/61b/Homework/hw3/SList.java&lt;/a&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;Revision as of 03:51, 20 February 2023&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-notice&quot; lang=&quot;en&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(No difference)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Lensovet</name></author>
		
	</entry>
	<entry>
		<id>http://www.lensovet.net/~sysadmin/w/index.php?title=Computer_Science/61b/Homework/hw3/SList.java&amp;diff=3995&amp;oldid=prev</id>
		<title>Lensovet: moved CS 61b/Homework/hw3/SList.java to CS/61b/Homework/hw3/SList.java:&amp;#32;fix cs 61b hierarchy</title>
		<link rel="alternate" type="text/html" href="http://www.lensovet.net/~sysadmin/w/index.php?title=Computer_Science/61b/Homework/hw3/SList.java&amp;diff=3995&amp;oldid=prev"/>
		<updated>2010-11-14T06:00:19Z</updated>

		<summary type="html">&lt;p&gt;moved &lt;a href=&quot;/~sysadmin/w/CS_61b/Homework/hw3/SList.java&quot; class=&quot;mw-redirect&quot; title=&quot;CS 61b/Homework/hw3/SList.java&quot;&gt;CS 61b/Homework/hw3/SList.java&lt;/a&gt; to &lt;a href=&quot;/~sysadmin/w/CS/61b/Homework/hw3/SList.java&quot; class=&quot;mw-redirect&quot; title=&quot;CS/61b/Homework/hw3/SList.java&quot;&gt;CS/61b/Homework/hw3/SList.java&lt;/a&gt;: fix cs 61b hierarchy&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;Revision as of 06:00, 14 November 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-notice&quot; lang=&quot;en&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(No difference)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Lensovet</name></author>
		
	</entry>
	<entry>
		<id>http://www.lensovet.net/~sysadmin/w/index.php?title=Computer_Science/61b/Homework/hw3/SList.java&amp;diff=2969&amp;oldid=prev</id>
		<title>Lensovet at 20:02, 22 May 2007</title>
		<link rel="alternate" type="text/html" href="http://www.lensovet.net/~sysadmin/w/index.php?title=Computer_Science/61b/Homework/hw3/SList.java&amp;diff=2969&amp;oldid=prev"/>
		<updated>2007-05-22T20:02:36Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{code}}&lt;br /&gt;
 /* SList.java */&lt;br /&gt;
 &lt;br /&gt;
 /**&lt;br /&gt;
  *  The SList class is a singly-linked implementation of the linked list&lt;br /&gt;
  *  abstraction.  SLists are mutable data structures, which can grow at either&lt;br /&gt;
  *  end.&lt;br /&gt;
  *&lt;br /&gt;
  *  @author Kathy Yelick and Jonathan Shewchuk&lt;br /&gt;
  **/&lt;br /&gt;
 &lt;br /&gt;
 public class SList {&lt;br /&gt;
 &lt;br /&gt;
   private SListNode head;&lt;br /&gt;
   private int size;&lt;br /&gt;
 &lt;br /&gt;
   /**&lt;br /&gt;
    *  SList() constructs an empty list.&lt;br /&gt;
    **/&lt;br /&gt;
 &lt;br /&gt;
   public SList() {&lt;br /&gt;
     size = 0;&lt;br /&gt;
     head = null;&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   /**&lt;br /&gt;
    *  isEmpty() indicates whether the list is empty.&lt;br /&gt;
    *  @return true if the list is empty, false otherwise.&lt;br /&gt;
    **/&lt;br /&gt;
 &lt;br /&gt;
   public boolean isEmpty() {&lt;br /&gt;
     return size == 0;&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   /**&lt;br /&gt;
    *  length() returns the length of this list.&lt;br /&gt;
    *  @return the length of this list.&lt;br /&gt;
    **/&lt;br /&gt;
 &lt;br /&gt;
   public int length() {&lt;br /&gt;
     return size;&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   /**&lt;br /&gt;
    *  insertFront() inserts item &amp;quot;obj&amp;quot; at the beginning of this list.&lt;br /&gt;
    *  @param obj the item to be inserted.&lt;br /&gt;
    **/&lt;br /&gt;
 &lt;br /&gt;
   public void insertFront(Object obj) {&lt;br /&gt;
     head = new SListNode(obj, head);&lt;br /&gt;
     size++;&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   /**&lt;br /&gt;
    *  insertEnd() inserts item &amp;quot;obj&amp;quot; at the end of this list.&lt;br /&gt;
    *  @param obj the item to be inserted.&lt;br /&gt;
    **/&lt;br /&gt;
 &lt;br /&gt;
   public void insertEnd(Object obj) {&lt;br /&gt;
     if (head == null) {&lt;br /&gt;
       head = new SListNode(obj);&lt;br /&gt;
     } else {&lt;br /&gt;
       SListNode node = head;&lt;br /&gt;
       while (node.next != null) {&lt;br /&gt;
         node = node.next;&lt;br /&gt;
       }&lt;br /&gt;
       node.next = new SListNode(obj);&lt;br /&gt;
     }&lt;br /&gt;
     size++;&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   /**&lt;br /&gt;
    *  nth() returns the item at the specified position.  If position &amp;lt; 1 or&lt;br /&gt;
    *  position &amp;gt; this.length(), null is returned.  Otherwise, the item at&lt;br /&gt;
    *  position &amp;quot;position&amp;quot; is returned.  The list does not change.&lt;br /&gt;
    *  @param position the desired position, from 1 to length(), in the list.&lt;br /&gt;
    *  @return the item at the given position in the list.&lt;br /&gt;
    **/&lt;br /&gt;
 &lt;br /&gt;
   public Object nth(int position) {&lt;br /&gt;
     SListNode currentNode;&lt;br /&gt;
 &lt;br /&gt;
     if ((position &amp;lt; 1) || (head == null)) {&lt;br /&gt;
       return null;&lt;br /&gt;
     } else {&lt;br /&gt;
       currentNode = head;&lt;br /&gt;
       while (position &amp;gt; 1) {&lt;br /&gt;
         currentNode = currentNode.next;&lt;br /&gt;
         if (currentNode == null) {&lt;br /&gt;
           return null;&lt;br /&gt;
         }&lt;br /&gt;
         position--;&lt;br /&gt;
       }&lt;br /&gt;
       return currentNode.item;&lt;br /&gt;
     }&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   /**&lt;br /&gt;
    *  squish() takes this list and, wherever two or more consecutive items are&lt;br /&gt;
    *  equal(), it removes duplicate nodes so that only one consecutive copy&lt;br /&gt;
    *  remains.  Hence, no two consecutive items in this list are equal() upon&lt;br /&gt;
    *  completion of the procedure.&lt;br /&gt;
    *&lt;br /&gt;
    *  After squish() executes, the list may well be shorter than when squish()&lt;br /&gt;
    *  began.  No extra items are added to make up for those removed.&lt;br /&gt;
    *&lt;br /&gt;
    *  For example, if the input list is [ 0 0 0 0 1 1 0 0 0 3 3 3 1 1 0 ], the&lt;br /&gt;
    *  output list is [ 0 1 0 3 1 0 ].&lt;br /&gt;
    *&lt;br /&gt;
    *  IMPORTANT:  Be sure you use the equals() method, and not the &amp;quot;==&amp;quot;&lt;br /&gt;
    *  operator, to compare items.&lt;br /&gt;
    **/&lt;br /&gt;
 &lt;br /&gt;
   public void squish() {&lt;br /&gt;
    if (size != 0) {&lt;br /&gt;
 	tool: for (SListNode node=head; node.next != null; node = node.next) {&lt;br /&gt;
 			while (node.next != null &amp;amp;&amp;amp; node.item.equals(node.next.item)) {&lt;br /&gt;
 				if (node.next.next != null) { node.next = node.next.next; }&lt;br /&gt;
 				else { node.next = null; break tool; }&lt;br /&gt;
 		}&lt;br /&gt;
 	} } }&lt;br /&gt;
 &lt;br /&gt;
   /**&lt;br /&gt;
    *  twin() takes this list and doubles its length by replacing each node&lt;br /&gt;
    *  with two consecutive nodes referencing the same item.&lt;br /&gt;
    *&lt;br /&gt;
    *  For example, if the input list is [ 3 7 4 2 2 ], the&lt;br /&gt;
    *  output list is [ 3 3 7 7 4 4 2 2 2 2 ].&lt;br /&gt;
    *&lt;br /&gt;
    *  IMPORTANT:  Do not try to make new copies of the items themselves.&lt;br /&gt;
    *  Just copy the references to the items.&lt;br /&gt;
    **/&lt;br /&gt;
 &lt;br /&gt;
   public void twin() {&lt;br /&gt;
    if (size != 0) {&lt;br /&gt;
 	tool: for (SListNode node=head; true; node = node.next.next) {&lt;br /&gt;
 		SListNode dupe = new SListNode(node.item, node.next);&lt;br /&gt;
 		node.next = dupe;&lt;br /&gt;
 		if (dupe.next == null) { break tool; }&lt;br /&gt;
 		}&lt;br /&gt;
 	} }&lt;br /&gt;
 &lt;br /&gt;
   /**&lt;br /&gt;
    *  toString() converts the list to a String.&lt;br /&gt;
    *  @return a String representation of the list.&lt;br /&gt;
    **/&lt;br /&gt;
 &lt;br /&gt;
   public String toString() {&lt;br /&gt;
     int i;&lt;br /&gt;
     Object obj;&lt;br /&gt;
     String result = &amp;quot;[  &amp;quot;;&lt;br /&gt;
 &lt;br /&gt;
     SListNode cur = head;&lt;br /&gt;
 &lt;br /&gt;
     while (cur != null) {&lt;br /&gt;
       obj = cur.item;&lt;br /&gt;
       result = result + obj.toString() + &amp;quot;  &amp;quot;;&lt;br /&gt;
       cur = cur.next;&lt;br /&gt;
     }&lt;br /&gt;
     result = result + &amp;quot;]&amp;quot;;&lt;br /&gt;
     return result;&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
   /**&lt;br /&gt;
    *  main() runs test cases on the SList class.  Prints summary&lt;br /&gt;
    *  information on basic operations and halts with an error (and a stack&lt;br /&gt;
    *  trace) if any of the tests fail.&lt;br /&gt;
    **/&lt;br /&gt;
 &lt;br /&gt;
   public static void main (String[] args) {&lt;br /&gt;
     testEmpty();&lt;br /&gt;
     testAfterInsertFront();&lt;br /&gt;
     testAfterInsertEnd();&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
     &lt;br /&gt;
   /**&lt;br /&gt;
    *  testEmpty() tests toString(), isEmpty(), length(), insertFront(), and&lt;br /&gt;
    *  insertEnd() on an empty list.  Prints summary information of the tests&lt;br /&gt;
    *  and halts the program if errors are detected.&lt;br /&gt;
    **/&lt;br /&gt;
 &lt;br /&gt;
   private static void testEmpty() {&lt;br /&gt;
     SList lst1 = new SList();&lt;br /&gt;
     SList lst2 = new SList();&lt;br /&gt;
     System.out.println();&lt;br /&gt;
     System.out.println(&amp;quot;Here is a list after construction: &amp;quot;&lt;br /&gt;
 		       + lst1.toString());&lt;br /&gt;
     TestHelper.verify(lst1.toString().equals(&amp;quot;[  ]&amp;quot;),&lt;br /&gt;
 		      &amp;quot;toString on newly constructed list failed&amp;quot;);&lt;br /&gt;
 &lt;br /&gt;
     System.out.println(&amp;quot;isEmpty() should be true. It is: &amp;quot; +&lt;br /&gt;
 		       lst1.isEmpty());&lt;br /&gt;
     TestHelper.verify(lst1.isEmpty() == true,&lt;br /&gt;
 		      &amp;quot;isEmpty() on newly constructed list failed&amp;quot;);    &lt;br /&gt;
 &lt;br /&gt;
     System.out.println(&amp;quot;length() should be 0. It is: &amp;quot; +&lt;br /&gt;
 		       lst1.length());&lt;br /&gt;
     TestHelper.verify(lst1.length() == 0, &lt;br /&gt;
 		      &amp;quot;length on newly constructed list failed&amp;quot;);    &lt;br /&gt;
     lst1.insertFront(new Integer(3));&lt;br /&gt;
     System.out.println(&amp;quot;Here is a list after insertFront(3) to an empty list: &amp;quot;&lt;br /&gt;
 		       + lst1.toString());&lt;br /&gt;
     TestHelper.verify(lst1.toString().equals(&amp;quot;[  3  ]&amp;quot;),&lt;br /&gt;
 		      &amp;quot;InsertFront on empty list failed&amp;quot;);&lt;br /&gt;
     lst2.insertEnd(new Integer(5));&lt;br /&gt;
     System.out.println(&amp;quot;Here is a list after insertEnd(5) on an empty list: &amp;quot;&lt;br /&gt;
 		       + lst2.toString());&lt;br /&gt;
     TestHelper.verify(lst2.toString().equals(&amp;quot;[  5  ]&amp;quot;),&lt;br /&gt;
 		      &amp;quot;insertEnd on empty list failed&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
 &lt;br /&gt;
   /**&lt;br /&gt;
    *  testAfterInsertFront() tests toString(), isEmpty(), length(),&lt;br /&gt;
    *  insertFront(), and insertEnd() after insertFront().  Prints summary&lt;br /&gt;
    *  information of the tests and halts the program if errors are detected.&lt;br /&gt;
    **/&lt;br /&gt;
 &lt;br /&gt;
   private static void testAfterInsertFront() {&lt;br /&gt;
     SList lst1 = new SList();&lt;br /&gt;
     lst1.insertFront(new Integer(3));&lt;br /&gt;
     lst1.insertFront(new Integer(2));&lt;br /&gt;
     lst1.insertFront(new Integer(1));&lt;br /&gt;
     System.out.println();&lt;br /&gt;
     System.out.println(&amp;quot;Here is a list after insertFront 3, 2, 1: &amp;quot;&lt;br /&gt;
 		       + lst1.toString());&lt;br /&gt;
     TestHelper.verify(lst1.toString().equals(&amp;quot;[  1  2  3  ]&amp;quot;),&lt;br /&gt;
 		      &amp;quot;InsertFronts on non-empty list failed&amp;quot;);&lt;br /&gt;
     System.out.println(&amp;quot;isEmpty() should be false. It is: &amp;quot; +&lt;br /&gt;
 		       lst1.isEmpty());&lt;br /&gt;
     TestHelper.verify(lst1.isEmpty() == false,&lt;br /&gt;
 		      &amp;quot;isEmpty() after insertFront failed&amp;quot;);    &lt;br /&gt;
     System.out.println(&amp;quot;length() should be 3. It is: &amp;quot; +&lt;br /&gt;
 		       lst1.length());&lt;br /&gt;
     TestHelper.verify(lst1.length() == 3, &lt;br /&gt;
 		      &amp;quot;length() after insertFront failed&amp;quot;);&lt;br /&gt;
     lst1.insertEnd(new Integer(4));&lt;br /&gt;
     System.out.println(&amp;quot;Here is the same list after insertEnd(4): &amp;quot;&lt;br /&gt;
 		       + lst1.toString());&lt;br /&gt;
     TestHelper.verify(lst1.toString().equals(&amp;quot;[  1  2  3  4  ]&amp;quot;),&lt;br /&gt;
 		      &amp;quot;insertEnd on non-empty list failed&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
     &lt;br /&gt;
   /**&lt;br /&gt;
    *  testAfterInsertEnd() tests toString(), isEmpty(), length(),&lt;br /&gt;
    *  insertFront(), and insertEnd() after insertEnd().  Prints summary&lt;br /&gt;
    *  information of the tests and halts the program if errors are detected.&lt;br /&gt;
    **/&lt;br /&gt;
 &lt;br /&gt;
   private static void testAfterInsertEnd() {&lt;br /&gt;
     SList lst1 = new SList();&lt;br /&gt;
     lst1.insertEnd(new Integer(6));&lt;br /&gt;
     lst1.insertEnd(new Integer(7));&lt;br /&gt;
     System.out.println();&lt;br /&gt;
     System.out.println(&amp;quot;Here is a list after insertEnd 6, 7: &amp;quot;&lt;br /&gt;
 		       + lst1.toString());&lt;br /&gt;
     System.out.println(&amp;quot;isEmpty() should be false. It is: &amp;quot; +&lt;br /&gt;
 		       lst1.isEmpty());&lt;br /&gt;
     TestHelper.verify(lst1.isEmpty() == false,&lt;br /&gt;
 		      &amp;quot;isEmpty() after insertEnd failed&amp;quot;);    &lt;br /&gt;
     System.out.println(&amp;quot;length() should be 2. It is: &amp;quot; +&lt;br /&gt;
 		       lst1.length());&lt;br /&gt;
     TestHelper.verify(lst1.length() == 2, &lt;br /&gt;
 		      &amp;quot;length() after insertEndfailed&amp;quot;);&lt;br /&gt;
     lst1.insertFront(new Integer(5));&lt;br /&gt;
     System.out.println(&amp;quot;Here is the same list after insertFront(5): &amp;quot;&lt;br /&gt;
 		       + lst1.toString());&lt;br /&gt;
     TestHelper.verify(lst1.toString().equals(&amp;quot;[  5  6  7  ]&amp;quot;),&lt;br /&gt;
 		      &amp;quot;insertFront after insertEnd failed&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Lensovet</name></author>
		
	</entry>
</feed>