dir.main added

This commit is contained in:
ada-dmitry
2023-04-25 08:05:41 +03:00
parent 62d99ba675
commit 1f8e90cb3d
37 changed files with 1018 additions and 321 deletions
Binary file not shown.
+15 -15
View File
@@ -1,14 +1,14 @@
#include "head.h"
void linked_print(LinkedList* l) {
void linked_print(LinkList* l) {
while (l != NULL) {
printf("%d %s\n", l->x, l->name);
l = l->next;
}
}
LinkedList* add_tail(LinkedList* l, LinkedList node) {
LinkList* add_tail(LinkList* l, LinkList node) {
if (l->x == 0 && !strcmp(l->name, "\0")) {
l->x = node.x;
memcpy(l->name, node.name, LEN);
@@ -17,10 +17,10 @@ LinkedList* add_tail(LinkedList* l, LinkedList node) {
return l;
}
LinkedList* res = l;
LinkList* res = l;
while (l->next != NULL) l = l->next;
LinkedList* tmp = malloc(sizeof(LinkedList));
LinkList* tmp = malloc(sizeof(LinkList));
tmp->x = node.x;
memcpy(tmp->name, node.name, LEN);
tmp->prev = l;
@@ -28,7 +28,7 @@ LinkedList* add_tail(LinkedList* l, LinkedList node) {
return res;
}
LinkedList* add_head(LinkedList* l, LinkedList node) {
LinkList* add_head(LinkList* l, LinkList node) {
if (l->x == 0 && !strcmp(l->name, "\0")) {
l->x = node.x;
memcpy(l->name, node.name, LEN);
@@ -37,7 +37,7 @@ LinkedList* add_head(LinkedList* l, LinkedList node) {
return l;
}
LinkedList* tmp = malloc(sizeof(LinkedList));
LinkList* tmp = malloc(sizeof(LinkList));
if (tmp == NULL)
exit(1);
@@ -49,19 +49,19 @@ LinkedList* add_head(LinkedList* l, LinkedList node) {
return tmp;
}
LinkedList* del_head(LinkedList* l) {
LinkList* del_head(LinkList* l) {
if (l == NULL) return NULL;
if (l->next == NULL) {
free(l);
return NULL;
}
LinkedList* tmp = l->next;
LinkList* tmp = l->next;
tmp->prev = NULL;
free(l);
return tmp;
}
void del_tail(LinkedList* l) {
void del_tail(LinkList* l) {
if (l == NULL) return;
else if (l->next == NULL) {
free(l);
@@ -69,13 +69,13 @@ void del_tail(LinkedList* l) {
return;
}
while (l->next != NULL) l = l->next;
LinkedList* tmp = l->prev;
LinkList* tmp = l->prev;
l->prev = NULL;
free(l);
tmp->next = NULL;
}
int len(LinkedList* l) {
int len(LinkList* l) {
int res = 0;
if (l->x == 0 && !strcmp(l->name, "\0"))
return 0;
@@ -87,7 +87,7 @@ int len(LinkedList* l) {
return res;
}
LinkedList get(LinkedList* l, int n) {
LinkList get(LinkList* l, int n) {
if (n > len(l)) exit(3);
int i;
for (i = 0; i < n; i++) {
@@ -96,7 +96,7 @@ LinkedList get(LinkedList* l, int n) {
return *l;
}
void tower_of_hanoi(int n, LinkedList* start, LinkedList* finish, LinkedList* tmp) {
void tower_of_hanoi(int n, LinkList* start, LinkList* finish, LinkList* tmp) {
if (n == 1) {
finish = add_head(finish, get(start, 0));
start = del_head(start);
@@ -118,11 +118,11 @@ void tower_of_hanoi(int n, LinkedList* start, LinkedList* finish, LinkedList* tm
linked_print(tmp);
}
void linked_free(LinkedList* l) {
void linked_free(LinkList* l) {
while (l->next != NULL) l = l->next;
while (l->prev != NULL) {
LinkedList* tmp = l;
LinkList* tmp = l;
l = l->prev;
free(tmp);
}
+4 -4
View File
@@ -3,9 +3,9 @@
int main(int argc, char* argv[]) {
srand(time(0));
LinkedList* l1 = malloc(sizeof(LinkedList));
LinkedList* l2 = malloc(sizeof(LinkedList));
LinkedList* l3 = malloc(sizeof(LinkedList));
LinkList* l1 = malloc(sizeof(LinkList));
LinkList* l2 = malloc(sizeof(LinkList));
LinkList* l3 = malloc(sizeof(LinkList));
if (l1 == NULL || l2 == NULL || l3 == NULL)
exit(1);
@@ -17,7 +17,7 @@ int main(int argc, char* argv[]) {
s[j] = 'a' + rand() % 25;
s[j + 1] = '\0';
LinkedList l;
LinkList l;
l.x = i;
memcpy(l.name, s, LEN);
l.next = NULL;
+13 -13
View File
@@ -6,19 +6,19 @@
#define N 3
#define LEN 8
typedef struct LinkedList {
typedef struct LinkList {
int x;
char name[LEN];
struct LinkedList* next;
struct LinkedList* prev;
} LinkedList;
struct LinkList* next;
struct LinkList* prev;
} LinkList;
void linked_print(LinkedList* l);
LinkedList* add_tail(LinkedList* l, LinkedList node);
LinkedList* add_head(LinkedList* l, LinkedList node);
LinkedList* del_head(LinkedList* l);
void del_tail(LinkedList* l);
int len(LinkedList* l);
LinkedList get(LinkedList* l, int n);
void tower_of_hanoi(int n, LinkedList* start, LinkedList* finish, LinkedList* tmp);
void linked_free(LinkedList* l);
void linked_print(LinkList* l);
LinkList* add_tail(LinkList* l, LinkList node);
LinkList* add_head(LinkList* l, LinkList node);
LinkList* del_head(LinkList* l);
void del_tail(LinkList* l);
int len(LinkList* l);
LinkList get(LinkList* l, int n);
void tower_of_hanoi(int n, LinkList* start, LinkList* finish, LinkList* tmp);
void linked_free(LinkList* l);