-
Notifications
You must be signed in to change notification settings - Fork 0
/
Self-organizing list.cpp
135 lines (110 loc) · 2.23 KB
/
Self-organizing list.cpp
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
// Self organizing list is a data structure where a node becomes a head node after searching it
#include <bits/stdc++.h>
using namespace std;
typedef unsigned short usint;
typedef unsigned uint;
typedef long lint;
typedef short sint;
typedef long double ld;
typedef unsigned long ulint;
typedef unsigned long long ullint;
typedef long long llint;
#define INF ullint(2e9)
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define pii pair <int,int>
#define pib pair <int,bool>
#define pll pair <long,long>
#define pcc pair <char,char>
#define pui pair <uint,uint>
#define pul pair <ulint,ulint>
#define pff pair <float,float>
#define pllll pair <llint,llint>
#define pss pair <string,string>
#define pullull pair <ullint,ullint>
struct SOL
{
int val;
SOL *next;
SOL()
{
val = 0;
next = nullptr;
}
};
void ispis(SOL *head)
{
cout << '\n';
SOL *temp = head;
while (temp != nullptr)
{
cout << temp->val << ' ';
temp = temp->next;
}
cout << '\n';
}
SOL* createNew(int &x)
{
SOL *novi = new SOL;
novi->val = x;
novi->next = nullptr;
return novi;
}
SOL* ubaci(SOL *head, int &x)
{
if (head == nullptr)
return createNew(x);
head->next = ubaci(head->next,x);
return head;
}
SOL* trazi(SOL *head, int &x)
{
if (head == nullptr || head->next == nullptr) return nullptr;
if (head->next->val == x) return head;
return trazi(head->next,x);
}
void oslobodi(SOL *head)
{
if (head == nullptr) return;
oslobodi(head->next);
delete head;
}
int main()
{
ios_base::sync_with_stdio(0);
SOL *head = nullptr;
int n;
cin >> n;
for (int t1 = 0; t1 < n; ++t1)
{
int x;
cin >> x;
head = ubaci(head,x);
}
ispis(head);
int q;
cin >> q;
while (q--)
{
int a;
cin >> a;
if (a == 2)
ispis(head);
else
{
int x;
cin >> x;
SOL *f = trazi(head,x);
if (f != nullptr)
{
SOL *cp = f->next;
f->next = cp->next;
cp->next = head;
head = cp;
}
}
}
oslobodi(head);
return 0;
}