Доброго времени суток. Мне нужна помощь. Пусть и прозвучит это странно, мне надо объяснить эту программу, то есть объяснить, как она работает:
#include<stdio.h>
#include<stdlib.h>
struct list {
int field;
struct list * next;
};
int ft_atoi(char *str) {
int result = 0;
int sign = 1;
unsigned int i = 0;

while(str[i] == ' ' || str[i] == '\n') {
++i;
}
while(str[i] == '-' || str[i] == '+') {
if(str[i] == '-') {
sign = sign * -1;
}
++i;
}
while(str[i] >= '0' && str[i] <= '9') {
result = result * 10 + str[i] - '0';
++i;
}
return((int)(result*sign));
}
void ft_push(struct list ** headRef, int value) {
struct list * lst = (struct list*) malloc(sizeof(struct list));
lst->field = value;
lst->next = *headRef;
*headRef = lst;
}

void ft_push_end(struct list ** headRef, int value) {
struct list * current = *headRef;
if(current == NULL) {
ft_push(headRef, value);
}
while(current->next != NULL) {
current = current->next;
}
ft_push(&(current->next), value);
}

void ft_swap(struct list ** headRef, int numf, int nums) {
struct list * lstf = *headRef;
struct list * lsts = *headRef;
while(lstf->field != numf && lstf->next != NULL) {
lstf = lstf->next;
}
if(lstf->field == numf) {
while(lsts->field != nums && lstf->next != NULL) {
lsts = lsts->next;
}
if(lsts->field == nums) {
int temp = lstf->field;
lstf->field = lsts->field;
lsts->field = temp;
}
}
}

void ft_insert(struct list ** headRef, struct list * lst, int value) {
if(*headRef == NULL) {
return;
}
else if((*headRef)->field == value) {
while(lst) {
ft_push(headRef, lst->field);
lst = lst->next;
}
}
else {
struct list * headf = *headRef;
struct list * heads = *headRef;
struct list * lstf = lst;

int i = 0;
while(headf->field != value && headf->next != NULL) {
++i;
headf = headf->next;
}

if (headf->field == value) {
while(lstf->next) {
lstf = lstf->next;
}
while(i != 1) {
heads = heads->next;
—i;
}
heads->next = lst;
lstf->next = headf;
headf = lstf;
}
}
}

void ft_pop_n(struct list ** headRef, int index) {
if(*headRef == NULL) {
return;
}
struct list * lst = *headRef;
struct list * lprev = *headRef;
int i = 0;

while(lst && lst->field != index) {
lst = lst->next;
++i;
}
while(i > 1) {
lprev = lprev->next;
—i;
}

if(lst->field == index && lprev->field != lst->field) {
lprev->next = lst->next;
lst->next = lprev;
free(lst);
}
else if(lst->field == index && lst == lprev) {
*headRef = (*headRef)->next;
free(lst);
}
}

void ft_print_list(struct list * head) {
struct list * lst = head;
while(lst) {
printf("%d -> ", lst->field);
lst = lst->next;
}
printf("NULL.\n");
}

void ft_delete_list(struct list ** headRef) {
struct list * prev = *headRef;
while(prev) {
*headRef = (*headRef)->next;
printf("Deleting..\t[%d]\n", prev->field);
free(prev);
prev = *headRef;
}
printf("List was successfully deleted.\n");
}
int main(int args, char *argv[]) {
int arr_keys[args - 2];
struct list * head = NULL;
for(int i = 0; i < args - 1; ++i) {
arr_keys[i] = ft_atoi(argv[i + 1]);
}
for(int i = args - 2; i >= 0; —i) {
ft_push(&head, arr_keys[i]);
}
ft_print_list(head);
struct list * lst = NULL;
ft_push(&lst, 99);
ft_insert(&head, lst, 2);
ft_print_list(head);
ft_swap(&head, 12, 14);
ft_print_list(head);
ft_pop_n(&head, 2);
ft_print_list(head);
ft_delete_list(&head);
return(0);
}
Задание было такое:
односвязные списки.
Функции: добавление элемента в начало, конец, в n-ую позицию. Очистка всего списка, очистка n-ого элемента в списке.
Даю последние баллы!
Please enter comments
Please enter your name.
Please enter the correct email address.
You must agree before submitting.

Answers & Comments


Copyright © 2024 SCHOLAR.TIPS - All rights reserved.