자바 단일 체인 표 반전 을 실현 하 는 여러 가지 방법 총화

싱글 체인 시계 에 익숙 하지 않 은 것 은 볼 수 있 습 니 다자바 기반 단일 체인 시트 의 추가 삭제 검 사 를 실현 합 니 다.
제자리 반전
1.보초병 노드 를 새로 만 들 고 다음 노드 는 머리 노드 를 가리킨다.
2.반전 대기 링크 의 다음 노드 를 보초병 노드 의 다음 노드 에 삽입
이전 링크 반전:1C>2C>3C>4>C>5
보초병 가입 노드:dummpC>1C>2C>3C>4>C>5
제자리 반전:
정의:prev=dummp.next;pcur=prev.next;
prev.next=pcur.next;
pcur.next=dummp.next;
dummp.next=pcur;
pcur=prev.next;


public Stu_node reverse_list(Stu_node head){
        if (head.next==null ||head.next.next==null)
            return null;
        Stu_node dump = new Stu_node(-1," ");
        dump.next=head;
        Stu_node prev = dump.next;
        Stu_node pcur = prev.next;
        while(pcur!=null){
            prev.next=pcur.next;
            pcur.next=dump.next;
            dump.next=pcur;
            pcur=prev.next;
        }
        return dump.next;
    }
2.새 체인 헤더 노드 삽입 법
2.새 체인 헤더 노드 삽입 방법:
새 헤드 노드 를 만 들 고 원본 링크 를 옮 겨 다 니 며 각 노드 를 헤드 노드 로 새 링크 에 삽입 합 니 다.마지막 으로 새로 만 든 링크 는 반전 후의 링크 이다.


public Stu_node reverse_list1 (Stu_node head){
        //            
        Stu_node dump = new Stu_node(-1," ");
        Stu_node pcur = head;
        //       ,           
        while(pcur!=null){
            Stu_node pnext = pcur.next;
            pcur.next = dump.next;
            dump.next=pcur;
            pcur=pnext;
        }
        //               ,            
        return dump.next;
    }
3.스 택 구 조 를 이용 하여 링크 의 반전 을 실현 한다.
스 택 구조 저장 데 이 터 는 선진 적 인 후 출(후진 선 출)이기 때문에 스 택 을 통 해 반전 링크 의 목적 을 달성 할 수 있 습 니 다.

 public Stu_node reverse_stack(Stu_node head){
        Stack<Stu_node> stack = new Stack<>();
        Stu_node temp = head;
        //    
        while(temp!=null){
            stack.push(temp);
            temp=temp.next;
        }
        //                   
        Stu_node new_head = stack.pop();
        Stu_node cur = new_head;
        //  
        while(!stack.isEmpty()){
            Stu_node node = stack.pop();
            //          
            node.next=null;
            //        
            cur.next = node;
            cur = node;
        }
        return new_head;
    }
4.전체 코드 를 드 립 니 다.

import java.util.Stack;

public class revere_node {
    public static void main(String[] args) {
        LinkedNode list= new LinkedNode();
        Stu_node node1 = new Stu_node(1,"  ");
        Stu_node node2 = new Stu_node(2,"  ");
        Stu_node node3 = new Stu_node(3,"  ");
        Stu_node node4 = new Stu_node(4,"  ");
        Stu_node node5 = new Stu_node(5,"  ");
        //           
        list.print();
        //       
        list.add(node1);
        list.add(node2);
        list.add(node3);
        list.add(node4);
        list.add(node5);
        //           
        list.print();
        System.out.println("-------------------");
        //                   
        Stu_node head = list.reverse_stack(list.head);
        //        
        while (head.next!=null){
            System.out.println(head);
            head=head.next;
        }

    }
}
//          
class LinkedNode{
    //       
    Stu_node head = new Stu_node(-1," ");
    //       
    public void add(Stu_node node){
        Stu_node temp = head;
        while(true){
            if (temp.next==null)
                break;
            temp=temp.next;
        }
        temp.next=node;
    }
    //    
    public void print(){
        Stu_node temp = head.next;
        if (head.next==null){
            System.out.println("     ");
        }
        while (temp!=null){
            System.out.println(temp);
            temp=temp.next;
        }
    }
    //    
    public Stu_node reverse_list(Stu_node head){
        if (head.next==null ||head.next.next==null)
            return null;
        Stu_node dump = new Stu_node(-1," ");
        dump.next=head;
        Stu_node prev = dump.next;
        Stu_node pcur = prev.next;
        while(pcur!=null){
            prev.next=pcur.next;
            pcur.next=dump.next;
            dump.next=pcur;
            pcur=prev.next;
        }
        return dump.next;
    }
    //        ,             
    public Stu_node reverse_list1 (Stu_node head){
        Stu_node dump = new Stu_node(-1," ");
        Stu_node pcur = head;
        while(pcur!=null){
            Stu_node pnext = pcur.next;
            pcur.next = dump.next;
            dump.next=pcur;
            pcur=pnext;
        }
        return dump.next;
    }
    //         
    public Stu_node reverse_stack(Stu_node head){
        Stack<Stu_node> stack = new Stack<>();
        Stu_node temp = head;
        //    
        while(temp!=null){
            stack.push(temp);
            temp=temp.next;
        }
        //                
        Stu_node new_head = stack.pop();
        Stu_node cur = new_head;
        //  
        while(!stack.isEmpty()){
            Stu_node node = stack.pop();
            //          
            node.next=null;
            //        
            cur.next = node;
            cur = node;
        }
        return new_head;
    }
}
//   
class Stu_node{
    int num;
    String name;
    Stu_node next;
    //  toString  ,      
    @Override
    public String toString() {
        return "Stu_node{" +
                "num=" + num +
                ", name='" + name + '\'' +
                '}';
    }

    public Stu_node(int num, String name) {
        this.num = num;
        this.name = name;
    }
}
총결산
자바 가 단일 체인 표 반전 을 실현 하 는 여러 가지 방법 에 관 한 글 은 여기까지 소개 되 었 습 니 다.더 많은 자바 단일 체인 표 반전 방법 내용 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 조회 하 시기 바 랍 니 다.앞으로 많은 응원 바 랍 니 다!

좋은 웹페이지 즐겨찾기