Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ctime>
- #include <iostream>
- using namespace std;
- struct list
- {
- int field;
- struct list* next;
- struct list* prev;
- };
- list *create(list *lst) {
- list* tmp;
- tmp = new list;
- tmp->field = 1 + rand() % 100;
- tmp->next = NULL;
- tmp->prev = lst;
- lst->next = tmp;
- return tmp;
- }
- list* init() {
- list* lst;
- lst = new list;
- srand(time(0));
- lst->field = 1 + rand() % 100;
- lst->next = NULL;
- lst->prev = NULL;
- return(lst);
- }
- void show(list *first) {
- list* tmp = first;
- while (tmp) {
- cout << tmp->field << ' ';
- tmp = tmp->next;
- }
- }
- list* chek(list* tmp, list *&lstTop)
- {
- if (tmp->field % 2 == 0) {
- list* prev = tmp->prev, * next = tmp->next;
- if (prev == NULL) {
- next->prev = prev;
- lstTop = next;
- delete tmp;
- return next;
- }
- else if (next == NULL) {
- prev->next = next;
- delete tmp;
- return prev->next;
- }
- else
- prev->next = next;
- next->prev = prev;
- delete tmp;
- return next;
- }
- else return tmp->next;
- }
- void deleteAllList(list* tmp) {
- list* next;
- while (tmp) {
- cout << tmp->field << ' ';
- next = tmp->next;
- delete tmp;
- tmp = next;
- }
- }
- int main()
- {
- setlocale(LC_ALL, "RU");
- list* lst, *lstTop; int m; cout << "Введите кол узлов: "; cin >> m;
- lst = init(); lstTop = lst;
- for (int i = 1; i < m; i++) lst = create(lst);
- show(lstTop);
- for (list* p = lstTop; p; ) {
- p = chek(p, lstTop);
- }
- cout << endl;
- //show(lstTop);
- deleteAllList(lstTop);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement