Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Run {
- public static void main(String[] args) {
- Node<Integer> chain1 = buildChain1();
- Node<Integer> chain2 = buildChain2();
- Node<Integer> chain3 = buildChain3();
- printChain(chain1);
- Node<Integer> nd;
- while(chain1.hasNext()) {
- nd = deleteByValue(chain1.getNext(), chain1.getValue());
- nd = nd.getNext();
- printChain(chain1);
- chain1 = chain1.getNext();
- }
- //Node<Integer> nd = append_By_Value(nd5, node3);
- //Node<Integer> nd = returnLast(node3);
- }
- public static Node<Integer> buildChain1(){
- Node<Integer> nd1 = new Node<Integer>(50);
- Node<Integer> nd2 = new Node<Integer>(50, nd1);
- Node<Integer> nd3 = new Node<Integer>(12, nd2);
- Node<Integer> nd4 = new Node<Integer>(12, nd3);
- Node<Integer> nd5 = new Node<Integer>(12, nd4);
- return nd5;
- }
- public static Node<Integer> buildChain2(){
- Node<Integer> nd1 = new Node<Integer>(51);
- Node<Integer> nd2 = new Node<Integer>(50, nd1);
- Node<Integer> nd3 = new Node<Integer>(20, nd2);
- Node<Integer> nd4 = new Node<Integer>(15, nd3);
- Node<Integer> nd5 = new Node<Integer>(12, nd4);
- return nd5;
- }
- public static Node<Integer> buildChain3(){
- Node<Integer> node1 = new Node<Integer>(46);
- Node<Integer> node2 = new Node<Integer>(44, node1);
- Node<Integer> node3 = new Node<Integer>(42, node2);
- return node3;
- }
- //deletes the node with the given value in the chain
- public static Node<Integer> deleteByValue(Node<Integer> chain, int value){
- Node<Integer> tmp = chain.getNext();
- while(tmp.hasNext() && tmp!=null) {
- if(tmp.getNext().getValue() != value) {
- chain.setNext(tmp.getNext());
- chain = chain.getNext();
- tmp = tmp.getNext();
- }
- tmp = tmp.getNext();
- }
- if(tmp.getValue()!= value)
- chain.setNext(tmp);
- return chain;
- }
- //return the last node in given chain
- public static void printChain(Node<Integer> first){
- while(first.hasNext()) {
- System.out.print(first.getValue() + "->");
- first = first.getNext();
- }
- System.out.println(first.getValue() + "->null");
- }
- //return the last node in given chain
- public static Node<Integer> returnLast(Node<Integer> first){
- while(first.hasNext()) {
- System.out.print(first.getValue() + "->");
- first = first.getNext();
- }
- //prints last node
- System.out.println(first.getValue()+"->null"+"\n");
- return first;
- }
- public static Node<Integer> append_By_Value(Node<Integer> ndList1,
- Node<Integer> ndList2){
- Node<Integer> tmp1 = ndList1;
- Node<Integer> tmp2 = ndList2;
- int value1 = ndList1.getValue();
- int value2 = ndList2.getValue();
- //go to last node in the nodeList
- while(tmp2.hasNext()) {
- tmp2 = tmp2.getNext();
- //System.out.println(tmp2.print());
- }
- boolean search = true;
- while(tmp1.hasNext() && search) {
- if(value2 < tmp1.getNext().getValue()) {
- tmp2.setNext(tmp1.getNext());
- tmp1.setNext(ndList2);
- search = false;
- }
- tmp1 = tmp1.getNext();
- }
- if(search)
- tmp1.setNext(ndList2);
- return ndList1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement