我试图从元素列表中找出哪些数字较小以及哪个数字比一个数字更大。下面是我到目前为止的代码。我正在尝试实现这两种方法......
public MyDynamicList smallerMyList(MyDynamicList m, int e)
public MyDynamicList biggerEqualMyList(MyDynamicList m, int e)
下面是我所有的代码到目前为止我所做的任何提示如何实现这两种方法都将非常欣赏..
public class DivideAndConquerAlgorithms {
public DivideAndConquerAlgorithms(){}
//-------------------------------------------------------------------
// smallerMyList --> Filters all elements in MyDynamicList smaller than e
//-------------------------------------------------------------------
/**
* The function filters all elements of MyDynamicList being smaller than 'e'
* @param m: The MyDynamicList we want to check.
* @param e: The number 'e' we want to compare each element of MyDynamicList
to.
* @return: The new MyDynamicList containing just the elements being smaller
than 'e'
*/
public MyDynamicList smallerMyList(MyDynamicList m, int e){
MyDynamicList res = null;
return res;
}
//-------------------------------------------------------------------
// biggerMyList --> Filters all elements in MyDynamicList bigger than e
//-------------------------------------------------------------------
/**
* The function filters all elements of MyDynamicList being bigger than 'e'
* @param m: The MyDynamicList we want to check.
* @param e: The number 'e' we want to compare each element of MyDynamicList
to.
* @return: The new MyDynamicList containing just the elements being bigger
or equal than 'e'
*/
public MyDynamicList biggerEqualMyList(MyDynamicList m, int e){
MyDynamicList res = null;
return res;
}
}
这是我的MyDynamicLilst类.......
public class MyDynamicList{
//--------------------------------------------------
// Attributes
//--------------------------------------------------
/**
* @param items: It stores the items of MyDynamicList.
* @param numItems: It represents the number of items of MyDynamicList.
* @param maxItems: It represents the maximum number of items a MyDynamicList can contain.
*/
private MyNode head;
private int numItems;
/**
* The constructor creates 1 instance (1 object) of the class MyDynamicList<br>
*/
public MyDynamicList(){
this.head = null;
this.numItems = 0;
}
//-------------------------------------------------------------------
// Basic Operation --> Get number of integers in MyDynamicList: my_get_length
//-------------------------------------------------------------------
/**
* Given a concrete MyDynamicList, it returns its number of items.<br>
* @return: Number of items of MyDynamicList.
*/
public int length(){
//-----------------------------
//Output Variable --> InitialValue
//-----------------------------
int res = 0;
//-----------------------------
//SET OF OPS
//-----------------------------
//-----------------------------
// I. SCENARIO IDENTIFICATION
//-----------------------------
int scenario = 0;
//Rule 1. The number of elements is included in the attribute numItems
scenario = 1;
//-----------------------------
// II. SCENARIO IMPLEMENTATION
//-----------------------------
switch(scenario){
//Rule 1. The number of elements is included in the attribute numItems
case 1:
res = this.numItems;
break;
}
//-----------------------------
//Output Variable --> Return FinalValue
//-----------------------------
return res;
}
//-------------------------------------------------------------------
// Basic Operation --> Get integer of MyDynamicList at a concrete index: my_get_element
//-------------------------------------------------------------------
/**
* Given a concrete MyDynamicList and a concrete index, it returns the item placed at that index (if any).<br>
* @param index: Index of MyDynamicList we are looking for.
* @return: Item placed at the desired index of MyDynamicList (ERROR if there is no item at such position).
*/
public int getElement(int index){
//-----------------------------
//Output Variable --> InitialValue
//-----------------------------
int res = 0;
//-----------------------------
//SET OF OPS
//-----------------------------
//-----------------------------
// I. SCENARIO IDENTIFICATION
//-----------------------------
int scenario = 0;
//Rule 1. Valid index
if ((index >= 0) && (index < numItems))
scenario = 1;
//Rule 2. Invalid index
else
scenario = 2;
//-----------------------------
// II. SCENARIO IMPLEMENTATION
//-----------------------------
switch(scenario){
//Rule 1. Valid index
case 1:
//1. We declare a reference node and a count of the elements we have traversed so far
MyNode current = this.head;
int count = 0;
//2. We look for the node to be consulted
while (count < index){
//2.1. We make current to point to its next node
current = current.getNext();
//2.2. We increase the amount of nodes visited so far.
count = count + 1;
}
//3. We assign the result to the info of the desired node
res = current.getInfo();
break;
//Rule 2. Invalid index
case 2:
res = 0;
System.out.println("ERROR: Cannot get item, invalid index " + index);
break;
}
//-----------------------------
//Output Variable --> Return FinalValue
//-----------------------------
return res;
}
//-------------------------------------------------------------------
// Basic Operation --> Add integer to MyDynamicList at a concrete index: my_add_element
//-------------------------------------------------------------------
/**
* Given a concrete MyDynamicList, an index and an item, it adds the item at the desired index of MyDynamicList (if capacity allows it).<br>
* @param index: Index of MyDynamicList we want to add the item at.
* @param item: Item we want to add to MyDynamicList.
*/
public void addElement(int index, int item){
//-----------------------------
//SET OF OPS
//-----------------------------
//-----------------------------
// I. SCENARIO IDENTIFICATION
//-----------------------------
int scenario = 0;
//Rule 1. Valid index, add at head
if (index == 0)
scenario = 1;
else{
//Rule 2. Valid index, add after head
if ((index > 0) && (index <= this.numItems))
scenario = 2;
//Rule 3. Invalid index
else
scenario = 3;
}
//-----------------------------
// II. SCENARIO IMPLEMENTATION
//-----------------------------
MyNode newNode = null;
MyNode currentNode = null;
switch(scenario){
//Rule 1. Valid index, add at head
case 1:
//1. We declare a reference node to head
currentNode = head;
//2. We create the node
newNode = new MyNode(item, null);
//3. We make newNode to be the first node of MyList
this.head = newNode;
//4. We make the new node to point at current
newNode.setNext(currentNode);
//5. We increase numItems
this.numItems = this.numItems + 1;
break;
//Rule 2. Valid index, add after head
case 2:
//1. We declare a reference node to head, and another one pointing to its previous node.
//We also declare a count of the elements we have traversed so far
currentNode = head;
MyNode previousNode = null;
int count = 0;
//2. We look for the place the node should be inserted
while (count < index){
//2.1. We make previous node to point to the current one.
previousNode = currentNode;
//2.2. We make current to point to its next node
currentNode = currentNode.getNext();
//2.3. We increase the amount of nodes visited so far.
count = count + 1;
}
//3. We create the node
newNode = new MyNode(item, null);
//4. We make previous node to point to newNode
previousNode.setNext(newNode);
//5. We make the new node to point at current
newNode.setNext(currentNode);
//6. We increase numItems
this.numItems = this.numItems + 1;
break;
//Rule 3. Invalid index
case 3:
System.out.println("ERROR: Cannot add item, invalid index " + index);
break;
}
}
//-------------------------------------------------------------------
// Basic Operation --> Remove index of MyDynamicList: my_remove_element
//-------------------------------------------------------------------
/**
* Given a concrete MyDynamicList and index, it removes the item placed at the desired index of MyDynamicList (if any).
* @param index: Index of MyDynamicList we want to remove its item from.
*/
public void removeElement(int index){
//-----------------------------
//SET OF OPS
//-----------------------------
//-----------------------------
// I. SCENARIO IDENTIFICATION
//-----------------------------
int scenario = 0;
//Rule 1. Invalid index
if ((index < 0) || (index > this.numItems))
scenario = 1;
else{
//Rule 2. Valid index, remove first node
if (index == 0)
scenario = 2;
//Rule 3. Valid index, remove after first node
else
scenario = 3;
}
//-----------------------------
// II. SCENARIO IMPLEMENTATION
//-----------------------------
switch(scenario){
//Rule 1. Invalid index
case 1:
System.out.println("ERROR: Cannot remove item, invalid index " + index);
break;
//Rule 2. Valid index, remove first node
case 2:
//1. Update head
head = head.getNext();
//2. We decrease the number of items
this.numItems = this.numItems - 1;
break;
//Rule 1. Valid index, remove after first node
case 3:
//1. We declare a reference node to head, and another one pointing to its previous node.
//We also declare a count of the elements we have traversed so far
MyNode currentNode = head;
MyNode previousNode = null;
int count = 0;
//2. We look for the place the node should be deleted
while (count < index){
//2.1. We make previous node to point to the current one.
previousNode = currentNode;
//2.2. We make current to point to its next node
currentNode = currentNode.getNext();
//2.3. We increase the amount of nodes visited so far.
count = count + 1;
}
//3. Update previous
previousNode.setNext(currentNode.getNext());
//4. We make current to point to null
currentNode = null;
//4. We decrease the number of items
this.numItems = this.numItems - 1;
break;
}
}
}
这是我的主要课程
public class MyMain {
/**
* This function is used to test the divide and conquer algorithms.
*/
public static void test(){
//1. We create the object to test the exercises
DivideAndConquerAlgorithms ex = new DivideAndConquerAlgorithms();
//2. We create extra variables for the tests
// m1 = []
MyDynamicList m1 = new MyDynamicList();
// m2 = [9,3,2]
MyDynamicList m2 = new MyDynamicList();
m2.addElement(0, 9);
m2.addElement(1, 3);
m2.addElement(2, 2);
// m3 = [4,8,1,3,6,5,2,6]
MyDynamicList m3 = new MyDynamicList();
m3.addElement(0, 4);
m3.addElement(1, 8);
m3.addElement(2, 1);
m3.addElement(3, 3);
m3.addElement(4, 6);
m3.addElement(5, 5);
m3.addElement(6, 2);
m3.addElement(7, 6);
//3. We create extra variables for the results
MyDynamicList resM = null;
//----------------------------
//test smallerMyList
//----------------------------
System.out.println("\n----------- Test: smallerMyList -------------\n");
/*resM = ex.smallerMyList(m1, 9);
ex.recursiveDisplayElements(resM);
resM = ex.smallerMyList(m2, 9);
ex.recursiveDisplayElements(resM);
resM = ex.smallerMyList(m3, 6);
ex.recursiveDisplayElements(resM);
*/
//----------------------------
// test biggerEqualMyList
//----------------------------
System.out.println("\n----------- Test: biggerEqualMyList -------------
\n");
/*resM = ex.biggerEqualMyList(m1, 9);
ex.recursiveDisplayElements(resM);
resM = ex.biggerEqualMyList(m2, 9);
ex.recursiveDisplayElements(resM);
resM = ex.biggerEqualMyList(m3, 6);
ex.recursiveDisplayElements(resM);*/
}
/**
* Main Method.
* @param args: We will run the program parameter free, so do not worry
about it.
*/
public static void main(String[] args) {
test();
}
}
最后是我的节点类
public class MyNode {
private int info;
private MyNode next;
public MyNode(int i, MyNode n){
this.info = i;
this.next = n;
}
public int getInfo(){
return this.info;
}
public MyNode getNext(){
return this.next;
}
public void setInfo(int i){
this.info = i;
}
public void setNext(MyNode n){
this.next = n;
}
}