Cov txheej txheem:

Kuv yuav txheeb cov npe uas txuas ib leeg li cas?
Kuv yuav txheeb cov npe uas txuas ib leeg li cas?

Video: Kuv yuav txheeb cov npe uas txuas ib leeg li cas?

Video: Kuv yuav txheeb cov npe uas txuas ib leeg li cas?
Video: Saib Mis paub Poj niam (hluas nkauj) tus yam ntxwv lub siab 2024, Tej zaum
Anonim

Hauv qab no yog qhov yooj yim ntxig xaiv algorithm rau linked lis . 1) Tsim ib qho khoob txheeb (los yog tshwm sim) lis 2) Hla qhov muab lis , ua raws li rau txhua qhov node. a) Ntxig tam sim no rau hauv txheeb txoj hauv kev txheeb los yog tshwm sim lis . 3) Hloov lub taub hau ntawm muab linked lis mus taub hau txheeb (los yog tshwm sim) lis.

Hais txog qhov no, koj tuaj yeem txheeb cov npe txuas?

Ua ke xaiv feem ntau nyiam rau sorting ib daim ntawv txuas . Kev ua haujlwm qeeb random-access ntawm a linked lis ua rau qee qhov lwm cov algorithms (xws li quicksort) ua tsis zoo, thiab lwm tus (xws li heapsort) ua tsis tau kiag li. Cia lub taub hau yog thawj node ntawm lub linked lis yuav tsum tau txheeb thiab headRef yog tus taw tes rau lub taub hau.

Qhov thib ob, ua li cas koj txheeb cov npe txuas nrog kev sib tw npuas? Txhawm rau ua npuas, peb ua raws cov kauj ruam hauv qab no:

  1. Kauj Ruam 1: Xyuas seb cov ntaub ntawv ntawm 2 qhov nyob ib sab yog nyob rau hauv qhov kev txiav txim nce los yog tsis. Yog tias tsis yog, sib pauv cov ntaub ntawv ntawm 2 qhov chaw nyob ib sab.
  2. Kauj Ruam 2: Thaum kawg ntawm kev hla 1, cov khoom loj tshaj plaws yuav yog qhov kawg ntawm daim ntawv.
  3. Kauj ruam 3: Peb txiav lub voj, thaum tag nrho cov ntsiab lus pib.

Yog li ntawd, koj yuav txheeb cov ntaub ntawv li cas hauv cov npe txuas?

Algorithm

  1. Tsim ib chav kawm Node uas muaj ob tus cwj pwm: cov ntaub ntawv thiab tom ntej.
  2. Tsim lwm chav kawm SortList uas muaj ob tus cwj pwm: lub taub hau thiab tus Tsov tus tw.
  3. addNode() yuav ntxiv cov node tshiab rau cov npe:
  4. sortList() yuav txheeb cov nodes ntawm cov npe nyob rau hauv ascending order.
  5. display() yuav tso saib cov nodes tam sim no nyob rau hauv daim ntawv:

Kuv yuav txheeb cov npe sib txuas li cas ntawm cov tsiaj ntawv?

Kev txheeb ib txoj hlua LinkedList hauv Java yog ib qho yooj yim. Koj ua tau xaiv txoj hlua LinkedList hauv ascending tus tsiaj ntawv xaj los ntawm kev siv xaiv ( Sau npe ) Koj tuaj yeem ua tau xaiv txoj hlua LinkedList hauv nqis tus tsiaj ntawv xaj los ntawm kev siv xaiv ( Sau npe , Sib piv c).

Pom zoo: