Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class BeadEx {
- public static Bead PutIn(Bead b1, Bead bt){
- boolean bool=false;
- Bead b=b1;
- if(bt.getNum()<b1.getNum()){
- bt.setNext(b1);
- return bt;
- }
- else {
- while (b!=null && bool==false){
- if(b.getNext()==null){
- if (bt.getNum()>b.getNum())
- b.setNext(bt);
- }
- else {
- if (bt.getNum()<b.getNext().getNum()){
- bt.setNext(b.getNext());
- b.setNext(bt);
- bool=true;
- }
- }
- b=b.getNext();
- }
- }
- return b1;
- }
- public static void PrintBead(Bead b){
- Bead b1=b;
- while (b1!=null){
- System.out.println(b1.getNum()+",");
- b1=b1.getNext();
- }
- }
- public static void main(String[]args){
- Bead b3= new Bead(2, new Bead(3,new Bead(5, new Bead(7,new Bead(9)))));
- Bead b=new Bead(1);
- PrintBead(b3);
- PutIn(b3,b);
- PrintBead(b3);
- }
- }
- class Bead{
- private int num;
- private Bead next;
- public Bead (int num){
- this.num=num;
- }
- public Bead (int num,Bead next){
- this.num=num;
- this.next=next;
- }
- int getNum(){
- return num;
- }
- Bead getNext(){
- return next;
- }
- void setNext(Bead b){
- this.next=b;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement