Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <conio.h>
- #include <stdio.h>
- using namespace std;
- class Lista {
- private:
- struct nod {
- int info;
- nod *next;
- };
- nod *head, *last;
- public:
- Lista();
- void Append(int nr = 0);
- void Remove(int poz);
- // Lista& Remove(int poz);
- void DisplayList();
- ~Lista();
- };
- Lista::~Lista() {
- cout << "\nDestructor\n";
- }
- Lista::Lista() {
- this->head = NULL;
- //this->last = head;
- }
- void Lista::Append(int nr) {
- nod *temp = new nod;
- temp->info = nr;
- if (head) {
- last->next = temp;
- last = temp;
- last->next = NULL;
- }
- else {
- head = temp;
- head->next = NULL;
- last = head;
- }
- }
- void Lista::Remove(int nr) {
- for (;;)
- for (nod *temp = head; temp; temp = temp->next) {
- if (head->info == nr) {
- if (head->next != NULL) {
- nod * _temp = head;
- head = head->next;
- delete _temp;
- break;
- }
- else {
- nod *_temp = head;
- last = head = NULL;
- delete _temp;
- goto exit;
- }
- }
- if (temp->info == nr) {
- nod* _temp = head;
- while (_temp->next != temp) {
- _temp = _temp->next;
- }
- _temp->next = temp->next;
- if (temp == last) {
- _temp->next = last->next;
- last = _temp;
- }
- delete temp;
- break;
- }
- if (temp->info != nr && temp == last) {
- goto exit;
- }
- }
- exit:;
- }
- void Lista::DisplayList() {
- cout << endl;
- if (head) {
- for (nod *temp = head; temp; temp = temp->next) {
- cout << temp->info << " ,";
- }
- printf("%c%c%c", 8, 32, 8);
- }
- else {
- cout << "Lista este vida";
- }
- }
- void main() {
- Lista ceva;
- ceva.Append(10);
- //ceva.Append(5);
- ceva.Append(10);
- //ceva.Append(-1);
- ceva.Append(10);
- ceva.Append(10);
- //ceva.Append(11);
- ceva.Append(10);
- ceva.DisplayList();
- ceva.Remove(10);
- ceva.DisplayList();
- _getch();
- }
- // din 2,2,3,3,2 , scapi de "2" si ramane doar "3" adica 3,3
- //##############################################
- #include <iostream>
- #include <conio.h>
- #include <stdio.h>
- using namespace std;
- class Lista {
- private:
- struct nod {
- int info;
- nod *next;
- };
- nod *head, *last;
- public:
- Lista();
- void Append(int nr = 0);
- void Remove(int poz);
- void Insert();
- void DisplayList();
- ~Lista();
- };
- Lista::~Lista() {
- cout << "\nDestructor\n";
- }
- Lista::Lista() {
- this->head = NULL;
- //this->last = head;
- }
- void Lista::Append(int nr) {
- nod *temp = new nod;
- temp->info = nr;
- if (head) {
- last->next = temp;
- last = temp;
- last->next = NULL;
- }
- else {
- head = temp;
- head->next = NULL;
- last = head;
- }
- }
- void Lista::Remove(int nr) {
- for (;;)
- for (nod *temp = head; temp; temp = temp->next) {
- if (head->info == nr) {
- if (head->next != NULL) {
- nod * _temp = head;
- head = head->next;
- delete _temp;
- break;
- }
- else {
- nod *_temp = head;
- last = head = NULL;
- delete _temp;
- goto exit;
- }
- }
- if (temp->info == nr) {
- nod* _temp = head;
- while (_temp->next != temp) {
- _temp = _temp->next;
- }
- _temp->next = temp->next;
- if (temp == last) {
- _temp->next = last->next;
- last = _temp;
- }
- delete temp;
- break;
- }
- if (temp->info != nr && temp == last) {
- goto exit;
- }
- }
- exit:;
- }
- void Lista::DisplayList() {
- cout << endl;
- if (head) {
- for (nod *temp = head; temp; temp = temp->next) {
- cout << temp->info << " ,";
- }
- printf("%c%c%c", 8, 32, 8);
- }
- else {
- cout << "Lista este vida";
- }
- }
- void Lista::Insert() {
- if (head && head->next) {
- for (nod * temp = head; temp; temp = temp->next) {
- nod *_temp = head;
- }
- }
- }
- void main() {
- Lista cevapar,cevaimpar;
- int n;
- cout << "n = ";
- cin >> n;
- for (int i = 0; i < n; i++) {
- int nr;
- cin >> nr;
- if (nr % 2 == 0) {
- cevapar.Append(nr);
- }
- else {
- cevaimpar.Append(nr);
- }
- }
- cevapar.DisplayList();
- cevaimpar.DisplayList();
- _getch();
- }
- // din 2,2,3,3,2 , scapi de "2" si ramane doar "3" adica 3,3
- //#############################################################
- // adauga intre nod-uri un nou nod, si acel nou nod este suma intre nodul din urma lui si nodul urmator , ex: 1,2,3 => 1,3,5,3
- #include <iostream>
- #include <conio.h>
- #include <stdio.h>
- using namespace std;
- class Lista {
- private:
- struct nod {
- int info;
- nod *next;
- };
- nod *head, *last;
- public:
- Lista();
- void Append(int nr = 0);
- void Remove(int poz);
- void Insert();
- void DisplayList();
- ~Lista();
- };
- Lista::~Lista() {
- cout << "\nDestructor\n";
- }
- Lista::Lista() {
- this->head = NULL;
- //this->last = head;
- }
- void Lista::Append(int nr) {
- nod *temp = new nod;
- temp->info = nr;
- if (head) {
- last->next = temp;
- last = temp;
- last->next = NULL;
- }
- else {
- head = temp;
- head->next = NULL;
- last = head;
- }
- }
- void Lista::Remove(int nr) {
- for (;;)
- for (nod *temp = head; temp; temp = temp->next) {
- if (head->info == nr) {
- if (head->next != NULL) {
- nod * _temp = head;
- head = head->next;
- delete _temp;
- break;
- }
- else {
- nod *_temp = head;
- last = head = NULL;
- delete _temp;
- goto exit;
- }
- }
- if (temp->info == nr) {
- nod* _temp = head;
- while (_temp->next != temp) {
- _temp = _temp->next;
- }
- _temp->next = temp->next;
- if (temp == last) {
- _temp->next = last->next;
- last = _temp;
- }
- delete temp;
- break;
- }
- if (temp->info != nr && temp == last) {
- goto exit;
- }
- }
- exit:;
- }
- void Lista::DisplayList() {
- cout << endl;
- if (head) {
- for (nod *temp = head; temp; temp = temp->next) {
- cout << temp->info << " ,";
- }
- printf("%c%c%c", 8, 32, 8);
- }
- else {
- cout << "Lista este vida";
- }
- }
- void Lista::Insert() {
- if (head && head->next) {
- for (nod * temp = head; temp->next; temp = temp->next) {
- nod *noduletz = new nod;
- nod *_temp = temp->next;
- noduletz->info = temp->info + _temp->info;
- noduletz->next = temp->next;
- temp->next = noduletz;
- temp = temp->next;
- }
- }
- }
- void main() {
- Lista cevapar,cevaimpar,ceva;
- int n;
- /* cout << "n = ";
- cin >> n;
- for (int i = 0; i < n; i++) {
- int nr;
- cin >> nr;
- if (nr % 2 == 0) {
- cevapar.Append(nr);
- }
- else {
- cevaimpar.Append(nr);
- }
- }
- cevapar.DisplayList();
- cevaimpar.DisplayList();*/
- ceva.Append(1);
- ceva.Append(2);
- ceva.Append(3);
- ceva.Append(4);
- ceva.DisplayList();
- ceva.Insert();
- ceva.DisplayList();
- _getch();
- }
- // din 2,2,3,3,2 , scapi de "2" si ramane doar "3" adica 3,3
- //##################################################################
- //scoate toate nr pare din lista
- #include <iostream>
- #include <conio.h>
- #include <stdio.h>
- using namespace std;
- class Lista {
- private:
- struct nod {
- int info;
- nod *next;
- };
- nod *head, *last;
- public:
- Lista();
- void Append(int nr = 0);
- void Remove(int poz);
- void RemovePar();
- void Insert();
- void DisplayList();
- ~Lista();
- };
- Lista::~Lista() {
- cout << "\nDestructor\n";
- }
- Lista::Lista() {
- this->head = NULL;
- //this->last = head;
- }
- void Lista::Append(int nr) {
- nod *temp = new nod;
- temp->info = nr;
- if (head) {
- last->next = temp;
- last = temp;
- last->next = NULL;
- }
- else {
- head = temp;
- head->next = NULL;
- last = head;
- }
- }
- void Lista::Remove(int nr) {
- for (;;)
- for (nod *temp = head; temp; temp = temp->next) {
- if (head->info == nr) {
- if (head->next != NULL) {
- nod * _temp = head;
- head = head->next;
- delete _temp;
- break;
- }
- else {
- nod *_temp = head;
- last = head = NULL;
- delete _temp;
- goto exit;
- }
- }
- if (temp->info == nr) {
- nod* _temp = head;
- while (_temp->next != temp) {
- _temp = _temp->next;
- }
- _temp->next = temp->next;
- if (temp == last) {
- _temp->next = last->next;
- last = _temp;
- }
- delete temp;
- break;
- }
- if (temp->info != nr && temp == last) {
- goto exit;
- }
- }
- exit:;
- }
- void Lista::DisplayList() {
- cout << endl;
- if (head) {
- for (nod *temp = head; temp; temp = temp->next) {
- cout << temp->info << " ,";
- }
- printf("%c%c%c", 8, 32, 8);
- }
- else {
- cout << "Lista este vida";
- }
- }
- void Lista::Insert() {
- if (head && head->next) {
- for (nod * temp = head; temp->next; temp = temp->next) {
- nod *noduletz = new nod;
- nod *_temp = temp->next;
- noduletz->info = temp->info + _temp->info;
- noduletz->next = temp->next;
- temp->next = noduletz;
- temp = temp->next;
- }
- }
- }
- void Lista::RemovePar() {
- for (;;)
- for (nod *temp = head; temp; temp = temp->next) {
- if (head->info %2== 0) {
- if (head->next != NULL) {
- nod * _temp = head;
- head = head->next;
- delete _temp;
- break;
- }
- else {
- nod *_temp = head;
- last = head = NULL;
- delete _temp;
- goto exit;
- }
- }
- if (temp->info %2 == 0) {
- nod* _temp = head;
- while (_temp->next != temp) {
- _temp = _temp->next;
- }
- _temp->next = temp->next;
- if (temp == last) {
- _temp->next = last->next;
- last = _temp;
- }
- delete temp;
- break;
- }
- if (temp->info%2 != 0 && temp == last) {
- goto exit;
- }
- }
- exit:;
- }
- void main() {
- Lista cevapar, cevaimpar, ceva;
- int n;
- /* cout << "n = ";
- cin >> n;
- for (int i = 0; i < n; i++) {
- int nr;
- cin >> nr;
- if (nr % 2 == 0) {
- cevapar.Append(nr);
- }
- else {
- cevaimpar.Append(nr);
- }
- }
- cevapar.DisplayList();
- cevaimpar.DisplayList();*/
- ceva.Append(1);
- ceva.Append(2);
- ceva.Append(3);
- ceva.Append(4);
- ceva.Append(6);
- ceva.Append(12);
- //ceva.Append(7);
- ceva.DisplayList();
- ceva.Insert();
- ceva.DisplayList();
- ceva.RemovePar();
- ceva.DisplayList();
- _getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement