Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct node {
- int value;
- struct node* back;
- struct node* next;
- };
- struct node* init_node (int x) {
- struct node* n = malloc (sizeof (struct node));
- n->value = x;
- return n;
- }
- struct list {
- struct node* begin;
- struct node* end;
- int size;
- };
- struct list* init_list (void) {
- struct list* list = malloc (sizeof (struct list));
- list->size = 0;
- return list;
- }
- void destruct_list (struct list* list) {
- if (list->size == 0) {
- free (list);
- return;
- } else if (list->size == 1) {
- free (list->begin);
- free (list);
- return;
- }
- for (struct node* node = list->begin->next; node != list->end; node = node->next) {
- free (node->back);
- }
- free (list->end->back);
- free (list->end);
- free (list);
- }
- void push_back (struct list* list, int x) {
- struct node* new_node = init_node(x);
- if (list->size == 0) {
- list->begin = list->end = new_node;
- } else {
- list->end->next = new_node;
- new_node->back = list->end;
- list->end = new_node;
- }
- ++list->size;
- }
- void sort (struct list* list) {
- for (int i = 0; i < list->size; ++i) {
- struct node* node = list->begin;
- for (int j = 0; j < list->size - 1; ++j) {
- if (node->value > node->next->value) {
- node->value ^= node->next->value;
- node->next->value ^= node->value;
- node->value ^= node->next->value;
- }
- node = node->next;
- }
- }
- }
- int main (void) {
- FILE* in = fopen ("input.txt", "r");
- FILE* out = fopen ("output.txt", "w");
- struct list* list = init_list();
- while (1) {
- int x;
- fscanf(in, "%d", &x);
- if (feof(in)) break;
- push_back(list, x);
- }
- sort (list);
- struct node* node = list->begin;
- for (int i = 0; i < list->size; ++i) {
- fprintf(out, "%d ", node->value);
- node = node->next;
- }
- fclose(in);
- fclose(out);
- destruct_list(list);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement