#include "stdio.h" #include "stdlib.h" #include "string.h" #define SIZE 5; #define LEN sizeof(struct node) typedef struct node { char name[20]; struct node *next; } nodes; nodes *createNewNodes(void) { printf("创建新链表\n"); nodes *p; if ((p = (nodes *) malloc(sizeof(nodes))) == NULL) { printf("out of mem!"); exit(1); } p->name[0] = '\0'; p->next = NULL; return (p); } nodes *getLastNode(nodes *p) { nodes *s; s = p->next; while (s != NULL) { s = s->next; } return (s); } int getNodesNum(nodes *p) { int i = 0; p = p->next; while (p) { i++; p = p->next; } return i; } void printAll(nodes *p, nodes *lastSecond, int n) { nodes *t; if (p->next == NULL) { printf("当前链表没有节点\n"); } else { t = p->next; printf("=====链表地址情况:共%d个节点=====\n", n); while (t != NULL) { printf("数据:%s ", t->name); printf("地址:%p\n", t); t = t->next; } printf("倒数第二个元素为:%s\n", lastSecond->name); printf("============================\n"); } } nodes *testInit() { nodes *tmp, *p; p = createNewNodes(); int i; char *test[5] = { "abc", "def", "ghi", "jkl", "mnt" }; for (i = 0; i < 5; i++) { if ((tmp = (nodes *) malloc(sizeof(nodes))) == NULL) { printf("out of mem!"); exit(1); } strncpy(tmp->name, test[i], 20); tmp->next = p->next; p->next = tmp; } return (p); } void WriteNodesToFile(char *filename, nodes *p) { printf("把链表写入文件\n"); FILE *fp; if ((fp = fopen(filename, "wb")) == NULL) //打开文件名为:file_name的文件 { printf("can not open the file!\n"); } p = p->next; while (p != NULL) { fwrite(p, LEN, 1, fp); p = p->next; } fclose(fp); } int headAppend(nodes *p, char *name, nodes **lastSecond,int *nums) { printf("写入节点: %s\n", name); s = p; if ((new = (nodes *) malloc(sizeof(nodes))) == NULL) { printf("out of mem!"); exit(1); } strncpy(new->name, name, 20); new->next = s->next; s->next = new; if (*nums < 5){ *nums +=1; }else{ (*lastSecond)->next = NULL; } return 1; } void loadNodeFromFile(char *filename, nodes *head, nodes **lastSecond,int *nums) //尾插法 { printf("从文件读入链表\n"); FILE *fp; nodes *p, *t; int i = 0; if ((fp = fopen(filename, "rb")) == NULL) //打开文件名为:file_name的文件 { return;//文件不存在! } t = head; while (!feof(fp)) { if ((p = (nodes *) malloc(sizeof(nodes))) == NULL) { printf("out of mem!"); } if (!fread(p, LEN, 1, fp)) { break; } i++; *nums = i; *lastSecond = t; p->next = t->next; t->next = p; t = p; } fclose(fp); } int main(int argc, char **argv) { char *filename = "file_name.txt"; /*第二次的时候可以尝试从这里开始注释*/ /* nodes *p; printf("初始化链表: \n"); p=testInit();//初始化,这里是测试的,应该是从file文件读取结构存放到指针p printAll(p); headAppend(p,"opq");//加入一个节点 printAll(p); headAppend(p,"rst");//加入一个节点 printAll(p); //WriteNodesToFile(filename,p); free(p);*/ /*一直到这里*/ nodes *t = createNewNodes()/*建立一个链表并初始化*/; nodes *lastSecond;/*指向链表的倒数第二个元素*/ int nodeNum;/*链表个数*/ loadNodeFromFile(filename, t, &lastSecond, &nodeNum); printAll(t, lastSecond, nodeNum); //printf("%d",last->next==null); headAppend(t, "uvw",&lastSecond, &nodeNum);//加入一个节点 printAll(t, lastSecond, nodeNum); headAppend(t, "zxy",&lastSecond, &nodeNum);//加入一个节点 printAll(t, lastSecond, nodeNum); /* headAppend(t,"uvw");//加入一个节点 printAll(t); WriteNodesToFile(filename,t);*/ free(t); return 0; }