Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

БДЗ ver 0306

.pdf
Скачиваний:
0
Добавлен:
03.06.2026
Размер:
299.56 Кб
Скачать

new_node->next=curr->next; new_node->prev=curr; curr->next = new_node;

}

void delete_value(Node** head, int value) { Node* curr = *head;

Node* prev = NULL;

while (curr->data!=value){prev = curr; curr = curr->next;} if (curr==*head){*head=curr->next; (*head)->prev=NULL;} else{prev->next = curr->next; curr->next->prev = prev;} free(curr);

}

Node* find(Node* head, int value) { Node* curr = head;

while (curr->data!=value){curr=curr->next;} return curr;

}

void free_list(Node* head) { Node* curr = head;

// Node* prev = NULL; while(curr->next!=NULL){free(curr->prev); curr=curr->next;}

}

void print_list(Node* head) { Node* curr = head;

while (curr!=NULL){ printf("%d ",curr->data); curr = curr->next;

}

}

void print_list_reverse(Node* head) { Node* curr = head;

while(curr->next!=NULL){curr = curr->next;} while(curr!=NULL){

printf("%d ", curr->data); curr = curr->prev;

}

}

#55 Симметричеость списка

void sym (Node* head){ Node* curr = head; Node* equal = head;

if(head==NULL || head->next==NULL){printf("wow ur empty array is symmetrical! \n"); return;}

int size = 0;

while(curr!=NULL){ size++; curr=curr->next;} // printf("arr size is: %d\n", size);

for (int i = 0; i < size / 2; i++) { Node* left = head;

Node* right = head;

for (int j = 0; j < i; j++) {left = left->next;}

for (int j = 0; j < size - 1 - i; j++) {right = right->next;} if (left->data != right->data) {printf("NOOOOOOOO \n");return;}

}

printf("ur array is symmetrical! \n");

}

ПЛИС Стр.11

#56 развернуть список

Node* reverse (Node* head){ Node* prev = NULL; Node* curr = head; Node* next = NULL; while(curr!=NULL){

next = curr->next; curr->next = prev; prev=curr; curr=next;

}

return prev;

}

#57 Есть ли цикл? алгоитм черепаха заяц void cycle(Node* head){

if (head==NULL || head->next==NULL){printf("this bitch is empty\n"); return;} Node* tort = head;

Node* hare = head;

while(hare!=NULL && hare->next!=NULL){ tort=tort->next; hare=hare->next->next;

if (tort==hare){printf("there is a cycle!\n"); return;}

}

printf("no cycle in here ig\n");

}

#58 Объеденить 2 списка

Node* mergeTwoLists(Node* list1, Node* list2){ Node new_list;

new_list.next=NULL; Node* curr = &new_list;

while(list1!=NULL && list2 !=NULL){

if (list1->data<=list2->data){curr->next=list1; list1=list1->next;} else {curr->next=list2; list2=list2->next;}

curr=curr->next;

}

if(list1!=NULL){

while(list1!=NULL){curr->next=list1; curr=curr->next; list1=list1->next;}

}

if(list2!=NULL){

while(list2!=NULL){curr->next=list2; curr=curr->next; list2=list2->next;}

}

return new_list.next;

}

#59 Бинарное дерево

#include <stdio.h> #include <stdlib.h> #include "tree.h"

Node* create_node(int key) {

Node* new_node = (Node*)malloc(sizeof(Node)); if(new_node==NULL){printf("malloc error!"); exit(EXIT_FAILURE);} new_node->key=key;

new_node->left=NULL; new_node->right=NULL; return new_node;

}

Node* insert(Node* root, int key) { Node* new_node = create_node(key); if (root==NULL){return new_node;} Node* curr = root;

ПЛИС Стр.12

Node* parent = NULL;

while(curr!=NULL){

parent=curr; if(key<curr->key){curr=curr->left;}

else if(key>curr->key){curr=curr->right;} else{free(new_node); return root;}

}

if (key<parent->key){parent->left=new_node;} else{parent->right=new_node;}

return root;

}

Node* search(Node* root, int key) { Node* curr = root; while(curr!=NULL){

if(key<curr->key){curr=curr->left;}

else if(key>curr->key){curr=curr->right;} else{return curr;}

}

return NULL;

}

static Node* find_min(Node* root) { if (root==NULL){return NULL;} Node* curr = root;

while(curr->left!=NULL){curr=curr->left;} return curr;

}

Node* delete(Node* root, int key) { if (root==NULL){return NULL;}

if (key<root->key){root->left=delete(root->left, key);}

else if(key>root->key){root->right=delete(root->right, key);} else{

if(root->left==NULL){Node* temp = root->right; free(root); return temp;} // 0 или 1

потомок

else if(root->right==NULL){Node* temp = root->left; free(root); return temp;}

else{

 

Node* succ = find_min(root->right);

// 2 потомка

root->key = succ->key;

// перемещаем

меньшего и сносим его

 

root->right = delete(root->right, succ->key);

 

}

 

}

 

return root;

 

}

 

void inorder(Node* root) {

 

if (root==NULL){return;}

 

inorder(root->left);

 

printf("%d ", root->key);

 

inorder(root->right);

 

}

 

void free_tree(Node* root) {

 

if (root==NULL){return;}

 

free_tree(root->left);

 

free_tree(root->right);

 

free(root);

 

}

 

#60 skip

============================================================================================

==========

ПЛИС Стр.13

#61 Ввод вывод строк

#include <stdio.h> #include <stdlib.h>

int main(){

char my_str[100];

fgets(my_str, sizeof(my_str), stdin); puts(my_str);

return 0;

}

#62 atoi

#include <stdio.h> #include <stdlib.h>

void main(){

char str_age[200];

fgets(str_age, sizeof(str_age), stdin); int age = atoi(str_age);

if(age<0){printf("naah that not possible");} else if(age<6){fputs("its free!", stdout);}

else if(age<18){fputs("its 50 rubs for u fam", stdout);} else{fputs("its 100 money.", stdout);}

}

#63 strccmp #include <stdio.h> #include <stdlib.h> #include <string.h>

int str_cmp(char* str1, char* str2){

while (*str1 != '\0' && *str2 != '\0' && *str1 == *str2) { str1++;

str2++;

}

return *str1 - *str2;

}

void main (){

char str_1[100]; char str_2[100];

fgets(str_1, sizeof(str_1), stdin); fgets(str_2, sizeof(str_2), stdin);

int result = strcmp(str_1, str_2); if (result==0){printf("equal!!!\n");} else{printf("not equal(( \n");}

int my_result = str_cmp(str_1, str_2); if (result==0){printf("equal\n");}

else if(result<0){printf("str1<str2\n");} else{printf("str1>str2\n");}

}

#64 strcat #include <stdio.h> #include <stdlib.h> #include <string.h>

char *new_merge_str(char* str1, char* str2){

char* new_str = (char*)malloc(strlen(str1) + strlen(str2) + 1);

char* ptr = new_str; while(*str1!='\0'){*ptr=*str1; ptr++; str1++;}

ПЛИС Стр.14

while(*str2!='\0'){*ptr=*str2; ptr++; str2++;}

*ptr = '\0'; return new_str;

}

void *merge_str(char* str1, char* str2){ while (*str1 != '\0'){str1++;}

while (*str2 != '\0'){*str1 = *str2; str1++; str2++;} *str1 = '\0';

}

int new_len(char* str){ char* ptr = str; while(*ptr!='\0'){ptr++;} return ptr-str;

}

void main(){

char str_1[100]; char str_2[100];

fgets(str_1, sizeof(str_1), stdin); fgets(str_2, sizeof(str_2), stdin); str_1[strcspn(str_1, "\n")] = '\0';

strcat(str_1,str_2); fputs(str_1,stdout);

printf("len of str_1: %d", new_len(str_1));

//char* new_str = new_merge_str(str_1, str_2);

//fputs(new_str,stdout);

//new_str[strcspn(new_str, "\n")] = '\0';

//merge_str(new_str, str_1);

//fputs(new_str,stdout);

}

#65 strstr #include <stdio.h>

#include <string.h>

char* str_in_str(char* text, char* str){ if (*str =='\0') {return (char*)text;}

for (;*text;text++){ char* t = text; char* s = str;

while(*t && *s && *t==*s){t++; s++;} if(*s == '\0'){return (char*)text;}

}

return NULL;

}

void main(){

char text[100];

fgets(text, sizeof(text), stdin); char* found = strstr(text, "hello");

if (found!=NULL){printf("found at %ld", found-text);} else{printf("nah");}

char* my_found = str_in_str(text, "hello");

if (found!=NULL){printf("found at %ld", found-text);} else{printf("nah");}

ПЛИС Стр.15

}

#66 str tok #include <stdio.h> #include <string.h>

int is_split(char c, char* split){ while(*split){if(c==*split++){return 1;}} return 0;

}

char* str_tok (char* str, char* split){ static char* next;

if (str) next = str;

if(next==NULL){return NULL;}

while(*next && is_split(*next, split)){next++;} if (*next=='\0'){next=NULL; return NULL;}

char* token = next;

while(*next && !is_split(*next, split)){next++;} if(*next){*next++ ='\0';}

else{next = NULL;}

return token;

}

int main (){

char my_str[] = "Richard Of York Gave Battle In Vain"; char* my_ptr = strtok(my_str," "); while(my_ptr!=NULL){

printf("%s\n", my_ptr);

my_ptr = strtok(NULL, " "); //вызов NULL это значит с того же места

}

char new_str[] = "Richard Of York Gave Battle In Vain"; char* new_ptr = str_tok(new_str," "); while(new_ptr!=NULL){

printf("%s\n", new_ptr);

new_ptr = str_tok(NULL, " "); //вызов NULL это значит с того же места

}

}

#67

#include <stdio.h> #include <string.h> #include <stdlib.h>

int ascii_to_int(char* str){ int result = 0;

int sign = 1; int i = 0;

while (str[i]==' ' || str[i]=='\t' || str[i]=='\n'){i++;}

if (str[i]=='-'){sign=-1;i++;}

while (str[i]>='0' && str[i]<='9'){result=result*10+(str[i]-'0'); i++;}

return result*sign;

}

void main(){

printf("%d", ascii_to_int("123")); printf("%d", ascii_to_int("-55"));

ПЛИС Стр.16

printf("%d", ascii_to_int("00012304")); printf("%d", ascii_to_int("lelele666le"));

}

#68

#include <stdio.h> #include <stdlib.h> #include <string.h>

int str_cmp(char* str1, char* str2){

while (*str1 != '\0' && *str2 != '\0' && *str1 == *str2) { str1++;

str2++;

}

return *str1 - *str2;

}

#69

#include <stdio.h> #include <stdlib.h> #include <string.h>

char *new_merge_str(char* str1, char* str2){

char* new_str = (char*)malloc(strlen(str1) + strlen(str2) + 1);

char* ptr = new_str; while(*str1!='\0'){*ptr=*str1; ptr++; str1++;} while(*str2!='\0'){*ptr=*str2; ptr++; str2++;}

*ptr = '\0'; return new_str;

}

void *merge_str(char* str1, char* str2){ while (*str1 != '\0'){str1++;}

while (*str2 != '\0'){*str1 = *str2; str1++; str2++;} *str1 = '\0';

}

int new_len(char* str){ char* ptr = str; while(*ptr!='\0'){ptr++;} return ptr-str;

}

#70

#include <stdio.h> #include <string.h>

char* str_in_str(char* text, char* str){ if (*str =='\0') {return (char*)text;}

for (;*text;text++){ char* t = text; char* s = str;

while(*t && *s && *t==*s){t++; s++;} if(*s == '\0'){return (char*)text;}

}

return NULL;

}

#71

char* str_tok (char* str, char* split){ static char* next;

ПЛИС Стр.17

if (str) next = str;

if(next==NULL){return NULL;}

while(*next && is_split(*next, split)){next++;} if (*next=='\0'){next=NULL; return NULL;}

char* token = next;

while(*next && !is_split(*next, split)){next++;} if(*next){*next++ ='\0';}

else{next = NULL;}

return token;

}

#72 skiiiiip

============================================================================================

===============

#73 ez gg

#74

#include <stdio.h>

void bin(long unsigned int k, long unsigned int size)

{

int total_bits = size*8;

for (int i=total_bits-1; i>=0; i--){

printf("%lu", (k>>i)&1); //бит %lu это unsigned long

if (i%8==0 && i!=0){printf(" ");} // пробел между байтами

}

printf("\n");

}

#75

int setbit(const int value, const int position) { return value | (1<<position);

}

#76

int unsetbit(const int value, const int position) { return value & ~(1<<position);

}

#77

int switchbit(const int value, const int position) { return value ^ (1 << position);

}

#78

int checkbit(const int value, const int position) { return (value >> position) & 1;

}

#79 skip

============================================================================================

=====================

#80

#include <stdio.h> #include <stdlib.h>

int main(void)

{

FILE* fp;

ПЛИС Стр.18

char sym[10];

fp = fopen("data.txt", "w"); fprintf(fp, "%s", "amogus is sus!"); fclose(fp);

fp = fopen("data.txt", "r");

while ((fgets(sym, 10, fp)) != NULL)

{

printf("%s\n", sym);

}

fclose(fp);

return 0;

}

#81

#include <stdio.h> #include <stdlib.h>

int main(void)

{

FILE* fp; char sym[10];

fp = fopen("data.txt", "w");

char messege[] = "amogus is sus!"; for (int i=0; i<sizeof(messege);i++){

putc(messege[i], fp);

}

fclose(fp);

char c;

fp = fopen("data.txt", "r");

while ((c=getc(fp))!= EOF){printf("%c", c);} fclose(fp);

return 0;

}

#82

#include <stdio.h> #include <stdlib.h> #include <string.h>

void str_in_file (FILE* fp, char* str){ char line[4096];

int line_num = 0; rewind(fp);

int found=0;

while (fgets(line, 4096, fp) != NULL){ line_num++;

line[strcspn(line, "\n")] = '\0'; char* find = strstr(line,str);

if (find!=NULL){printf("str found at %d, %ld \n",line_num, find-line+1); found=1;}

}

if (found==0){printf("str found at %d, %d \n",-1, -1);}

}

void main(){ FILE* fp;

char sym[4096];

fp = fopen("data.txt", "r");

while ((fgets(sym, 4096, fp)) != NULL)

{

printf("%s\n", sym);

}

ПЛИС Стр.19

str_in_file(fp, "Doshik was here"); fclose(fp);

}

#83

#include <stdio.h> #include <stdint.h>

uint32_t check_sum_djb(char* filename){ FILE* fp = fopen(filename, "rb");

if (fp==NULL){printf("error"); return 0;}

uint32_t hash = 666; int byte;

while ((byte=fgetc(fp))!=EOF){hash = ((hash<<5)+hash)+(unsigned char)byte;} fclose(fp);

return hash;

}

void main(){

int hash = check_sum_djb("data.txt"); printf("%d", hash);

}

#84

============================================================================================

= skip

#87

#include <stdio.h>

#define print_value(a) _Generic((a),\ int: print_value_int,\

float: print_value_float,\ double: print_value_double,\ char: print_value_char,\ char*: print_value_string,\ void*: print_value_ptr,\ default: print_value_unknown\

)(a)

void print_value_int(int a) {printf("int: %d\n", a);}

void print_value_float(float a) {printf("float: %f\n", a);} void print_value_double(double a) {printf("double: %lf\n", a);} void print_value_char(char a) {printf("char: '%c'\n", a);}

void print_value_string(char* a) {printf("string: \"%s\"\n", a);} void print_value_ptr(void* a) {printf("pointer: %p\n", a);}

void print_value_unknown() { printf("unsupported type\n");}

#define max(a, b) _Generic((a),\ int: max_int,\

float: max_float,\ double: max_double,\ long: max_long\

)(a, b)

int max_int(int a, int b) {return (a > b) ? a : b;}

float max_float(float a, float b) {return (a > b) ? a : b;} double max_double(double a, double b) {return (a > b) ? a : b;} long max_long(long a, long b) {return (a > b) ? a : b;}

#define is_int(a) _Generic((a),\

ПЛИС Стр.20