mirror of
https://github.com/ada-dmitry/prog.l_ada.git
synced 2026-09-24 01:00:15 +00:00
555
This commit is contained in:
@@ -0,0 +1,56 @@
|
|||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
struct Node {
|
||||||
|
int data;
|
||||||
|
struct Node* next;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Node* top = NULL; // инициализация вершины стека как пустой
|
||||||
|
|
||||||
|
/* функция для добавления элемента в стек */
|
||||||
|
void push(int data) {
|
||||||
|
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
|
||||||
|
new_node->data = data;
|
||||||
|
new_node->next = top;
|
||||||
|
top = new_node;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* функция для удаления элемента из стека */
|
||||||
|
int pop() {
|
||||||
|
if (top == NULL) { // проверка на пустой стек
|
||||||
|
printf("Stack Underflow!");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
int data = top->data;
|
||||||
|
struct Node* temp = top;
|
||||||
|
top = top->next; // Спросить по поводу отсутствия эл-та
|
||||||
|
free(temp);
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* функция для просмотра последнего элемента стека */
|
||||||
|
int peek() {
|
||||||
|
if (top == NULL) { // проверка на пустой стек
|
||||||
|
printf("Stack is Empty!");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return top->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
printf("%d\n", peek());
|
||||||
|
push(1);
|
||||||
|
printf("%d\n", peek());
|
||||||
|
push(2);
|
||||||
|
printf("%d\n", peek());
|
||||||
|
push(3);
|
||||||
|
|
||||||
|
printf("%d\n", pop()); // выведет 3
|
||||||
|
printf("%d\n", peek()); // выведет 2
|
||||||
|
printf("%d", pop()); // выведет 2
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,172 @@
|
|||||||
|
#include "header.h"
|
||||||
|
|
||||||
|
|
||||||
|
void linked_print(Node* l) {
|
||||||
|
while (l != NULL) {
|
||||||
|
printf("%d %s\n", l->x, l->name);
|
||||||
|
l = l->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Node* add_tail(Node* 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
Node* res = l;
|
||||||
|
while (l->next != NULL) l = l->next;
|
||||||
|
|
||||||
|
Node* tmp = malloc(sizeof(Node));
|
||||||
|
tmp->x = x;
|
||||||
|
memcpy(tmp->name, name, STR_LEN);
|
||||||
|
tmp->prev = l;
|
||||||
|
l->next = tmp;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
Node* add_head(Node* 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
Node* tmp = malloc(sizeof(Node));
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
Node* del_head(Node* l) {
|
||||||
|
if (l == NULL) return NULL;
|
||||||
|
else if (l->next == NULL) {
|
||||||
|
free(l);
|
||||||
|
l = NULL;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
Node* tmp = l->next;
|
||||||
|
tmp->prev = NULL;
|
||||||
|
free(l);
|
||||||
|
l = NULL;
|
||||||
|
return tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
void del_tail(Node* l) {
|
||||||
|
if (l == NULL) return;
|
||||||
|
else if (l->next == NULL) {
|
||||||
|
free(l);
|
||||||
|
l = NULL;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
while (l->next != NULL) l = l->next;
|
||||||
|
Node* tmp = l->prev;
|
||||||
|
l->prev = NULL;
|
||||||
|
free(l);
|
||||||
|
tmp->next = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
int len(Node* l) {
|
||||||
|
int res = 0;
|
||||||
|
while (l != NULL) {
|
||||||
|
res++;
|
||||||
|
l = l->next;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
void swap(Node* 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);
|
||||||
|
}
|
||||||
|
Node* tmp = l;
|
||||||
|
|
||||||
|
for (i = 0; i < max - min; i++) {
|
||||||
|
if (l != NULL) l = l->next;
|
||||||
|
else exit(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
Node temp = *l;
|
||||||
|
l->x = tmp->x;
|
||||||
|
tmp->x = temp.x;
|
||||||
|
memcpy(l->name, tmp->name, STR_LEN);
|
||||||
|
memcpy(tmp->name, temp.name, STR_LEN);
|
||||||
|
}
|
||||||
|
|
||||||
|
Node get(Node* 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(Node* 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(Node* 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
Node* list_fscanf(char path[]) {
|
||||||
|
int tmp_x, i;
|
||||||
|
char s[STR_LEN] = {'\0'};
|
||||||
|
FILE* f = fopen(path, "r");
|
||||||
|
if (f == NULL)
|
||||||
|
exit(5);
|
||||||
|
|
||||||
|
Node* tmp = malloc(sizeof(Node)); //Дописать проверку на NULL
|
||||||
|
tmp->prev = NULL;
|
||||||
|
tmp->next = NULL;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
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);
|
||||||
|
} // Перенести в a_t
|
||||||
|
else add_tail(tmp, tmp_x, s);
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(f);
|
||||||
|
return tmp;
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
#define N 5
|
||||||
|
#define STR_LEN 8
|
||||||
|
|
||||||
|
typedef struct Node {
|
||||||
|
int x;
|
||||||
|
struct Node* next;
|
||||||
|
struct Node* prev;
|
||||||
|
char name[STR_LEN];
|
||||||
|
} Node;
|
||||||
|
|
||||||
|
void linked_print(Node* l);
|
||||||
|
Node* add_tail(Node* l, int x, char name[]);
|
||||||
|
Node* add_head(Node* l, int x, char name[]);
|
||||||
|
Node* del_head(Node* l);
|
||||||
|
void del_tail(Node* l);
|
||||||
|
int len(Node* l);
|
||||||
|
Node get(Node* l, int n);
|
||||||
|
|
||||||
|
void swap(Node* l1, int i, int j);
|
||||||
|
void select_sort(Node* l);
|
||||||
|
void list_fprintf(Node* l, char path[]);
|
||||||
|
Node* list_fscanf(char path[]);
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
#include "function.c"
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
srand(time(0));
|
||||||
|
Node* l = malloc(sizeof(Node));
|
||||||
|
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");
|
||||||
|
Node* l2 = list_fscanf("test.txt");
|
||||||
|
linked_print(l2);
|
||||||
|
|
||||||
|
free(l);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Executable
BIN
Binary file not shown.
@@ -0,0 +1,4 @@
|
|||||||
|
2 qjrjqtg
|
||||||
|
7 upivrdd
|
||||||
|
49 bokdbfv
|
||||||
|
74 hlchgxy
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
@@ -0,0 +1,171 @@
|
|||||||
|
#include "head.h"
|
||||||
|
|
||||||
|
void linked_print(Node* l) {
|
||||||
|
while (l != NULL) {
|
||||||
|
printf("%d %s\n", l->x, l->name);
|
||||||
|
l = l->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Node* add_tail(Node* 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
Node* res = l;
|
||||||
|
while (l->next != NULL) l = l->next;
|
||||||
|
|
||||||
|
Node* tmp = malloc(sizeof(Node));
|
||||||
|
tmp->x = x;
|
||||||
|
memcpy(tmp->name, name, STR_LEN);
|
||||||
|
tmp->prev = l;
|
||||||
|
l->next = tmp;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
Node* add_head(Node* 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
Node* tmp = malloc(sizeof(Node));
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
Node* del_head(Node* l) {
|
||||||
|
if (l == NULL) return NULL;
|
||||||
|
else if (l->next == NULL) {
|
||||||
|
free(l);
|
||||||
|
l = NULL;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
Node* tmp = l->next;
|
||||||
|
tmp->prev = NULL;
|
||||||
|
free(l);
|
||||||
|
l = NULL;
|
||||||
|
return tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
void del_tail(Node* l) {
|
||||||
|
if (l == NULL) return;
|
||||||
|
else if (l->next == NULL) {
|
||||||
|
free(l);
|
||||||
|
l = NULL;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
while (l->next != NULL) l = l->next;
|
||||||
|
Node* tmp = l->prev;
|
||||||
|
l->prev = NULL;
|
||||||
|
free(l);
|
||||||
|
tmp->next = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
int len(Node* l) {
|
||||||
|
int res = 0;
|
||||||
|
while (l != NULL) {
|
||||||
|
res++;
|
||||||
|
l = l->next;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
void swap(Node* 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);
|
||||||
|
}
|
||||||
|
Node* tmp = l;
|
||||||
|
|
||||||
|
for (i = 0; i < max - min; i++) {
|
||||||
|
if (l != NULL) l = l->next;
|
||||||
|
else exit(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
Node temp = *l;
|
||||||
|
l->x = tmp->x;
|
||||||
|
tmp->x = temp.x;
|
||||||
|
memcpy(l->name, tmp->name, STR_LEN);
|
||||||
|
memcpy(tmp->name, temp.name, STR_LEN);
|
||||||
|
}
|
||||||
|
|
||||||
|
Node get(Node* 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(Node* 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(Node* 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
Node* list_fscanf(char path[]) {
|
||||||
|
int tmp_x, i;
|
||||||
|
char s[STR_LEN] = {'\0'};
|
||||||
|
FILE* f = fopen(path, "r");
|
||||||
|
if (f == NULL)
|
||||||
|
exit(5);
|
||||||
|
|
||||||
|
Node* tmp = malloc(sizeof(Node)); //Дописать проверку на NULL
|
||||||
|
tmp->prev = NULL;
|
||||||
|
tmp->next = NULL;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
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);
|
||||||
|
} // Перенести в a_t
|
||||||
|
else add_tail(tmp, tmp_x, s);
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(f);
|
||||||
|
return tmp;
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
typedef struct Node {
|
||||||
|
int x;
|
||||||
|
struct Node* next;
|
||||||
|
struct Node* prev;
|
||||||
|
char name[STR_LEN];
|
||||||
|
} Node;
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
#include "func.c"
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user