-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.cpp
More file actions
193 lines (187 loc) · 3.22 KB
/
Copy pathLinkedList.cpp
File metadata and controls
193 lines (187 loc) · 3.22 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <bits/stdc++.h>
#define lld long long int
using namespace std;
struct node
{
int a;
struct node *next;
};
//node *first = NULL;
node *create(int a[], int n)
{
node *temp;
node *last;
node *fi = new node;
temp = fi;
temp->a = a[0];
temp->next = NULL;
last = fi;
for (int i = 1; i < n; i++)
{
temp = new node;
temp->a = a[i];
temp->next = NULL;
last->next = temp;
last = temp;
}
return fi;
}
void display(struct node *fir)
{
cout << fir->a << " ";
if (fir->next != NULL)
{
display(fir->next);
}
}
int count(struct node *fir)
{
static int counter = 0;
counter++;
if (fir->next != NULL)
{
count(fir->next);
}
else
return counter;
}
int sum(struct node *fir)
{
static int suma = 0;
suma += fir->a;
if (fir->next != NULL)
{
sum(fir->next);
}
else
return suma;
}
int max(struct node *fir)
{
static int maxi = 0;
maxi = max(maxi, fir->a);
if (fir->next != NULL)
{
max(fir->next);
}
else
return maxi;
}
bool search(struct node *fir, int key)
{
if (fir->a == key)
return true;
if (fir->next == NULL)
return false;
else
search(fir->next, key);
}
node *insert(struct node *fir, int pos, int val)
{
node *fi = fir;
node *counter = fir;
node *temp = new node;
temp->a = val;
if (fir == NULL)
{
temp->next = NULL;
fi = temp;
}
else
{
while (pos-- > 2)
{
counter = counter->next;
}
temp->next = counter->next;
counter->next = temp;
}
return fi;
}
node *sortedInsert(struct node *fir, int x)
{
node *fi = fir;
node *temp = new node;
temp->a = x;
node *p = fir;
if (fir == NULL)
{
temp->next = NULL;
fi = temp;
}
else if (fir->next == NULL)
{
if (x > fir->a)
{
fir->next = temp;
temp->next = NULL;
}
else
{
temp->next = fir;
fi = temp;
}
}
else
{
while (p->next->a < x && p->next != NULL)
{
p = p->next;
}
temp->next = p->next;
p->next = temp;
}
return fi;
}
node *delete_node(struct node *fir, int val)
{
node *fi = fir;
node *temp = NULL;
if (fir->next == NULL)
{
fi = NULL;
delete (fir);
}
else
{
while (fir->next->a != val)
{
fir = fir->next;
}
temp = fir->next;
fir->next = fir->next->next;
delete (temp);
}
return fi;
}
node *merge(node *fir, node *sec)
{
node *temp = fir;
while (fir->next != NULL)
fir = fir->next;
fir->next = sec;
return temp;
}
int main()
{
ios_base::sync_with_stdio(false);
int ar[] = {5, 9, 12, 67, 128};
node *first = NULL;
node *second = NULL;
second = create(ar, 5);
display(second);
//delete_node(first, 12);
cout << "\n";
// display(first);
// cout << sum(first) << "\n";
// cout << max(first) << "\n";
// bool flag = search(first, 32) != 0 ? true : false;
// cout << flag << "\n";
first = sortedInsert(first, 130);
first = sortedInsert(first, 260);
first = sortedInsert(first, 11);
display(first);
cout << "\n";
first = merge(second, first);
display(first);
}