1 / 6

Inserarea unui nou element in lista

Inserarea unui nou element in lista. Inserarea unui nou element in lista. * Inaintea nodului cu valoare x * Dupa nodul cu valoare x * Pe pozitia k. Inserarea unui element inaintea primului nod. Pasii : Se aloca memorie nodului Se scrie informatia in nodul nou

britain
Download Presentation

Inserarea unui nou element in lista

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Inserareaunuinou element in lista

  2. Inserareaunuinou element in lista *Inainteanodului cu valoare x * Dupanodul cu valoare x * Pepozitia k

  3. Inserareaunui element inainteaprimului nod • Pasii: • Se alocamemorienodului • Se scrieinformatia in nodulnou • Nodulnou se leaga de nodul prim • Nodulnouinseratdevine prim • void inserare() • { • nod *p, *nou; • inty; • cout<<“y=“; cin>>y; • if (prim->info==x) • {nou=new nod; • nou->info=y; • nou->urm=prim; • prim=nou;} • } Y prim nou prim prim

  4. Inserareainainteanodului cu valoare x • Pasii: • Se alocamemorienodului • Completaminformatia in nod • Noul nod se leaga de nodul p • Succesorullui p devinenoul nod • void inserare() • {nod *p, *nou; • int y; cin>>y; • p=prim; • while (p->urm->info!=x) • p=p->urm; • nou=new nod; • nou->info=y; • p->urm=nou->urm; • p->urm=nou;} p->info p->urm Y nou p

  5. Inserareadupanodul cu valoare x • Pasii: • Se alocamemorienodului • Se scrieinformatia in nod • Nodulnou se leaga de succesorulnodului q • Nodul q se leaga de nodulnou • Dacanodul p a fostultimul nod al listeiatuncinoudevineultim • void inserare() • {nod *p, *nou; • int y; cout<<“y=“; cin>>y; q=prim; • while (q->info!=x && q) q=q->urm; • nou=new nod; • nou->info=y; • nou->urm=q->urm; • q->urm=nou; • if (q==ultim) • ultim=nou; } q->urm nou Y q->urm q nou

  6. Inserareaunui element pepozitia K • Pasii: • Daca k=1, se alocamemorienoduluinou • Completamnodul cu informatie • Succesorulnoului nod esteprimul nod • Noul nod devineprimul nod al listei • Daca K estediferit de 1, parcurgemlista cu ajutorullui p • Alocamspatiu de memorienoului nod • Completamnodul cu informatie • Legamnoul nod de succesorullui p • Succesorullui p vafinoul nod • void inserare (int k, int y) • {if (k==1) • {nou=new nod; • nou->info=y; • nou->urm=prim; • prim=nou;} • else • {p=prim; • for (inti=1; i<k-1; i++) • p=p->urm; • nou=new nod; • nou->info=y; • nou->urm=p->urm; • p->urm=nou;}} Y Y nou prim K prim p nou

More Related