用java實現的雙向鏈表增加刪除修改操作

來源:互聯網
上載者:User
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Scanner;class Dnode{public int score=-1;public Dnode llink;public Dnode rlink;}class DoubleList{static Dnode pre,ptr,head,current;DoubleList(){ptr=new Dnode();ptr.llink=ptr;ptr.rlink=ptr;head=ptr;}public static void insert(Dnode ptr){Dnode first=head.rlink;ptr.rlink=head.rlink;ptr.llink=head;head.rlink=ptr;first.llink=ptr;} public static void delete(Dnode ptr){ptr.llink.rlink=ptr.rlink;ptr.rlink.llink=ptr.llink;ptr=null;}public static void modify(Dnode ptr,int m){ptr.score=m;}public static int length(){current=head.rlink;int count=0;while(current.rlink!=head){current=current.rlink;count++;}return count;}}public class Main {public static void main(String[] args) throws IOException {DoubleList doublist=new DoubleList();printDoublelist(doublist);//輸出當前雙向鏈表Scanner sc=new Scanner(new BufferedReader(new InputStreamReader(System.in)));while(true){System.out.println("1.插入 2.刪除  3.修改");System.out.println("請輸入要選擇的操作序號:");int select=sc.nextInt();if(select==1){insertopp(doublist);}if(select==2){deleteopp(doublist);}if(select==3){modifyopp(doublist);}System.out.println();} }public static void insertopp(DoubleList doublist){//插入操作System.out.println("請輸入待插入節點:");Scanner sc=new Scanner(new BufferedReader(new InputStreamReader(System.in)));int a=sc.nextInt();while(a!=0){  Dnode node=new Dnode();node.score=a;doublist.insert(node);a=sc.nextInt();}printDoublelist(doublist);}public static void deleteopp(DoubleList doublist){//刪除操作System.out.println("請輸入要刪除節點序號:");Scanner sc=new Scanner(new BufferedReader(new InputStreamReader(System.in)));int count=sc.nextInt();Dnode current=doublist.head;for(int i=1;i<=count;i++){  current=current.rlink;}doublist.delete(current);printDoublelist(doublist);}public static void modifyopp(DoubleList doublist){//修改操作System.out.println("請輸入要修改節點序號:");Scanner sc=new Scanner(new BufferedReader(new InputStreamReader(System.in)));int count=sc.nextInt();Dnode current=doublist.head;for(int i=1;i<=count;i++){  current=current.rlink;}System.out.println("請輸入要修改的值:");int n=sc.nextInt();doublist.modify(current,n);printDoublelist(doublist);}public static void printDoublelist(DoubleList doublist){System.out.println("雙向鏈表:");System.out.print("head"+"<->");   if(doublist.head.rlink!=doublist.head)   {Dnode current=doublist.head.rlink; int count=0;while(current.rlink!=doublist.head){System.out.print(current.score+"<->");current=current.rlink;}System.out.print(current.score);   }   System.out.println();   } }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.