第1题:我给你搭建算法框架,具体需求,你只需往里面写Code即可:public class Program {private static final int N=6;public static void main(String[] args) {Node head=new Node(-1,null); // 定义头指针,带头结点的单链表 for(int i=0;i
第1题:我给你搭建算法框架,具体需求,你只需往里面写Code即可:
public class Program {private static final int N=6;public static void main(String[] args) {Node head=new Node(-1,null); // 定义头指针,带头结点的单链表 for(int i=0;i<N;i++){ Node e=new Node(i+1,null); tailInsert(head, e); } // Test Node p=head; while(p.getNext()!=null){ p=p.getNext(); }}/** * @param head 实施尾插法算法的单链表头指针 * @param e 所需的元素 */private static void tailInsert(Node head,Node e){Node p=head;while(p.getNext()!=null){p=p.getNext();// 寻访单链表,直至到达单链表末尾}// 实施尾插法p.setNext(e);}}class Node{private int id; // 编号private Node next; // 单链表后继指针private String vote;// 选票 public Node(){}public Node(int id, Node next) {super();this.id = id;this.next = next;}public Node(int id, Node next, String vote) {super();this.id = id;this.next = next;this.vote = vote;}@Overridepublic String toString() {return "Node [id=" + id + ", next=" + next + "]";}public int getId() {return id;}public void setId(int id) {this.id = id;}public Node getNext() {return next;}public void setNext(Node next) {this.next = next;}}第2题:参看我以前的回答:
https://zhidao.baidu.com/question/431512924412893084算法思想已经写的清楚得不能在清楚了。转成Java就是小菜一碟。
2017-03-20