Jump to content
adrianisv

[cpp] Problema liste simplu inlantuite

Recommended Posts

Posted

Va salut,

Avem o lista cu n numere naturale. Lista trebuie impartita in doua liste, una cu numere pare si cealalta cu numere impare.

Ce am facut pana acum:



#include <iostream>
#include <fstream>
using namespace std;

fstream f("nr.in");

struct nod {
int info;
nod *urm;
};

nod *p, *u, *pare, *impare;

void adaug(nod *&p, nod *&u, int &val) {
nod *nou;
if(p==NULL) {
p=new nod;
p->info=val;
p->urm=NULL;
u=p;
}

else {
nou=new nod;
nou->info=val;
nou->urm=NULL;
u->urm=nou;
u=nou;
}
}

void creare(nod *&p, nod *&u, int n) {
int i, val;
cin>>n;
for(i=0;i<n;i++) {
cin>>val;
adaug(p, u, val);
}
}

void adaugp(nod *&p, int val) {
nod *nou;
nou=new nod;
nou->info=val;
nou->urm=p;
p=nou;
}

void separ(nod *p, nod *&pare, nod *&impare) {
nod *c=p;
while(c) {
if(c->info%2==0)
adaugp(pare,p->info);
else
adaugp(impare,p->info);
p=p->urm;
}
}

void afisare(nod *p) {
nod *c=p;
while(c!=NULL){
cout<<c->info<<" ";
c=c->urm;
}
cout<<endl;
}

int main() {
adaug(p,u,val);
creare(p,u,n);
separ(p,pare,impare);
afisare(pare);
afisare(impare);
return 0;
}

Idei? :)

  • Active Members
Posted

Asta ar fi o varianta:


void split(ListNode *head,ListNode **ptrOddList,ListNode **ptrEvenList){
int remainder;
int countO=0;
int countE=0;
ListNode *tempO,*tempE;

if (head==NULL)
return;
else{
(*ptrOddList)=(struct node*)malloc(sizeof(ListNode));
(*ptrEvenList)=(struct node*)malloc(sizeof(ListNode));

while(head!=NULL){
remainder=head->item%2;

if(remainder==0){
if(countE>0){
tempE->next=head;
tempE=tempE->next;
}
else
tempE=*ptrOddList;
tempE->item=head->item;
countE++;

}
else{
if(countO>0){
tempO->next=head;
tempO=tempO->next;
}
else
tempO=*ptrOddList;
tempO->item=head->item;
countO++;

}
head=head->next;
}
tempE->next=NULL;
tempO->next=NULL;
printf("Lista para:");
printNode((*ptrEvenList));
printf("Lista impara:");
printNode((*ptrOddList));
}
}


In loc de printf, pui tu acolo cout<<"Lista para:"; si la fel si pentru cea impara :)

Posted

Mersi fain, mi-a iesit.

#include <iostream>
using namespace std;


struct nod {
int info;
nod *urm;
};

nod *p, *u, *pare, *impare, *pareu, *impareu;

void adaug(nod *&p, nod *&u, int &val) {
nod *nou;
if(p==NULL) {
p=new nod;
p->info=val;
p->urm=NULL;
u=p;
}

else {
nou=new nod;
nou->info=val;
nou->urm=NULL;
u->urm=nou;
u=nou;
}
}

void creare(nod *&p, nod *&u, int n) {
int i, val;
cin>>n;
for(i=0;i<n;i++) {
cin>>val;
adaug(p, u, val);
}
}

void adaugp(nod *&p, nod *&u, int val) {
nod *nou;
nou=new nod;
nou->info=val;
nou->urm=p;
p=nou;
}

void separ(nod *p, nod *&pare, nod *&impare) {
nod *c=p;
while(c!=NULL) { // pana cand c != NULL (adica pana la sfarasitul listei)
if(c->info%2==0) //daca elementul este par
adaugp(pare,pareu,p->info); // il adaugi in lista para
else
adaugp(impare,impareu,p->info); //daca nu in lista impara
c=c->urm;//merga la urmatorul elemnt
}
}

void afisare(nod *p) {
nod *c=p;
while(c!=NULL){
cout<<c->info<<" ";
c=c->urm;
}
cout<<endl;
}

int main() {
nod *p, *u, *pare, *impare;
int val, n;
adaug(p,u,val);
creare(p,u,n);
separ(p,pare,impare);
afisare(pare);
afisare(impare);
return 0;
}

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...