Merge two sorted Single-Chain tables

Source: Internet
Author: User

// The structure of the linked list class using list {int data; using list next ;} /***** merge two sorted linked lists **/public class UnionLinkList {/***** create a single-chain table * @ param head single-chain header node */public void createLinkedList (sorted list head) {empty list cur = head; empty tables = new tables (System. in); String data; System. out. println ("Enter the value of the single-link table node, input # End"); while (true) {data = complete. next (); if (data. equals ("#") {cur. next = null; break;} vertex list node = new Vertex list (); node. data = Integer. valueOf (data); cur. next = node; cur = cur. next ;}} /***** @ param a linked list a * @ param B linked list B * @ param union merged linked list * @ return merged linked list */public outer list union (except list, except List B, except list union) {except list pc = union = a; except list pa =. next; sort list pb = B. next; while (pa! = Null & pb! = Null) {if (pa. data <pb. data) {pc. next = pa; pc = pa; pa = pa. next;} else {pc. next = pb; pc = pb; pb = pb. next;} if (pa! = Null) {pc. next = pa;} if (pb! = Null) {pc. next = pb;} return union;}/*** print a single-chain table * @ param head pointer */public void printLinkList (empty list head) {if (head = null | head. next = null) {return;} head = head. next; while (head! = Null) {System. out. print (head. data + ""); head = head. next;} System. out. println ();} public static void main (String [] args) {UnionLinkList ull = new UnionLinkList (); physical list pa = new physical list (); physical list pb = new physical list (); ull. createLinkedList (pa); ull. createLinkedList (pb); distinct list union = null; union = ull. union (pa, pb, union); ull. printLinkList (union );}}

 

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.