查看: 1548| 回复: 0
跳转到指定楼层
上一主题 下一主题
收起左侧

[CareerCup] Career Cup 2.4ListPartition我这个一运行Eclpse就崩溃了,咋回事啊

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
  1. package Chapter2;
  2. public class partition {

  3. public static void partition(MyLinkedList<Integer> list,int k){


  4. Node<Integer> BeforeStart=null;//左边是Node的引用,这个引用哪里也没指,指向null

  5. Node<Integer> BeforeEnd=null;
  6. Node<Integer> AfterStart=null;
  7. Node<Integer> AfterEnd=null;
  8. Node<Integer> Node=list.head;

  9. while(Node!=null){

  10. if(Node.data <k){
  11. if(BeforeStart==null){
  12. BeforeStart=Node;
  13. BeforeEnd=BeforeStart;

  14. }else{

  15. BeforeEnd.next=Node;
  16. BeforeEnd=Node;//BeforeEnd向前移动以便下一次遍历


  17. }
  18. }

  19. else{

  20. if(AfterStart==null){
  21. AfterStart=Node;
  22. AfterEnd=AfterStart;

  23. }else{

  24. AfterEnd.next=Node;
  25. AfterEnd=Node;//AfterEnd向前移动以便下一次遍历


  26. }

  27. }

  28. Node=Node.next;







  29. }

  30. BeforeEnd.next=AfterStart;}



  31. public static void main(String[] args) {
  32. MyLinkedList<Integer> list = new MyLinkedList<>(new Integer[] { 1, 2,
  33. 6,8,2,4,9,7});
  34. list.print();
  35. partition(list, 9);
  36. list.print();
  37. }

  38. }





复制代码
Node.java
  1. package Chapter2;

  2. public class Node<T> {  //T可以是int,string等
  3. public T data;
  4. public Node<T> next;
  5. public Node(T d, Node<T> n){
  6. data = d;
  7. next = n;
  8. }
  9. }
复制代码
MyLikedList.JAVA
  1. package Chapter2;

  2. //Singly Linked List
  3. public class MyLinkedList<T> {
  4. public Node<T> head;

  5. public MyLinkedList(Node<T> h) {
  6. head = h;
  7. }

  8. public MyLinkedList(T[] dataArray) {
  9. if (dataArray == null || dataArray.length <= 0)
  10. return;
  11. head = new Node<>(dataArray[0], null);
  12. Node<T> node = head;//node指向head
  13. for (int i = 1; i < dataArray.length; i++) {
  14. node.next = new Node<T>(dataArray[i], null);
  15. node = node.next;
  16. }
  17. }

  18. public void print() {
  19. Node<T> cur = head;
  20. while (cur != null) {
  21. System.out.print(cur.data);
  22. if (cur.next != null) {
  23. System.out.print(" -> ");
  24. }
  25. cur = cur.next;
  26. }
  27. System.out.println();
  28. }
  29. }
复制代码

上一篇:小白求助一个简单的C语言程序
下一篇:刷题好累怎么办
您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表