Created
April 17, 2020 01:16
-
-
Save leequixxx/f8d50a2cdf9dbb34ce11944e172d80ef to your computer and use it in GitHub Desktop.
Lab 01 Var 02
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
struct llist_node | |
{ | |
int value; | |
struct llist_node* next; | |
}; | |
int filter(int); | |
int main(int argc, char** argv) | |
{ | |
struct llist_node* begin = NULL; | |
struct llist_node* current = NULL; | |
while (1) | |
{ | |
int value; | |
printf("Please, enter next number (or 0 for stop): "); | |
scanf("%d", &value); | |
if (value == 0) | |
{ | |
break; | |
} | |
if (current == NULL) | |
{ | |
current = malloc(sizeof(struct llist_node)); | |
current->value = value; | |
current->next = NULL; | |
begin = current; | |
} | |
else | |
{ | |
current->next = malloc(sizeof(struct llist_node)); | |
current->next->value = value; | |
current->next->next = NULL; | |
current = current->next; | |
} | |
} | |
printf("Your numbers: "); | |
current = begin; | |
while (current) | |
{ | |
printf("%d ", current->value); | |
current = current->next; | |
} | |
printf("\n"); | |
current = begin; | |
while (current) | |
{ | |
if (filter(current->value) && (!current->next || filter(current->next->value))) | |
{ | |
current = current->next; | |
continue; | |
} | |
if (!filter(current->value) && current == begin) | |
{ | |
begin = current->next; | |
free(current); | |
current = begin; | |
continue; | |
} | |
struct llist_node* temp = current->next->next; | |
free(current->next); | |
current->next = temp; | |
} | |
printf("Your numbers after operation: "); | |
current = begin; | |
while (current) | |
{ | |
printf("%d ", current->value); | |
current = current->next; | |
} | |
printf("\n"); | |
return 0; | |
} | |
int filter(int value) | |
{ | |
return value % 2; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment