Add deletion and modification operations for a two-way linked list implemented in Java

Source: Internet
Author: User
Import Java. io. bufferedreader; import Java. io. ioexception; import Java. io. inputstreamreader; import Java. util. category; 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; H EAD. 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); // outputs the current bidirectional linked list; optional SC = new (New bufferedreader (New inputstreamreader (system. in); While (true) {system. out. println ("1. insert 2. delete 3. modify "); system. out. println ("Enter the operation number to be selected:"); 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) {// insert operation system. out. println ("Enter the node to be inserted:"); then SC = new second (New bufferedreader (New inputstreamreader (system. in); int A = SC. nextint (); While (! = 0) {dnode node = new dnode (); node. score = A; doublist. insert (node); A = SC. nextint ();} printdoublelist (doublist);} public static void deleteopp (doublelist doublist) {// Delete the system. out. println ("Enter the serial number of the node to be deleted:"); then SC = new second (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); P Rintdoublelist (doublist);} public static void modifyopp (doublelist doublist) {// modify the system. out. println ("Enter the serial number of the node to be modified:"); then SC = new second (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 ("Enter the value to be modified:"); int n = SC. nextint (); doublist. modify (current, n); printdoublelist (doublist );} Public static void printdoublelist (doublelist doublist) {system. out. println ("two-way linked list:"); 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 ();}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.