有两个编程题 各位大哥帮看看 绝对给分 信誉第一
1。设一个双向循环链表L,用代码实现在链表的给的定节点p后加入一个节点q, 和删除节点p的操作,要求用类L,节点Node,以及方法add(),delete()
2。设一个整数数组int[]array,长度为M,该数组中任意连续X(1 <=X <=M)个元素可以组成一个子数组,每个子数组的所有元素的和为Si。求Si最大的子数组的起始下标值以及截止下标值
------解决方案--------------------数据结构的问题,不敢瞎说,等高手的优秀代码
估计第二题得三层循环
------解决方案--------------------第一个:
public class L
{
private java.util.LinkedList list;
public java.util.LinkedList getList()
{
return list;
}
public void setList(java.util.LinkedList list)
{
this.list = list;
}
public L()
{
list = new java.util.LinkedList();
for (int i = 0; i < 10; i++)
{
Node node = new Node( "name " + i, "value " + i);
list.add(node);
}
}
public void add(Node node, Node position)
{
int index = list.indexOf(position);
list.add(index + 1, node);
}
public void add(Node node, int position)
{
list.add(position, node);
}
public boolean delete(Node node)
{
return list.remove(node);
}
public String toString()
{
StringBuffer sb = new StringBuffer();
sb.append( "The list is: { ");
java.util.Iterator i = list.iterator();
while (i.hasNext())
{
Node tmp = (Node) i.next();
sb.append( "[ " + tmp.getName() + ", " + tmp.getValue() + "] ");
}
sb.append( "} ");
return sb.toString();
}
public static void main(String[] args)
{
L l = new L();
System.out.println( "Begin: " + l);
l.add(new Node( "csdn ", "中国软件开发网 "), 3);
System.out.println( "after add 1: " + l);
l.add(new Node( "java ", "www.java.sun.com "),new Node( "csdn ", "中国软件开发网 "));
System.out.println( "after add 2: " + l);
l.delete(new Node( "name5 ", "value5 "));
System.out.println( "after delete: " + l);
}
}
class Node
{
private String name;
private String value;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getValue()
{
return value;
}
public void setValue(String value)
{
this.value = value;
}
public Node(String name, String value)
{
this.name = name;
this.value = value;
}
public boolean equals(Object node)
{
if ((name.equals(((Node) node).getName())) && (value.equals(((Node) node).getValue())))
return true;
return false;
}
}
------解决方案--------------------upup
------解决方案--------------------public class Untitled1 {
public Untitled1() {
}
public static void getMax(int[] array){
int max=array[0];
int begin=0;
int end=0;
for(int i=0;i <array.length;i++){
int sum=array[i];
for (int j = i + 1; j < array.length; j++) {
sum+=array[j];
if(sum> max){
max=sum;
begin=i;
end=j;
}
}
}
System.out.println(max);