mirror of
https://github.com/ada-dmitry/prog.l_ada.git
synced 2026-09-24 09:10:14 +00:00
restruct 1
This commit is contained in:
Binary file not shown.
@@ -0,0 +1,206 @@
|
||||
#include "head.h"
|
||||
|
||||
void linked_print(LinkedList *l)
|
||||
{
|
||||
while (l != NULL)
|
||||
{
|
||||
printf("%d %s\n", l->x, l->name);
|
||||
l = l->next;
|
||||
}
|
||||
}
|
||||
|
||||
LinkedList *add_tail(LinkedList *l, int x, char name[])
|
||||
{
|
||||
if (l->x == 0 && !strcmp(l->name, "\0"))
|
||||
{
|
||||
l->x = x;
|
||||
memcpy(l->name, name, STR_LEN);
|
||||
l->next = NULL;
|
||||
l->prev = NULL;
|
||||
return l;
|
||||
}
|
||||
|
||||
LinkedList *res = l;
|
||||
while (l->next != NULL)
|
||||
l = l->next;
|
||||
|
||||
LinkedList *tmp = malloc(sizeof(LinkedList));
|
||||
tmp->x = x;
|
||||
memcpy(tmp->name, name, STR_LEN);
|
||||
tmp->prev = l;
|
||||
l->next = tmp;
|
||||
return res;
|
||||
}
|
||||
|
||||
LinkedList *add_head(LinkedList *l, int x, char name[])
|
||||
{
|
||||
if (l->x == 0 && !strcmp(l->name, "\0"))
|
||||
{
|
||||
l->x = x;
|
||||
memcpy(l->name, name, STR_LEN);
|
||||
l->next = NULL;
|
||||
l->prev = NULL;
|
||||
return l;
|
||||
}
|
||||
|
||||
LinkedList *tmp = malloc(sizeof(LinkedList));
|
||||
if (tmp == NULL)
|
||||
exit(1);
|
||||
|
||||
tmp->next = l;
|
||||
l->prev = tmp;
|
||||
tmp->prev = NULL;
|
||||
tmp->x = x;
|
||||
memcpy(tmp->name, name, STR_LEN);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
LinkedList *del_head(LinkedList *l)
|
||||
{
|
||||
if (l == NULL)
|
||||
return NULL;
|
||||
else if (l->next == NULL)
|
||||
{
|
||||
free(l);
|
||||
l = NULL;
|
||||
return NULL;
|
||||
}
|
||||
LinkedList *tmp = l->next;
|
||||
tmp->prev = NULL;
|
||||
free(l);
|
||||
l = NULL;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
void del_tail(LinkedList *l)
|
||||
{
|
||||
if (l == NULL)
|
||||
return;
|
||||
else if (l->next == NULL)
|
||||
{
|
||||
free(l);
|
||||
l = NULL;
|
||||
return;
|
||||
}
|
||||
while (l->next != NULL)
|
||||
l = l->next;
|
||||
LinkedList *tmp = l->prev;
|
||||
l->prev = NULL;
|
||||
free(l);
|
||||
tmp->next = NULL;
|
||||
}
|
||||
|
||||
int len(LinkedList *l)
|
||||
{
|
||||
int res = 0;
|
||||
while (l != NULL)
|
||||
{
|
||||
res++;
|
||||
l = l->next;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
void swap(LinkedList *l, int first, int second)
|
||||
{
|
||||
if (first >= len(l) || second >= len(l))
|
||||
exit(3);
|
||||
int i, min, max;
|
||||
min = (first <= second) ? first : second;
|
||||
max = first + second - min;
|
||||
|
||||
for (i = 0; i < min; i++)
|
||||
{
|
||||
if (l != NULL)
|
||||
l = l->next;
|
||||
else
|
||||
exit(2);
|
||||
}
|
||||
LinkedList *tmp = l;
|
||||
|
||||
for (i = 0; i < max - min; i++)
|
||||
{
|
||||
if (l != NULL)
|
||||
l = l->next;
|
||||
else
|
||||
exit(2);
|
||||
}
|
||||
|
||||
LinkedList temp = *l;
|
||||
l->x = tmp->x;
|
||||
tmp->x = temp.x;
|
||||
memcpy(l->name, tmp->name, STR_LEN);
|
||||
memcpy(tmp->name, temp.name, STR_LEN);
|
||||
}
|
||||
|
||||
LinkedList get(LinkedList *l, int n)
|
||||
{
|
||||
if (n > len(l))
|
||||
exit(3);
|
||||
int i;
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
l = l->next;
|
||||
}
|
||||
return *l;
|
||||
}
|
||||
|
||||
void select_sort(LinkedList *l)
|
||||
{
|
||||
if (l == NULL || l->next == NULL)
|
||||
return;
|
||||
|
||||
int n = len(l), i, j;
|
||||
|
||||
for (i = 0; i < n - 1; i++)
|
||||
{
|
||||
for (j = i + 1; j < n; j++)
|
||||
{
|
||||
if (get(l, j).x < get(l, i).x)
|
||||
swap(l, i, j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void list_fprintf(LinkedList *l, char path[])
|
||||
{
|
||||
FILE *f = fopen(path, "w");
|
||||
if (f == NULL)
|
||||
exit(4);
|
||||
int i, n = len(l);
|
||||
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
fprintf(f, "%d %s\n", l->x, l->name);
|
||||
l = l->next;
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
LinkedList *list_fscanf(char path[])
|
||||
{
|
||||
FILE *f = fopen(path, "r");
|
||||
if (f == NULL)
|
||||
exit(5);
|
||||
|
||||
LinkedList *tmp = malloc(sizeof(LinkedList));
|
||||
tmp->prev = NULL;
|
||||
tmp->next = NULL;
|
||||
int tmp_x, i;
|
||||
char s[STR_LEN] = {'\0'};
|
||||
|
||||
for (i = 0; fscanf(f, "%d %s\n", &tmp_x, s) != EOF; i++)
|
||||
{
|
||||
if (i == 0)
|
||||
{
|
||||
tmp->x = tmp_x;
|
||||
memcpy(tmp->name, s, STR_LEN);
|
||||
}
|
||||
else
|
||||
add_tail(tmp, tmp_x, s);
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
return tmp;
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
#include "head.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
srand(time(0));
|
||||
LinkedList *l = malloc(sizeof(LinkedList));
|
||||
if (l == NULL)
|
||||
exit(1);
|
||||
|
||||
int i, j;
|
||||
for (i = 0; i < N; i++)
|
||||
{
|
||||
char s[STR_LEN];
|
||||
for (j = 0; j < STR_LEN - 1; j++)
|
||||
s[j] = 'a' + rand() % 25;
|
||||
s[j + 1] = '\0';
|
||||
add_tail(l, rand() % 100, s);
|
||||
}
|
||||
|
||||
printf("\nAfter add_tail(%d):\n", len(l));
|
||||
linked_print(l);
|
||||
|
||||
l = add_head(l, 5, "head");
|
||||
printf("\nAfter add_head(%d):\n", len(l));
|
||||
linked_print(l);
|
||||
|
||||
l = del_head(l);
|
||||
printf("\nAfter del_head(%d):\n", len(l));
|
||||
linked_print(l);
|
||||
|
||||
swap(l, 0, 1);
|
||||
printf("\nAfter swap first and second el:\n");
|
||||
linked_print(l);
|
||||
|
||||
del_tail(l);
|
||||
printf("\nAfter del_tail(%d):\n", len(l));
|
||||
linked_print(l);
|
||||
|
||||
select_sort(l);
|
||||
printf("\nAfter sort:\n");
|
||||
linked_print(l);
|
||||
|
||||
printf("\nWriting to file test.txt\n");
|
||||
list_fprintf(l, "test.txt");
|
||||
|
||||
printf("\nRead from file test.txt:\n");
|
||||
LinkedList *l2 = list_fscanf("test.txt");
|
||||
linked_print(l2);
|
||||
|
||||
free(l);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#define N 5
|
||||
#define STR_LEN 8
|
||||
|
||||
typedef struct LinkedList
|
||||
{
|
||||
int x;
|
||||
struct LinkedList *next;
|
||||
struct LinkedList *prev;
|
||||
char name[STR_LEN];
|
||||
} LinkedList;
|
||||
|
||||
void linked_print(LinkedList *l);
|
||||
LinkedList *add_tail(LinkedList *l, int x, char name[]);
|
||||
LinkedList *add_head(LinkedList *l, int x, char name[]);
|
||||
LinkedList *del_head(LinkedList *l);
|
||||
void del_tail(LinkedList *l);
|
||||
int len(LinkedList *l);
|
||||
LinkedList get(LinkedList *l, int n);
|
||||
|
||||
void swap(LinkedList *l1, int i, int j);
|
||||
void select_sort(LinkedList *l);
|
||||
void list_fprintf(LinkedList *l, char path[]);
|
||||
LinkedList *list_fscanf(char path[]);
|
||||
Reference in New Issue
Block a user