From 5dfd36e0dfc491fc85a4874d861cec0c33ba2364 Mon Sep 17 00:00:00 2001 From: John Vonelli Date: Tue, 24 Mar 2015 15:00:24 -0400 Subject: [PATCH] Submission for the Linked List Lab --- src/.DS_Store | Bin 0 -> 6148 bytes src/Node.class | Bin 0 -> 808 bytes src/Node.java | 0 src/Node.java~ | 41 ++++++++ src/SortedLinkedList.class | Bin 0 -> 2435 bytes src/SortedLinkedList.java | 136 +++++++++++++++++++++++-- src/SortedLinkedList.java~ | 199 +++++++++++++++++++++++++++++++++++++ src/testLinkedList.class | Bin 0 -> 2129 bytes src/testLinkedList.java | 97 ++++++++++++++++++ src/testLinkedList.java~ | 55 ++++++++++ 10 files changed, 522 insertions(+), 6 deletions(-) create mode 100644 src/.DS_Store create mode 100644 src/Node.class mode change 100755 => 100644 src/Node.java create mode 100644 src/Node.java~ create mode 100644 src/SortedLinkedList.class mode change 100755 => 100644 src/SortedLinkedList.java create mode 100644 src/SortedLinkedList.java~ create mode 100644 src/testLinkedList.class create mode 100644 src/testLinkedList.java create mode 100644 src/testLinkedList.java~ diff --git a/src/.DS_Store b/src/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T07m3C!2$Z8`p`;^ zD)pg1fFFgJJx5TH$kN{4?#}Go`1bDaKL7{VE5pZT2?cCP*p~1vz)pbo3SOEFw8BdB zJh@65{Uq%*PP36odxr|ei*b@>Ci||SPGnTyWD0(>HTG>bl|w~6@3 zNrmO6Np)*H=;+Z|(&^iE!*0@VCnF>GY0=BRnM-apnI5#Ps!-A|4!6lh&u@vPsksGA?I zD4`r;0U=*0eTB-SgP%L+x=X95pp7ls?ej;ta0JQB5wJap=UIRv@e<=QZyoksp4Ipd zRQzEACtjPtjnxF+FN>-$)`$Ws@bHXmD?~r%SLF@Z%<-%WJDJ!il3lVYH#^K%$&l}B z@RVL5{tf3R6Nj{*WGTM1gke4~B~*C9`YhP}@dUg+7yJsZ=YYMZo44nJ-(YPDKBi7b gbc#j)86(dF_GnLJ8EW~|gBd>7X^T90!^(Yo51J@;X8-^I literal 0 HcmV?d00001 diff --git a/src/Node.java b/src/Node.java old mode 100755 new mode 100644 diff --git a/src/Node.java~ b/src/Node.java~ new file mode 100644 index 0000000..253c9bb --- /dev/null +++ b/src/Node.java~ @@ -0,0 +1,41 @@ +/** + * This class represents a single node in a linked list. The data stored + * in the node is immutable, but the next field can be modified. + */ +public class Node +{ + private String name; + private int quantity; + private Node next; + + /** + * Create a node with the specified name and quantity. The next field will + * be null. + */ + public Node(String itemName, int itemQuantity) + { + name = itemName; + quantity = itemQuantity; + next = null; + } + + public String getNadme() + { + return name; + } + + public int getQuantity() + { + return quantity; + } + + public Node getNext() + { + return next; + } + + public void setNext(Node newNext) + { + next = newNext; + } +} diff --git a/src/SortedLinkedList.class b/src/SortedLinkedList.class new file mode 100644 index 0000000000000000000000000000000000000000..bd5e2a8509bfb07691801b0e7817fa39dce7a8cd GIT binary patch literal 2435 zcmai#+jCP@6vn@uoRgEBp05 z5Gf4=RIqTjr=hAeGLKTAOI}+Bv(tS-{^B8{+nME(2y+#r7{Dng`%A+Gh*2qF9_5Rmd(t` z0khz^5=}rGv&w06oWYwr`eUgR<|(r)Yv#tf2Fr9ElQHbvsC7m_^wZAdD>7bC#&g}m zkNRkMQJ{{2_f^bX*)E?Y0E5%t@sq2}Icw#{%Ez6Y<&Iq%uw<$d$cotDZns%NdG`Pwv0rc&fRN}*_-63_?pm14%)WlM#d=Zd&mDq`R< zL<}rOi-ti1`*A>^Wv*1)Dt30%DjHaUAp-~T5+&;~kU*E54Rh9R;1Gs195(Pcju<$K zV+LMEL;$3b#PXtU1FygoST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ortedLinkedList.java old mode 100755 new mode 100644 index f75caa0..4ebbafe --- a/src/SortedLinkedList.java +++ b/src/SortedLinkedList.java @@ -34,7 +34,24 @@ public void print() */ public String getName(int index) { + Node temp = head; + int count = 0; + String tempName = ""; + while(temp != null){ + if(count == index) + { + tempName = temp.getName(); + return tempName; + } + else{ + count++; + } + temp = temp.getNext(); + } + return null; + + } /** @@ -44,7 +61,20 @@ public String getName(int index) */ public int getQuantity(int index) { - + Node temp = head; + int count = 0; + int tempQuan = 0; + while(temp != null){ + if(count == index){ + tempQuan = temp.getQuantity(); + return tempQuan; + } + else{ + count++; + } + temp = temp.getNext(); + } + return -1; } /** @@ -52,7 +82,19 @@ public int getQuantity(int index) */ public int length() { + int count = 0; + Node temp = head; + if(temp == null) + { + return count; + } + while(temp != null) + { + count++; + temp = temp.getNext(); + } + return count; } /** @@ -61,15 +103,97 @@ public int length() */ public boolean isMember(String name) { + Node temp = head; + String tempName = ""; + while(temp != null){ + tempName = temp.getName(); + if(name == tempName) + { + return true; + } + else{ + temp = temp.getNext(); + } + } + return false; } - /** - * This method will add the specified name/quantity to the list in sorted - * order. This order is specified by the quantity from low to high. - */ +// public void insert(String name, int quantity) +// { +// Node temp = head; +// if(temp == null){ +// Node newNode = new Node(name, quantity); +// newNode.setNext(head); +// head = newNode; +// } +// else{ +// Node newNode2 = new Node(name, quantity); +// if(temp.getQuantity() < newNode2.getQuantity()){ +// newNode2.setNext(head); +// head = newNode2; +// } +// else{ +// while(temp.getNext() != null){ +// temp = temp.getNext(); +// } +// temp.setNext(newNode2); +// } +// } + +// } public void insert(String name, int quantity) { + Node curr = head; + Node prev = null; - } + if(curr == null){ + Node newNode = new Node(name, quantity); + newNode.setNext(head); + head = newNode; + } + + else{ + Node newNode2 = new Node(name, quantity); + while(curr != null){ + if(newNode2.getQuantity() > curr.getQuantity()){ + if(prev == null){ + Node newNode = new Node(name, quantity); + newNode.setNext(head); + head = newNode; + break; + } + else{ + prev.setNext(newNode2); + newNode2.setNext(curr); + break; + } + } + if(newNode2.getQuantity() == curr.getQuantity()){ + if(prev == null){ + Node newNode = new Node(name, quantity); + newNode.setNext(head); + head = newNode; + break; + } + else{ + prev.setNext(newNode2); + newNode2.setNext(curr); + } + } + prev = curr; + curr = curr.getNext(); + } + if(curr == null){ + prev.setNext(newNode2); + } + } + + } + +// newNode2.setNext(head); +// head = newNode2; +// temp.getQuantity() < newNode2.getQuantity() + } + diff --git a/src/SortedLinkedList.java~ b/src/SortedLinkedList.java~ new file mode 100644 index 0000000..ac8a110 --- /dev/null +++ b/src/SortedLinkedList.java~ @@ -0,0 +1,199 @@ +public class SortedLinkedList2 +{ + private Node head; + + /** + * Create an empty list. + */ + public SortedLinkedList() + { + head = null; + } + + /** + * Print all the elements of the list to the console. Each element will have + * the form "name:quantity" and the entries will be separated by a single + * space. + */ + public void print() + { + Node temp = head; + while(temp != null) + { + System.out.print(temp.getName() + ":" + temp.getQuantity() + " "); + temp = temp.getNext(); + } + + System.out.println(); + } + + /** + * This method will return the name at the specified index. Similar to an + * array, the index of the first entry is zero. If the index is invalid, + * this method will return null. + */ + public String getName(int index) + { + Node temp = head; + int count = 0; + String tempName = ""; + while(temp != null){ + if(count == index) + { + tempName = temp.getName(); + return tempName; + } + else{ + count++; + } + temp = temp.getNext(); + } + + return null; + + + } + + /** + * This method will return the quantity at the specified index. Similar to + * an array, the index of the first entry is zero. If the index is invalid, + * this method will return -1. + */ + public int getQuantity(int index) + { + Node temp = head; + int count = 0; + int tempQuan = 0; + while(temp != null){ + if(count == index){ + tempQuan = temp.getQuantity(); + return tempQuan; + } + else{ + count++; + } + temp = temp.getNext(); + } + return -1; + } + + /** + * This method will return the number of elements currently held in the list. + */ + public int length() + { + int count = 0; + Node temp = head; + if(temp == null) + { + return count; + } + while(temp != null) + { + count++; + temp = temp.getNext(); + } + + return count; + } + + /** + * This method will return whether or not the specified name is contained in + * the list. + */ + public boolean isMember(String name) + { + Node temp = head; + String tempName = ""; + while(temp != null){ + tempName = temp.getName(); + if(name == tempName) + { + return true; + } + else{ + temp = temp.getNext(); + } + } + + return false; + } + +// public void insert(String name, int quantity) +// { +// Node temp = head; +// if(temp == null){ +// Node newNode = new Node(name, quantity); +// newNode.setNext(head); +// head = newNode; +// } +// else{ +// Node newNode2 = new Node(name, quantity); +// if(temp.getQuantity() < newNode2.getQuantity()){ +// newNode2.setNext(head); +// head = newNode2; +// } +// else{ +// while(temp.getNext() != null){ +// temp = temp.getNext(); +// } +// temp.setNext(newNode2); +// } +// } + +// } + public void insert(String name, int quantity) + { + Node curr = head; + Node prev = null; + + if(curr == null){ + Node newNode = new Node(name, quantity); + newNode.setNext(head); + head = newNode; + } + + else{ + Node newNode2 = new Node(name, quantity); + while(curr != null){ + if(newNode2.getQuantity() > curr.getQuantity()){ + if(prev == null){ + Node newNode = new Node(name, quantity); + newNode.setNext(head); + head = newNode; + break; + } + else{ + prev.setNext(newNode2); + newNode2.setNext(curr); + break; + } + } + if(newNode2.getQuantity() == curr.getQuantity()){ + if(prev == null){ + Node newNode = new Node(name, quantity); + newNode.setNext(head); + head = newNode; + break; + } + else{ + prev.setNext(newNode2); + newNode2.setNext(curr); + } + } + prev = curr; + curr = curr.getNext(); + } + if(curr == null){ + prev.setNext(newNode2); + } + } + + } + +// newNode2.setNext(head); +// head = newNode2; +// temp.getQuantity() < newNode2.getQuantity() + +} + diff --git a/src/testLinkedList.class b/src/testLinkedList.class new file mode 100644 index 0000000000000000000000000000000000000000..6de41799f18948d53cab06c5e48d09ff7e39e125 GIT binary patch literal 2129 zcmb`H>vGdZ6vzMaRTw2PA~u({0YXUZ$i!TdPzack5Q7KDT%4vQy-byONd$^bWf_K# zeStneU!b4+C1lze9)O4H^uMybHckrpz-VT*dvx}k-~P|hzW?{{cK~KlOu)oc9GL_{ zn36*_fn%7~VaY{K4i}|mM$YnbxFnaCbzISLRmZH3xj5$IxTYa?)2TatK|?r`UDFU* zYSe^=k+M@4mDbLN@K)^&mlm_!*tFd>+jHc)V~qIQPE$j&?2D$)EiXi^>@@ua4MWnh zyz|n3BMllfx|!u_!}Fy}pX0D}tUC2AS5$)3slXsM8}*trRK)B1!mepJ@uB_BGhcY^ zFKPMZiFhe&2A8H%y(v5nWq29|JF zE|)3%qT`YXrCR-m!fOWtq z#}rq2zBRt1({G{u#YqThj`CU80n_bGoq#%y;0t`oz#6_%p?~8(8G5c8+A?GMF5+g)3hzSS!x3Fh z5=<81r(GP~gAv)6ehhI2NilsIBSFGb80QIcOgYP>J)Aj(b5Y@pDIC9plN-RX%%NmL zVhwe%2pDMt_Cz>MJ%KS&oFTN;s|kY<5dZ&9fNmAc(Wa8Rwr6~XTvr3(9$ zYAd9==8y`FlLox)bS#o-i5z#SP!U<&qfYlpSn6XdD0SwQI_E#e_CX(Yt9aPzwb>q! zt<26^AxDL5kI43zY){Bm{RG>DV*9hB+ryr218gJm4zSE*`=?7D&|o9o24k!7T97wI z??;^G?FW2I*fqj_N3ietTRz1keqiJM$ddm=X@BMse(8gLs06*I1iipGAx!=UG<;VY literal 0 HcmV?d00001 diff --git a/src/testLinkedList.java b/src/testLinkedList.java new file mode 100644 index 0000000..6fe2b26 --- /dev/null +++ b/src/testLinkedList.java @@ -0,0 +1,97 @@ +import junit.framework.TestCase; + +/** + * A JUnit test case class. + * Every method starting with the word "test" will be called when running + * the test with JUnit. + */ +public class testLinkedList extends TestCase { + + /** + * A test method. + * (Replace "X" with a name describing the test. You may write as + * many "testSomething" methods in this class as you wish, and each + * one will be called when running JUnit over this class.) + */ + public void testEmptyList() { + SortedLinkedList s = new SortedLinkedList(); + assertEquals(0, s.length()); + } + public void testSingleNode(){ + SortedLinkedList s = new SortedLinkedList(); + s.insert("Steve", 5); + + assertEquals("Steve", s.getName(0)); + assertEquals(5, s.getQuantity(0)); + assertEquals(true, s.isMember("Steve")); + assertEquals(1, s.length()); + } + public void testSecondNodeNewHead(){ + SortedLinkedList s = new SortedLinkedList(); + s.insert("Steve", 5); + s.insert("Bill", 10); + + assertEquals("Bill", s.getName(0)); + assertEquals("Steve", s.getName(1)); + } + public void testSecondNodeAfterHead(){ + SortedLinkedList s = new SortedLinkedList(); + s.insert("Bill", 10); + s.insert("Steve", 5); + + assertEquals("Bill", s.getName(0)); + assertEquals("Steve", s.getName(1)); + } + public void testRepeatedHeadInsert(){ + SortedLinkedList s = new SortedLinkedList(); + s.insert("Steve", 5); + s.insert("Bill", 10); + s.insert("Sam", 20); + + assertEquals(3, s.length()); + assertEquals("Sam", s.getName(0)); + assertEquals("Bill", s.getName(1)); + assertEquals("Steve", s.getName(2)); + } + public void testSecondTail(){ + SortedLinkedList s = new SortedLinkedList(); + s.insert("Steve", 5); + s.insert("Bill", 10); + + assertEquals("Bill", s.getName(0)); + assertEquals("Steve", s.getName(1)); + } + public void testMiddleThird(){ + SortedLinkedList s = new SortedLinkedList(); + s.insert("Steve", 5); + s.insert("Sam", 20); + s.insert("Bill", 10); + + assertEquals("Sam", s.getName(0)); + assertEquals("Bill", s.getName(1)); + assertEquals("Steve", s.getName(2)); + } + public void testMiddleThird2(){ + SortedLinkedList s = new SortedLinkedList(); + s.insert("Bill", 10); + s.insert("Steve", 5); + s.insert("Sam", 20); + + assertEquals("Sam", s.getName(0)); + assertEquals("Bill", s.getName(1)); + assertEquals("Steve", s.getName(2)); + } + public void testSame(){ + SortedLinkedList s = new SortedLinkedList(); + s.insert("Bill", 10); + s.insert("Steve", 5); + s.insert("Sally", 5); + s.insert("Bilbo", 10); + + assertEquals("Bilbo", s.getName(0)); + assertEquals("Bill", s.getName(1)); + assertEquals("Sally", s.getName(2)); + assertEquals("Steve", s.getName(3)); + + } +} diff --git a/src/testLinkedList.java~ b/src/testLinkedList.java~ new file mode 100644 index 0000000..d493218 --- /dev/null +++ b/src/testLinkedList.java~ @@ -0,0 +1,55 @@ +import junit.framework.TestCase; + +/** + * A JUnit test case class. + * Every method starting with the word "test" will be called when running + * the test with JUnit. + */ +public class testLinkedList extends TestCase { + + /** + * A test method. + * (Replace "X" with a name describing the test. You may write as + * many "testSomething" methods in this class as you wish, and each + * one will be called when running JUnit over this class.) + */ + public void testEmptyList() { + SortedLinkedList s = new SortedLinkedList(); + assertEquals(0, s.length()); + } + public void testSingleNode(){ + SortedLinkedList s = new SortedLinkedList(); + s.insert("Steve", 5); + + assertEquals("Steve", s.getName(0)); + assertEquals(5, s.getQuantity(0)); + assertEquals(true, s.isMember("Steve")); + assertEquals(1, s.length()); + } + public void testSecondNodeNewHead(){ + SortedLinkedList s = new SortedLinkedList(); + s.insert("Steve", 5); + s.insert("Bill", 10); + + assertEquals("Bill", s.getName(0)); + assertEquals("Steve", s.getName(1)); + } + public void testSecondNodeAfterHead(){ + SortedLinkedList s = new SortedLinkedList(); + s.insert("Bill", 10); + s.insert("Steve", 5); + + assertEquals("Bill", s.getName(0)); + assertEquals("Steve", s.getName(1)); + } + public void testRepeatedHeadInsert(){ + SortedLinkedList s = new SortedLinkedList(); + s.insert("Steve", 5); + s.insert("Bill", 10); + s.insert("Sam", 20); + + assertEquals("Sam", s.getName(0)); + assertEquals("Bill", s.getName(1)); + assertEquals("Steve", s.getName(2)); + } +}