Skip to content

Submission for the Linked List Lab #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added src/.DS_Store
Binary file not shown.
Binary file added src/Node.class
Binary file not shown.
Empty file modified src/Node.java
100755 → 100644
Empty file.
41 changes: 41 additions & 0 deletions src/Node.java~
Original file line number Diff line number Diff line change
@@ -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;
}
}
Binary file added src/SortedLinkedList.class
Binary file not shown.
136 changes: 130 additions & 6 deletions src/SortedLinkedList.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -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;


}

/**
Expand All @@ -44,15 +61,40 @@ 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;
}

/**
* 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;
}

/**
Expand All @@ -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()

}

Loading