Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<conio.h>
- #include<math.h>
- #include<iostream>
- using namespace std;
- class Lista {
- private:
- struct Nod {
- int info;
- struct Nod *next;
- };
- Nod* prim;
- public:
- Lista();
- void adaugare(int x);
- void afisare();
- void suma();
- void stergerePare();
- void inversareLista();
- void bubble();
- void swap(Nod *a, Nod *b){
- int temp=a->info;
- a->info=b->info;
- b->info=temp;
- }
- };
- Lista::Lista() {
- prim=NULL;
- }
- void Lista:: adaugare(int x) {//la coada
- Nod * v;
- v=new Nod;
- v->info=x;
- v->next=NULL;
- if(prim==NULL) {
- prim=v;
- } else {
- Nod* temp=prim;
- while(temp->next!=NULL) {
- temp=temp->next;
- }
- temp->next=v;
- }
- }
- void Lista::afisare() {
- Nod *temp=prim;
- while(temp!=NULL) {
- cout<<temp->info<<" ";
- temp=temp->next;
- }
- cout<<endl;
- }
- void Lista::suma() {
- if(prim!=NULL && prim->next!=NULL) {
- Nod * q,*p;//parcurge lista
- q=prim;
- p=q->next;
- while(p!=NULL) {
- Nod *temp;// in el se calculeaza suma a 2 elem consecutive;
- temp=new Nod;
- temp->info=p->info + q->info;
- temp->next=p;
- q->next=temp;
- q=p;
- p=p->next;
- }
- }
- }
- void Lista::stergerePare() {
- while(prim!=NULL && prim->info%2==0) {
- Nod *temp=prim;
- prim=prim->next;
- delete temp;
- }
- if(prim!=NULL) {
- Nod*p=prim;
- Nod*q=prim->next;
- while(q!=NULL ) {
- if(q->info%2==0) {
- p->next=q->next;
- delete q;
- q=p->next;
- } else {
- p=q;
- q=q->next;
- }
- }
- }
- }
- void Lista::inversareLista() {
- Nod *r, *p, *q;
- r=NULL;
- p=prim;
- q=NULL;
- while(p!=NULL) {
- q=p->next;
- p->next=r;
- r=p;
- p=q;
- }
- prim=r;
- }
- void Lista::bubble(){
- int ok=0;
- Nod *p, *q;
- if(prim==NULL){
- return;
- }else{
- do{
- ok=0;
- p=prim;
- while(p->next!=NULL){
- q=p->next;
- if(p->info > q->info){
- swap(p,q);
- ok=1;
- }
- p=p->next;
- }
- }while(ok);
- }
- }
- int main() {
- int n=0;
- Lista pare, impare;
- cout<<"dati n= ";
- cin>>n;
- while(n!=-1) {
- /* if(n%2==0) {
- pare.adaugare(n);
- } else {
- impare.adaugare(n);
- }*/
- impare.adaugare(n);
- cout<<"dati n= ";
- cin>>n;
- };
- /*cout<<"Lista pare ";
- pare.afisare();*/
- cout<<"Lista: ";
- impare.afisare();
- //cout<<" Elementele inserate sunt suma a 2 consecutive"<< endl;
- //impare.suma();
- //impare.afisare();
- //cout<<"stergere pare ";
- //impare.stergerePare();
- //impare.afisare();
- // cout<<"inversare lista ";
- //impare.inversareLista();
- //impare.afisare();
- cout<<"Bubble sort: ";
- impare.bubble();
- impare.afisare();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement