Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include "ctime"
- using namespace std;
- //-----------------------------------------------------------
- struct Spis2 {
- int info;
- Spis2 * next, *prev;
- } *t, *beg, *finish;
- void Sort_p(Spis2 **);
- void Sort_info(Spis2 *);
- void Del_2(Spis2 *);
- void Del_All(Spis2 **);
- void Create(Spis2**, Spis2**, int);
- void Add(int, Spis2**, Spis2**, int);
- void View(int, Spis2*);
- //----------------------------------------------------------
- void Create(Spis2 **b, Spis2 **e, int in) {
- t = new Spis2;
- t->info = in;
- t->next = t->prev = NULL;
- *b = *e = t;
- }
- void Add(int kod, Spis2 **b, Spis2 **e, int in) {
- t = new Spis2;
- t->info = in;
- if (kod == 0) {
- t->prev = NULL;
- t->next = *b;
- (*b)->prev = t;
- *b = t;
- }
- else {
- t->next = NULL;
- t->prev = *e;
- (*e)->next = t;
- *e = t;
- }
- }
- //-----------------------------------------------------------
- void View(int kod, Spis2 *p) {
- while (t != NULL) {
- cout << " " << t->info << endl;
- if (kod == 0) t = t->next;
- else t = t->prev;
- }
- }
- //-----------------------------------------------------------
- void Del_2(Spis2 *p)
- {
- t = p->next;
- while (t != NULL) {
- if (t->info % 2 == 0) {
- p->next = t->next;
- delete t;
- t = p->next;
- }
- else {
- p = t;
- t = t->next;
- }
- }
- delete t;
- }
- //-----------------------------------------------------------
- void Sort_p(Spis2 **p) {
- Spis2 *t = NULL, *t1, *r, *l;
- if ((*p)->next->next == NULL) return;
- do {
- for (t1 = *p; t1->next->next != t; t1 = t1->next)
- if (t1->next->info > t1->next->next->info) {
- r = t1->next->next; l = t1->next->next->prev;
- t1->next->next = r->next;
- t1->next->prev = l;
- r->next = t1->next;
- l->prev = t1->next->prev;
- t1->next = r;
- }
- t = t1->next;
- } while ((*p)->next->next != t);
- }
- void Sort_info(Spis2 *p) {
- Spis2 *t = NULL, *t1;
- int r;
- do {
- for (t1 = p; t1->next != t; t1 = t1->next)
- if (t1->info > t1->next->info) {
- r = t1->info;
- t1->info = t1->next->info;
- t1->next->info = r;
- }
- t = t1;
- } while (p->next != t);
- }
- void Del_All(Spis2 **p) {
- Spis2 *t;
- while (*p != NULL) {
- t = *p;
- *p = (*p)->next;
- delete t;
- }
- }
- Spis2* InSpis2(Spis2 *p, int in) {
- Spis2 *t = new Spis2;
- t->info = in;
- t->next = p;
- return t;
- }
- Spis2* pop(Spis2* p, int &out)
- {
- if (!p) return p;
- out = p->info;
- Spis2* tmp = p->next;
- delete p;
- return tmp;
- }
- //-----------------------------------------------------------
- int main()
- {
- int in, kod, kod1;
- char Str[2][10] = { "Begin ", "End " };
- while (true) {
- cout << "\n\tCreat - 1.\n\tAdd - 2.\n\tView - 3.\n\tDel - 4.\n\tSort1 - 5.\n\tSort2 - 6.\n\tInd_zad - 7.\n\tEXIT - 0. : ";
- cin >> kod;
- switch (kod) {
- case 1: if (beg != NULL) {
- cout << "Clear Memory!" << endl;
- break;
- }
- cout << "Begin Info = "; cin >> in;
- Create(&beg, &finish, in);
- cout << "Creat Begin = " << beg->info << endl;
- break;
- case 2:
- cout << "Info = "; cin >> in;
- cout << "Add Begin - 0, Add End - 1 : "; cin >> kod1;
- Add(kod1, &beg, &finish, in);
- if (kod1 == 0) t = beg;
- else t = finish;
- cout << "Add to " << Str[kod1] << " " << t->info << endl;
- break;
- case 3: if (!beg) {
- cout << "Spis2 Pyst!" << endl;
- break;
- }
- cout << "--- Spis2 ---" << endl;
- cout << "View Begin-0,View End-1:";
- cin >> kod1;
- if (kod1 == 0) {
- t = beg;
- cout << "-- Begin --" << endl;
- }
- else {
- t = finish;
- cout << "--- End --" << endl;
- }
- View(kod1, t);
- break;
- case 4:
- Del_All(&beg);
- cout << "Memory Free!" << endl;
- break;
- case 5:
- beg = InSpis2(beg, 0);
- int kek;
- if (beg != NULL) Sort_p(&beg);
- beg = pop(beg, kek);
- break;
- case 6:
- if (beg != NULL) Sort_info(beg);
- break;
- case 7:
- beg = InSpis2(beg, 1337);
- if (beg != NULL) Del_2(beg);
- int t;
- beg = pop(beg, t);
- break;
- case 0:
- if (beg != NULL)
- Del_All(&beg);
- return 0;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement