Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. import java.util.*;
  2. public class BeadEx {
  3. public static Bead PutIn(Bead b1, Bead bt){
  4.  
  5. boolean bool=false;
  6. Bead b=b1;
  7. if(bt.getNum()<b1.getNum()){
  8. bt.setNext(b1);
  9. return bt;
  10. }
  11.  
  12. else {
  13. while (b!=null && bool==false){
  14.  
  15. if(b.getNext()==null){
  16. if (bt.getNum()>b.getNum())
  17. b.setNext(bt);
  18. }
  19. else {
  20. if (bt.getNum()<b.getNext().getNum()){
  21. bt.setNext(b.getNext());
  22. b.setNext(bt);
  23. bool=true;
  24. }
  25. }
  26. b=b.getNext();
  27. }
  28. }
  29. return b1;
  30. }
  31. public static void PrintBead(Bead b){
  32. Bead b1=b;
  33. while (b1!=null){
  34. System.out.println(b1.getNum()+",");
  35. b1=b1.getNext();
  36. }
  37. }
  38. public static void main(String[]args){
  39.  
  40. Bead b3= new Bead(2, new Bead(3,new Bead(5, new Bead(7,new Bead(9)))));
  41. Bead b=new Bead(1);
  42. PrintBead(b3);
  43. PutIn(b3,b);
  44. PrintBead(b3);
  45. }
  46. }
  47.  
  48. class Bead{
  49.  
  50. private int num;
  51. private Bead next;
  52.  
  53. public Bead (int num){
  54. this.num=num;
  55. }
  56.  
  57. public Bead (int num,Bead next){
  58. this.num=num;
  59. this.next=next;
  60. }
  61.  
  62. int getNum(){
  63. return num;
  64. }
  65.  
  66. Bead getNext(){
  67. return next;
  68. }
  69. void setNext(Bead b){
  70. this.next=b;
  71. }
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement