0% found this document useful (0 votes)
5 views

Infocplusplus

The document discusses different sorting and linking algorithms including insertion sorting, merging two sorted lists, and reversing the order of nodes in a linked list. Functions are defined to create lists, add nodes, display lists, perform the sorting and linking operations.

Uploaded by

DxxM
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Infocplusplus

The document discusses different sorting and linking algorithms including insertion sorting, merging two sorted lists, and reversing the order of nodes in a linked list. Functions are defined to create lists, add nodes, display lists, perform the sorting and linking operations.

Uploaded by

DxxM
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Interclasare :

#include<iostream>

struct nod
{
int x;
struct nod* next,*p,*c,*u;
}
int n,i;

void creare()
{
cout<<"Dati nr de inregistrari=";
cin>>n;
cin>>c->x;
c->next=0;
p=c;
u=c;
for(i=2; i<=n; i++)
{
cin>>c->x;
c->next=0;
u->next=c;
u=c;
}
}

void listare()
{
c=p;
while(c)
{
cout<<c->x<<" ";
c=c->next;
}
}

void interclasare()
{
struct nod *c1,*c2,*c3;
c1=p1;
c2=p2;
if(c1->x<c2->x)
{
c3->x=c1->x;
c1=c1->next;
}
else
{
c3->x=c2->x;
c2=c2->next;
}
c3->next=0;
p3=c3;
u3=c3;
while(c1&&c2)
if(c1->x<c2->x)
{
c3->x=c1->x;
c3->next=0;
u3->next=c3;
u3=c3;
c1=c1->next;
}
else
{
c3->x=c2->x;
c3->next=0;
u3->next=c3;
u3=c3;
c2=c2->next;
}

if(!c1)
while(c2)
{
c3->x=c2->x;
c3->next=0;
u3->next=c3;
u3=c3;
c2=c2->next;
}
if(!c2)
while(c1)
{
c3->x=c1->x;
c3->next=0;
u3->next=c3;
u3=c3;
c1=c1->next;
}
c3=p3;
while(c3)
{
cout<<c3->x<<" ";
c3=c3->next;
}

void main()
{
creare();
listare();
interclasare();
listare();
return 0;
}
Sortare insertie:
#include <iostream>

using namespace std;

struct nod {
int val;
struct nod* next;
};

void insert(struct nod** p, int nr) {


nod* nou = new nod;
nou->val = nr;
nou->next = NULL;

if (*p == NULL || (*p)->val >= val) {


nou->next = *p;
*p = nou;
} else {
nod* curent = *p;
while (curent->next != NULL && curent->next->val < nr ) {
curent = curent->next;
}

nou->next = curent->next;
curent->next = nou;
}
}

void afisareLista(struct nod* p) {


struct nod* temp = p;
while (temp != NULL) {
cout<<temp->val<<" ";
temp = temp->next;
}
cout << endl;
}

void sortarePrinInsertie(struct nod** p) {


struct nod* listaSortata = NULL;
struct nod* curent = *p;

while (curent != NULL) {


struct nod* urmator = curent->next;

insert(&listaSortata, curent->val);

curent = urmator;
}

*p = listaSortata;
}

/// Exemplu de utilizare


int main() {
nod* p = NULL;
insert(&p, 5);
insert(&p, 3);
insert(&p, 8);
insert(&p, 1);
insert(&p, 2);
cout << "Lista initiala: ";
afisareLista(p);

sortarePrinInsertie(&p);

cout << "Lista sortata: ");


afisareLista(p);

return 0;
}

Inverse leg:

#include<fstream>

ifstream f("date.in");
ofstream g("date.out");

using namespace std;

struct nod
{
int info;
nod *leg;
};
nod *prim;

void adaugs(nod *&prim,int x)


{
if(prim)
{
nod *u=prim;
while(u->leg) u=u->leg;
nod *nou=new nod;
nou->info=x;
nou->leg=0;
u->leg=nou;
}
else
{
prim=new nod;
prim->info=x;
prim->leg=0;
}
}
void creare()
{
int x;
while(f>>x) adaugs(prim,x);
}

void afis(nod *prim)


{
nod *p;
p=prim;
while(p)
{
g<<p->info<<" ";
p=p->leg;
}
g<<endl;
}

void invers(nod *&prim)


{
nod *r,*p,*q;
r=prim;
p=prim->leg;
q=prim->leg->leg;
while(q)
{
p->leg-r;
r=p;
p=q;
q=q->leg;
}
prim=p;
}

void main()
{
creare();
afis(prim);
invers(prim);
afis(prim);
}

You might also like