#include "linklist.h"
link_p createHead()
{
link_p H = (link_p)malloc(sizeof(link));
if (H == NULL)
{
printf("空间申请失败\n");
return NULL;
}
H->next = NULL;
H->len = 0;
return H;
}
link_p createNode(int data)
{
link_p node = (link_p)malloc(sizeof(link));
if (node == NULL)
{
printf("空间申请失败\n");
return NULL;
}
node->next = NULL;
node->data = data;
return node;
}
void insertHead(link_p H,int data)
{
if (H == NULL)
{
printf("入参为空,请检查\n");
return;
}
link_p node = createNode(data);
node->next = H->next;
H->next = node;
H->len++;
}
void insertTail(link_p H,int data)
{
if (H == NULL)
{
printf("入参为空,请检查\n");
return;
}
link_p node = createNode(data);
link_p current = H;
while (current->next != NULL)
current = current->next;
node->next = current->next;
current->next = node;
H->len++;
}
void outPutDataToFile(link_p H, char *pathname)
{
FILE* fp = fopen(pathname, "w");
if (fp == NULL)
{
perror("");
return;
}
link_p current = H;
while (current->next != NULL)
{
current = current->next;
fprintf(fp,"%d ",current->data);
}
}
link_p readDataFromFile(char* pathname)
{
FILE* fp = fopen(pathname, "r");
if (fp == NULL)
{
perror("readDataFromFile");
return NULL;
}
link_p H = createHead();
int tmp;
while (1)
{
int ret = fscanf(fp,"%d",&tmp);
if (ret != 1)
break;
insertTail(H, tmp);
}
//outPut(H);
return H;
}
void outPut(link_p H)
{
if (H == NULL)
{
printf("入参为空,请检查\n");
return;
}
link_p current = H;
while ((current = current->next) != NULL)
printf("%d\t", current->data);
printf("\n");
}