Task 2 is done
This commit is contained in:
parent
23eff9e484
commit
cd08e7ba3a
20 changed files with 453 additions and 0 deletions
83
2025.05.23/02Ex/io_node.c
Normal file
83
2025.05.23/02Ex/io_node.c
Normal file
|
@ -0,0 +1,83 @@
|
|||
#include "io_node.h"
|
||||
#include "node.h"
|
||||
#include "status.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int get_length (node * head)
|
||||
{
|
||||
node *curr;
|
||||
|
||||
int i = 0;
|
||||
for (curr = head; curr; curr=curr->next)
|
||||
i++;
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
void delete_list (node * head)
|
||||
{
|
||||
node *curr, *next;
|
||||
for (curr = head; curr; curr = next) {
|
||||
next = curr->next;
|
||||
free(curr->string);
|
||||
free(curr);
|
||||
}
|
||||
}
|
||||
|
||||
io_status read_list (node **list, const char *filename)
|
||||
{
|
||||
FILE *fp = 0;
|
||||
char *string, buf[LEN_STR];
|
||||
node *head, *lunit, *unit;
|
||||
|
||||
head = lunit = unit = NULL;
|
||||
|
||||
if (!(fp = fopen(filename, "r")))
|
||||
return ERR_OPEN;
|
||||
|
||||
while (fgets(buf, LEN_STR, fp))
|
||||
{
|
||||
unit = (node *)malloc(sizeof(node));
|
||||
if (!unit) {
|
||||
fclose(fp);
|
||||
delete_list(head);
|
||||
|
||||
return ERR_MEM;
|
||||
}
|
||||
|
||||
unit->next = NULL;
|
||||
|
||||
buf[strcspn(buf, "\n")] = '\0';
|
||||
string = strdup(buf);
|
||||
if (!string) {
|
||||
fclose(fp);
|
||||
free(unit);
|
||||
delete_list(head);
|
||||
|
||||
return ERR_MEM;
|
||||
}
|
||||
|
||||
unit->string = string;
|
||||
if (!lunit)
|
||||
head = unit;
|
||||
else
|
||||
lunit->next = unit;
|
||||
|
||||
lunit = unit;
|
||||
}
|
||||
|
||||
*list = head;
|
||||
|
||||
fclose(fp);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
void print_list (node *head, const int p)
|
||||
{
|
||||
for (int i = 0; head && i < p; head = head->next, i++)
|
||||
printf("%s\n", head->string);
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue