如题,主要考虑执行效率如果只有一个元素,不删除
如果全都是相同元素,保留一个节点
其他情况,删除所有最小元素节点

解决方案 »

  1.   

    自己先丢一个吧,欢迎不同解法都上来啊
    public class LinkNode  
    {
    private int data;
    private LinkNode next;
    private LinkList list;
    public LinkNode(LinkList list)
    {
    this.list=list;
    }public LinkNode(int data)
    {
    this.data=data;
    }
    public void setNext(LinkNode next)
    {
    this.next=next;
    }public LinkNode getNext()
    {
    return this.next;
    }
    public void setData(int data)
    {
    this.data=data;
    }
    public int getData()
    {
    return data;
    }
    }public class AlgNode  
    {
    LinkNode node;
    LinkNode previousNode;public AlgNode(LinkNode node,LinkNode preNode)
    {
    this.node=node;
    this.previousNode=preNode;
    }public LinkNode getNode()
    {
    return this.node;
    }public LinkNode getPreNode()
    {
    return previousNode;
    }
    }
    public class LinkList  
    {private LinkNode head;
    private LinkNode tailNode;
    private int size=0;
    // private LinkNode currentNode = null;
    public LinkList(LinkNode head)
    {
    this.head=head;
    tailNode=head;
    }public LinkList()
    {
    head = new LinkNode(this);
    head.setNext(null);
    this.tailNode=head;
    }public void setSize(int size)
    {
    this.size=size;
    }//删除第一个元素
    public void delete()
    {
    if(head.getNext()!=null)
    {
    this.head.setNext(head.getNext().getNext());
    this.size--;
    }
    else
    this.tailNode=head;
    }//在当前节点之后插入新节点
    public void append(int data)
    {
    LinkNode node = new LinkNode(data);
    this.tailNode.setNext(node);
    tailNode = node;
    this.size++;
    }public LinkNode getTailNode()
    {
    return tailNode;
    }public int getSize()
    {
    return this.size;
    }public LinkNode getHead()
    {
    return this.head;
    }// public boolean delete(LinkNode node)
    // {
    // 
    // return false;
    // }
    }public class Main {/**
    * @param args
    */
    public static void main(String[] args)  
    {
    LinkList linkList = new LinkList();
    linkList.append(2);
    linkList.append(2);
    linkList.append(2);
    linkList.append(2);
    linkList.append(3);
    linkList.append(1);
    linkList.append(5);LinkNode head = linkList.getHead();
    LinkNode currNode = head.getNext();
    LinkNode preNode = head;
    List<AlgNode> list = new ArrayList<AlgNode>();
    int minval=currNode.getData();while(currNode!=null)
    {
    if(minval>currNode.getData())
    {
    minval=currNode.getData();
    list.clear();
    AlgNode node = new AlgNode (currNode,preNode);
    list.add(node);
    }
    else if(minval==currNode.getData())
    {
    AlgNode node = new AlgNode (currNode,preNode);
    list.add(node);
    }
    preNode = currNode;
    currNode=currNode.getNext(); 
    }AlgNode algNode = null;
    for(int i=list.size()-1;i>=0&&linkList.getSize()>1;i--)
    {
    algNode=list.get(i);
    LinkNode pnode=algNode.getPreNode();
    LinkNode node = algNode.getNode();
    pnode.setNext(node.getNext());
    linkList.setSize(linkList.getSize()-1);
    }currNode = head.getNext();
    while(currNode!=null)
    {
    System.out.print(currNode.getData()+" ");
    currNode = currNode.getNext();
    }
    }}
      

  2.   

    你先提供一个单链表的所提供的方法。java的 LinkedList 是 双向链表。
      

  3.   

    大家有人用过java调用extmail的userctl.pl脚本 往extmail中添加用户吗,我自己写了代码执行了 但是不会向extmail中插入数据.我的代码如下
    String comm = "perl /var/www/extsuite/extman/tools/userctl.pl--mod=add -username="+uname +"-password="+pass;
    runLinuxCmd(String cmd) //本类调用public String runLinuxCmd(String cmd) {
    BufferedReader bf = null;
    try {
    Process process = Runtime.getRuntime().exec(cmd);
    bf = new BufferedReader(new InputStreamReader(process.getInputStream()));
    String line = "";
    String resutl="";
    while ((line = bf.readLine()) != null) {
    resutl =resutl+ line.trim()+"\n";
    }
    System.out.println("---------------try result------------------"+resutl);
    return resutl;
    } catch (java.io.IOException e) {
    e.printStackTrace();
    System.out.println("------------error--------------");
    return null;
    } finally {
    if (bf != null) {
    try {
    bf.close();
    bf = null;
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }}单独在shell环境下执行perl命令是没有问题,能够插入数据。 我在网上看到有人讲过把perl命令写进sh脚本就可以了,我也不知道这是为什么按理说java能够调用shell命令也能调用perl脚本命令。非要包装一下的话我也写了一个shell脚本单独运行shell脚本也能添加数据就是java调用又没有添加数据了 我想要的是验证过的结果 大道理就不用讲了直接来真工夫啊 我写的shell脚本代码如下:#!/bin/sh
    perl /var/www/extsuite/extman/tools/userctl.pl--mod=add -username="$1" -password="$2";这个做了两天都没有做好 如果能有满意的结果我会加分的谢谢大家   欢迎大家来这里回帖http://topic.csdn.net/u/20110821/20/ae1e4815-1c81-4c75-88ae-db263128f963.html?61772
      

  4.   

    linkList.setSize(linkList.getSize()-1);作为一个链表,size怎么可以由外部改变?
      

  5.   

    public class Node {
    Object data; Node next; Node(Object d) {
    data = d;
    next = null;
    }
    }public class LinkList { /* 用变量来实现表头 */
    private Node Head = null; private Node Tail = null; private Node Pointer = null; //point to previous private int Length = 0; public void deleteAll() {// 清空整个链表
    Head = null;
    Tail = null;
    Pointer = null;
    Length = 0;
    } // 链表复位,使第一个节点成为当前节点
    public void reset() {
    Pointer = null;
    } // 判断链表是否为空
    public boolean isEmpty() {
    return (Length == 0);
    } // 判断当前结点是否为最后一个结点
    public boolean isEnd() {
    if (Length == 0)
    throw new java.lang.NullPointerException();
    else if (Length == 1)
    return true;
    else
    return (cursor() == Tail);
    } // 返回当前结点的下一个结点的值,并使其成为当前结点
    public Object nextNode() {
    if (Length == 1)
    throw new java.util.NoSuchElementException();
    else if (Length == 0)
    throw new java.lang.NullPointerException();
    else {
    Node temp = cursor();
    Pointer = temp;
    if (temp != Tail)
    return (temp.next.data);
    else
    throw new java.util.NoSuchElementException();
    }
    } // 返回当前结点的值
    public Object currentNode() {
    Node temp = cursor();
    return temp.data;
    } // 在当前结点前插入一个结点,并使其成为当前结点
    public void insert(Object obj) {
    Node e = new Node(obj);
    if (Length == 0) {
    Tail = e;
    Head = e;
    } else {
    Node temp = cursor();
    e.next = temp;
    if (Pointer == null)
    Head = e;
    else
    Pointer.next = e;
    }
    Length++;
    } // 返回链表的大小
    public int size() {
    return Length;
    } // 将当前结点移出链表,下一个结点成为当前结点,
    // 如果移出的结点是最后一个结点,
    // 则第一个结点成为当前结点
    public Object remove() {
    Object temp;
    if (Length == 0)
    throw new java.util.NoSuchElementException();
    else if (Length == 1) {
    temp = Head.data;
    deleteAll();
    } else {
    Node cur = cursor();
    temp = cur.data;
    if (cur == Head)
    Head = cur.next;
    else if (cur == Tail) {
    Pointer.next = null;
    reset();
    } else
    Pointer.next = cur.next;
    Length--;
    }
    return temp;
    } // 返回当前结点的指针
    private Node cursor() {
    if (Head == null)
    throw new java.lang.NullPointerException();
    else if (Pointer == null)
    return Head;
    else
    return Pointer.next;
    } // 链表的简单应用举例
    public static void main(String[] args) {
    LinkList a = new LinkList();
    for (int i = 1; i <= 10; i++)
    a.insert(new Integer(i));
    System.out.println("The currentNode is:" + a.currentNode());
    while (!a.isEnd())
    System.out.println("The nextNode is:" + a.nextNode());
    a.reset();
    while (!a.isEnd()) {
    a.remove();
    }
    a.remove();
    a.reset();
    if (a.isEmpty()) {
    System.out.println("There is no Node in List");
    }
    }
    }