123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- #include <stdio.h>
- #include <malloc.h>
- struct onelist_list {
- int id;
- struct onelist_list* new_elem;
- };
- typedef struct onelist_list list;
- list* create_list(int count_elem) {
- list* first_elem = (list*)malloc(sizeof(list));
- first_elem->id = 0;
- list* post_elem_list, * next_elem_eist;
- post_elem_list = first_elem;
- for (size_t i = 0; i < count_elem - 1; i++)
- {
- next_elem_eist = (list*)malloc(sizeof(list));
- next_elem_eist->id = post_elem_list->id + 1;
- post_elem_list->new_elem = next_elem_eist;
- post_elem_list = next_elem_eist;
- }
- post_elem_list->new_elem = NULL;
- return first_elem;
- }
- void delete_list(list* one_list) {
- list* temp_list = one_list;
- while (temp_list) {
- temp_list = one_list->new_elem;
- free(one_list);
- one_list = temp_list;
- }
- }
- list* input_in_list(list* one_list, int index, int value) {
- list* new_elem = (list*)malloc(sizeof(list));
- list* head = one_list;
- new_elem->id = value;
- new_elem->new_elem = NULL;
- if (index == 0) {
- new_elem->new_elem = one_list;
- return new_elem;
- }
- int count = 0;
- while (one_list) {
- if (count == index - 1) {
- list* temp_elem = one_list->new_elem;
- one_list->new_elem = new_elem;
- new_elem->new_elem = temp_elem;
- break;
- }
- else {
- one_list = one_list->new_elem;
- count++;
- }
- }
- one_list = head;
- return one_list;
- }
- list* delete_in_list(list* one_list, int index) {
- list* head = one_list;
- if (index == 0) {
- head = one_list->new_elem;
- free(one_list);
- one_list = head;
- return one_list;
- }
- int count = 0;
- while (one_list) {
- if (count == index - 1) {
- list* temp_elem = one_list->new_elem->new_elem;
- free(one_list->new_elem);
- one_list->new_elem = temp_elem;
- break;
- }
- else {
- one_list = one_list->new_elem;
- count++;
- }
- }
- /*if (index == count_elem || index == count_elem - 1) {
- for (size_t i = 0; i < count_elem; i++)
- {
- if (i == count_elem - 2) {
- free(one_list->new_elem);
- one_list->new_elem = NULL;
- one_list = head;
- return one_list;
- }
- else {
- one_list = one_list->new_elem;
- }
- }
- }
- for (size_t i = 0; i < count_elem; i++)
- {
- if (i == index - 1) {
- list* temp_elem = one_list->new_elem->new_elem;
- free(one_list->new_elem);
- one_list->new_elem = temp_elem;
- break;
- }
- else {
- one_list = one_list->new_elem;
- }
- }*/
- one_list = head;
- return one_list;
- }
- list* change_in_list(list* one_list, int index_first, int index_second) {
- if (index_first > index_second) {
- int temp = index_first;
- index_first = index_second;
- index_second = temp;
- }
- list* head = one_list;
- list* first_elem = malloc(sizeof(list));
- list* second_elem = malloc(sizeof(list));
- int count = 0;
- while (one_list) {
- if (count == index_first)
- {
- first_elem = one_list;
- break;
- }
- else {
- one_list = one_list->new_elem;
- count++;
- }
- }
- one_list = head;
- count = 0;
- while (one_list) {
- if (count == index_second)
- {
- second_elem = one_list;
- break;
- }
- else {
- one_list = one_list->new_elem;
- count++;
- }
- }
- one_list = head;
- if (index_first != 0) {
- list* temp_elem = first_elem->new_elem;
- first_elem->new_elem = second_elem->new_elem;
- second_elem->new_elem = temp_elem;
- count = 0;
- while (one_list) {
- if (count == index_first - 1) {
- one_list->new_elem = second_elem;
- break;
- }
- else {
- one_list = one_list->new_elem;
- count++;
- }
- }
- one_list = head;
- count = 0;
- while (one_list) {
- if (count > index_first && one_list->new_elem->id == second_elem->id) {
- one_list->new_elem = first_elem;
- break;
- }
- else {
- one_list = one_list->new_elem;
- count++;
- }
- }
- }
- else {
- list* temp_elem = second_elem->new_elem;
- second_elem->new_elem = head->new_elem;
- head = second_elem;
- one_list = head;
- count = 0;
- while (one_list) {
- if (count != 0 && one_list->new_elem->id == second_elem->id) {
- one_list->new_elem = first_elem;
- first_elem->new_elem = temp_elem;
- break;
- }
- else {
- one_list = one_list->new_elem;
- count++;
- }
- }
- }
- one_list = head;
- return one_list;
- }
- list* print_list(list* one_list) {
- list* head = one_list;
- while(one_list) {
- printf_s("%d ", one_list->id);
- one_list = one_list->new_elem;
- }
- return head;
- }
- int main() {
- list* one_list = create_list(10);
- one_list = print_list(one_list);
- printf_s("\n");
- //delete_list(one_list);
- one_list = input_in_list(one_list, 0, 20);
- one_list = input_in_list(one_list, 7, 20);
- one_list = input_in_list(one_list, 7, 20);
- one_list = input_in_list(one_list, 7, 20);
- one_list = input_in_list(one_list, 7, 20);
- one_list = input_in_list(one_list, 7, 20);
- one_list = input_in_list(one_list, 7, 20);
- one_list = input_in_list(one_list, 7, 20);
- one_list = input_in_list(one_list, 7, 20);
- one_list = print_list(one_list);
- printf_s("\n");
- one_list = delete_in_list(one_list, 5);
- one_list = print_list(one_list);
- printf_s("\n");
- one_list = change_in_list(one_list, 1, 0);
- one_list = print_list(one_list);
- printf_s("\n");
- }
|