summaryrefslogtreecommitdiff
path: root/list/task7.cpp
blob: 600c9a5c18cf25e4ef25a34e132ac83d57a724a6 (plain)
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
#include <iostream>
#include "list.h"

using namespace std;

void solve(list *&h, list *&t, int x)
{
    list *tmp_h = NULL;
    list *tmp_t = NULL;

    list *res_h = NULL;
    list *res_t = NULL;

    list *cur = h;
    while (cur)
    {
        if (cur->inf < x)
            push(res_h, res_t, cur->inf);
        else
            push(tmp_h, tmp_t, cur->inf);

        cur = cur->next;
    }

    cur = tmp_h;
    while (cur)
    {
        push(res_h, res_t, cur->inf);
        cur = cur->next;
    }

    h = res_h;
    t = res_t;
}

int main()
{
    int n;
    cout << "n = ";
    cin >> n;

    int x;
    cout << "x = ";
    cin >> x;

    list *head = NULL;
    list *tail = NULL;
    int t;
    for (int i = 0; i < n; i++)
    {
        cin >> t;
        push(head, tail, t);
    }

    solve(head, tail, x);
    print(head, tail);
    cout << endl;

    return 0;

}