综合本章例9.9(建立链表的函数creat)、例9.10(输出链表的函数print)和本章习题第7题(删除链表中结点的函数del)、第8题(插入结点的函数insert),再编写一个主函数,先后调用这些函数。用以上5个函数组成一个程序,实现链表的建立、输出、删除和插入,在主函数中指定需要删除和插人的结点的数据,并把最后生成的链表中的数据保存在out.dat文件中
#include <stdio.h>
#include <stdlib.h>
#define COUNT 5
typedef struct LNode
{
int num;
struct LNode *next;
} LNode;
LNode* create(int n)
{
LNode *head, *p;
head = (LNode *)malloc(sizeof(LNode)); //动态内存申请使指针变成变量
p = head; //头节点为0 加上头节点共11个节点
head->num = 0; //变量初始化
head->next = NULL;
for (int i = 1; i <= n; i++)
{
LNode *newNode = (LNode *)malloc(sizeof(LNode));
newNode->num = i;
newNode->next = NULL;
p->next = newNode;
p = p->next;
}
return head;
}
//在指定位置插入数据
void insert(int n, int positon, LNode *root)
{
//首先找到指定位置
while (positon--)
{
root = root->next;
}
//插入新的数据,重新链接插入点的前后节点关系
LNode *newNode = (LNode *)malloc(sizeof(LNode));
newNode->num = n;
newNode->next = root->next;
root->next = newNode;
}
void del(int n, LNode *root)
{
LNode *pre;
while (root->num != n)
{
pre = root;
root = root->next;
}
pre->next = root->next;
}
void printList(LNode *root)
{
printf("----\n");
int i = 0;
while (root != NULL)
{
printf("node %d -> %d\n", i, root->num);
root = root->next;
i++;
}
}
int main()
{
int n, position;
printf("请输入插入/删除的数,及插入的位置,位置最大为:%d\n", COUNT);
scanf("%d %d", &n, &position);
LNode *head = create(COUNT);
printList(head->next);
insert(n, position, head->next);
printList(head->next);
del(n, head->next);
printList(head->next);
return 0;
}
结尾保存文件的代码怎么写呀,各位友人
修改如下,供参考:
#include <stdio.h>
#include <stdlib.h>
#define COUNT 5
typedef struct LNode
{
int num;
struct LNode *next;
} LNode;
LNode* create(int n)
{
LNode *head, *p;
head = (LNode *)malloc(sizeof(LNode)); //动态内存申请使指针变成变量
p = head; //头节点为0 加上头节点共11个节点
head->num = 0; //变量初始化
head->next = NULL;
for (int i = 1; i <= n; i++)
{
LNode *newNode = (LNode *)malloc(sizeof(LNode));
newNode->num = i;
newNode->next = NULL;
p->next = newNode;
p = p->next;
}
return head;
}
//在指定位置插入数据
void insert(int n, int positon, LNode *root)
{
//首先找到指定位置
while (positon--)
{
root = root->next;
}
//插入新的数据,重新链接插入点的前后节点关系
LNode *newNode = (LNode *)malloc(sizeof(LNode));
newNode->num = n;
newNode->next = root->next;
root->next = newNode;
}
void del(int n, LNode *root)
{
LNode *pre;
while (root->num != n)
{
pre = root;
root = root->next;
}
pre->next = root->next;
}
void printList(LNode *root)
{
printf("----\n");
int i = 0;
while (root != NULL)
{
printf("node %d -> %d\n", i, root->num);
root = root->next;
i++;
}
}
void savetofile(LNode *root)
{
FILE *fp;
if((fp=fopen("out.dat","w"))==NULL){
printf("open file fail!\n");
}
else{
while(root){
fprintf(fp,"%5d",root->num);
root=root->next;
}
fclose(fp);
printf("save success.\n");
}
}
int main()
{
int n, position;
printf("请输入插入/删除的数,及插入的位置,位置最大为:%d\n", COUNT);
scanf("%d %d", &n, &position);
LNode *head = create(COUNT);
printList(head->next);
insert(n, position, head->next);
printList(head->next);
del(n, head->next);
printList(head->next);
savetofile(head->next);
return 0;
}
fopen打开文件,然后遍历链表,逐条写入文件就可以了
下面的代码是一个节点数据占一行
//写文件
void write2file(LNode* head)
{
FILE* fp;
LNode* p = head->next;
fp = fopen("out.dat","w");
while (p)
{
if(p==head->next)
fprintf(fp,"%d",p->num); //一个数据占一行
else
fprintf(fp,"\n%d",p->num);
p = p->next;
}
fclose(fp);
}
然后在main函数中调用即可:
int main()
{
//原来的代码
//保存到文件
write2file(head); //这里传入的是head
return 0;
}