#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* prev;
struct Node* next;
} Node;
Node* head = NULL;
Node* create_node(int data) {
Node
* new_node
= (Node
*)malloc(sizeof(Node
)); new_node->data = data;
new_node->prev = new_node->next = NULL;
return new_node;
}
void insert(int data) {
Node* new_node = create_node(data);
if (!head) {
head = new_node;
return;
}
Node* curr = head;
while (curr->next) curr = curr->next;
curr->next = new_node;
new_node->prev = curr;
}
void delete_node(int data) {
Node* curr = head;
while (curr) {
if (curr->data == data) {
if (curr->prev) curr->prev->next = curr->next;
if (curr->next) curr->next->prev = curr->prev;
if (curr == head) head = curr->next;
return;
}
curr = curr->next;
}
}
void print_list() {
Node* curr = head;
while (curr) {
curr = curr->next;
}
}
int get_length() {
int count = 0;
Node* curr = head;
while (curr) {
count++;
curr = curr->next;
}
return count;
}
void delete_middle() {
int length = get_length();
if (length == 0) return;
int mid = length / 2;
Node* curr = head;
for (int i = 0; i < mid; i++) curr = curr->next;
delete_node(curr->data);
}
void exchange_first_last() {
if (!head || !head->next) return;
Node* first = head;
Node* last = head;
while (last->next) last = last->next;
int temp = first->data;
first->data = last->data;
last->data = temp;
print_list();
}
void search(int value) {
Node* curr = head;
int pos = 0;
printf("Positions of %d: ", value
); while (curr) {
if (curr
->data
== value
) printf("%d ", pos
); curr = curr->next;
pos++;
}
}
void find_min() {
if (!head) return;
int min = head->data;
Node* curr = head->next;
while (curr) {
if (curr->data < min) min = curr->data;
curr = curr->next;
}
printf("Smallest value: %d\n", min
); }
int main() {
insert(5);
insert(3);
insert(9);
insert(1);
insert(3);
print_list();
printf("Length: %d\n", get_length
()); delete_middle();
print_list();
exchange_first_last();
search(3);
find_min();
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCnR5cGVkZWYgc3RydWN0IE5vZGUgewogICAgaW50IGRhdGE7CiAgICBzdHJ1Y3QgTm9kZSogcHJldjsKICAgIHN0cnVjdCBOb2RlKiBuZXh0Owp9IE5vZGU7CgpOb2RlKiBoZWFkID0gTlVMTDsKCk5vZGUqIGNyZWF0ZV9ub2RlKGludCBkYXRhKSB7CiAgICBOb2RlKiBuZXdfbm9kZSA9IChOb2RlKiltYWxsb2Moc2l6ZW9mKE5vZGUpKTsKICAgIG5ld19ub2RlLT5kYXRhID0gZGF0YTsKICAgIG5ld19ub2RlLT5wcmV2ID0gbmV3X25vZGUtPm5leHQgPSBOVUxMOwogICAgcmV0dXJuIG5ld19ub2RlOwp9Cgp2b2lkIGluc2VydChpbnQgZGF0YSkgewogICAgTm9kZSogbmV3X25vZGUgPSBjcmVhdGVfbm9kZShkYXRhKTsKICAgIGlmICghaGVhZCkgewogICAgICAgIGhlYWQgPSBuZXdfbm9kZTsKICAgICAgICByZXR1cm47CiAgICB9CiAgICBOb2RlKiBjdXJyID0gaGVhZDsKICAgIHdoaWxlIChjdXJyLT5uZXh0KSBjdXJyID0gY3Vyci0+bmV4dDsKICAgIGN1cnItPm5leHQgPSBuZXdfbm9kZTsKICAgIG5ld19ub2RlLT5wcmV2ID0gY3VycjsKfQoKdm9pZCBkZWxldGVfbm9kZShpbnQgZGF0YSkgewogICAgTm9kZSogY3VyciA9IGhlYWQ7CiAgICB3aGlsZSAoY3VycikgewogICAgICAgIGlmIChjdXJyLT5kYXRhID09IGRhdGEpIHsKICAgICAgICAgICAgaWYgKGN1cnItPnByZXYpIGN1cnItPnByZXYtPm5leHQgPSBjdXJyLT5uZXh0OwogICAgICAgICAgICBpZiAoY3Vyci0+bmV4dCkgY3Vyci0+bmV4dC0+cHJldiA9IGN1cnItPnByZXY7CiAgICAgICAgICAgIGlmIChjdXJyID09IGhlYWQpIGhlYWQgPSBjdXJyLT5uZXh0OwogICAgICAgICAgICBmcmVlKGN1cnIpOwogICAgICAgICAgICByZXR1cm47CiAgICAgICAgfQogICAgICAgIGN1cnIgPSBjdXJyLT5uZXh0OwogICAgfQp9Cgp2b2lkIHByaW50X2xpc3QoKSB7CiAgICBOb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