Swap of elements in bidirectional list.

Guys i have to swap the 2nd element and max.I did it,but the code is too big,i need ur help with func exchange.It needs to be optimized.I need ur help guys.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
  struct Spis2 {
int info;
Spis2 *next, *prev;
};
void create(Spis2 **b, Spis2 **e)
{
Spis2 *t = new Spis2;
int in;
cout << "Input value -  ";
cin >> in;
t->info = in;
t->next = NULL;
if (*b == NULL) {
    t->prev = t->next = NULL;
    *b = *e = t;
} else {
    t->next = NULL;
    t->prev = *e;
    (*e)->next = t;
    (*e) = t;
}
}
void view(Spis2 *begin, Spis2 *end) 
{
Spis2 *t;
int d;
if (begin == NULL) {
    cout << "Queue is empty!" << endl;
    return;
}
cout << "Select way to view queue: \n1 - from start \n2 - from end" << endl;
cin >> d;
switch (d) {
    case 1:
        t = begin;
        cout << "Queue: " << endl;
        while (t != NULL) {
            cout << t->info << endl;
            t = t->next;
        }
        break;
    case 2:
        t = end;
        cout << "Queue: " << endl;
        while (t != NULL) {
            cout << t->info << endl;
            t = t->prev;
        }
        break;
}
cout << endl;
}
Spis2 *max(Spis2 *begin) 
{

int maks = begin->info;
Spis2 *t = begin, *max = begin;
while (t != NULL) {
    if (t->info > maks) {
        max = t;
        maks = t->info;
    }
    t = t->next;
}
return max;
}
Spis2* exchange(Spis2 *begin, Spis2 *max, Spis2 **end) {
Spis2 *p = begin->next;
if (max == p) 
{
    cout << "Second element is biggest!" << endl;
    return begin;
}
Spis2 *tmp;
if (p->next == NULL) 
{
    begin->next = NULL;
    p->prev = NULL;
    p->next = begin;
    begin->prev = p;
    *end = begin;
    return p;
}

if (max == begin) 
{
    tmp = p->next;
    tmp->prev = begin;
    begin->next = tmp;
    p->next = begin;
    begin->prev = p;
    p->prev = NULL;
    return p;
}
if (max->next == NULL) 
{
    max->prev->next = p;
    p->prev->next = max;
    p->next->prev = max;
    tmp = max->prev;
    max->prev = begin;
    p->prev = tmp;
    max->next = p->next;
    p->next = NULL;
    *end = p;
    return begin;
}

if (max == p->next)
{
max->next->prev = p;
max->prev->next = max -> next;
p->prev->next = max;
p->next = max->next;
p->prev = max;
max->prev = begin;
begin->prev = NULL;
return begin;
}

max->next->prev = p;
max->prev->next = p;
p->prev->next = max;
p->next->prev = max;
tmp = max->next;
max->next = p->next;
p->next = tmp;
tmp = p->prev;
p->prev = max->prev;
max->prev = begin;

return begin;
}
int main() {
srand(time(NULL));
int n, y;
Spis2 *begin = NULL, *end = NULL, *begin1 = NULL, *end1 = NULL;
cout << "Input queue size: ";
cin >> n;
for (int i = 0; i < n; i++) create(&begin, &end);
view(begin, end);
begin = exchange(begin, max(begin), &end);
view(begin, end);
system("pause");
return 0;
}
Topic archived. No new replies allowed.