C语言怎样把链表存入文件,然后从文件读取到链表

树濑杨先生 树濑杨先生 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define MAX 200
typedef struct node{
int number;
char name[MAX];
int counter;
struct node *prior,*next;
}node;
node* head = NULL;
void output_one(node* n)
{
printf("%d\t%s\t%d\n", n->number, n->name, n->counter);
}
void output()
{
node* pos = head;
if(head == NULL)
{
return;
}
while (pos)
{
output_one(pos);
pos = pos->next;
}
}
int insert()
{
node* pos = head;
node* n = malloc(sizeof(node));
n->prior = NULL;
n->next = NULL;
printf("请输入商品编号:");
scanf("%d", &n->number);
printf("请输入商品名称:");
scanf("%s", &n->name);
printf("请输入商品数量:");
scanf("%d", &n->counter);
if(head==NULL)
{
head = n;
return 1;
}
while (pos)
{
if(pos->number > n->number)
{
if(pos->prior)
pos->prior->next = n;
n->prior = pos->prior;
pos->prior = n;
if(pos->next)
pos->next->prior = n;
n->next = pos;
return 1;
}
else if(pos->number == n->number)
{
free(n);
return 0;
}
if (!pos->next)
{
pos->next = n;
n->prior = pos;
return 1;
}
pos = pos->next;
}
return 1;
}
void init()
{
while (1)
{
insert();
printf("按任意键继续输入,按Esc停止输入\n");
if(getch()==27)
break;
}
}
int delete()
{
int num;
node* pos = head;
printf("请输入要删除的编号:");
scanf("%d", &num);

if(head == NULL)
{
return 0;
}
while (pos)
{
if(pos->number == num)
{
if(pos->prior)
pos->prior->next = pos->next;
if(pos->next)
pos->next->prior = pos->prior;
free(pos);
printf("出库完毕!");
return 1;
}
pos = pos->next;
}
return 0;
}
int amend()
{
int num, count;
node* pos = head;
printf("请输入要修改的编号:");
scanf("%d", &num);
printf("请输入要修改的数量:");
scanf("%d", &count);
if(head == NULL)
{
return 0;
}

while (pos)
{
if(pos->number == num)
{
if (count == 0)
{
if(pos->prior)
pos->prior->next = pos->next;
if(pos->next)
pos->next->prior = pos->prior;
free(pos);
return 1;
}
pos->counter = count;
return 1;
}
pos = pos->next;
}
return 0;
}
void search()
{
int num;
node* pos = head;
printf("请输入要查找的编号:");
scanf("%d", &num);
if(head == NULL)
{
return;
}
while (pos)
{
if(pos->number == num)
{
output_one(pos);
return;
}
else
{
printf("找不到该商品!\n");
}
pos = pos->next;
}
}
void search_by_name()
{
char name[MAX];
node* pos = head;
printf("请输入要查找的名称:");
scanf("%s", name);
if(head == NULL)
{
printf("找不到该商品!\n");
return;
}
while (pos)
{
if(!strcmp(pos->name, name))
{
output_one(pos);
}
pos = pos->next;
}
}
void jiemian()
{
printf(" 仓库管理系统\n");
printf(" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
printf(" ! 请选择: !\n");
printf(" ! 1.商品入库 !\n");
printf(" ! 2.商品出库 !\n");
printf(" ! 3.修改库存 !\n");
printf(" ! 4.商品查找 !\n");
printf(" ! 5.商品盘点 !\n");
printf(" ! 6.退出程序 !\n");
printf(" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
}
int chazhao(di)
{
char ch;
if(di==1)
{
init();
}
if(di==2)
{
delete();
}
if(di==3)
{
amend();
}
if(di==4)
{
printf("1. 按编号查找\n2. 按名称查找\n");
ch = getch();
if(ch == '1')
{
search();
}
else if(ch == '2')
{
search_by_name();
}
}
if(di==5)
{
output();
}
if(di==6)
{
exit(0);
}
return di;
}
int main()
{
int d;
jiemian();
scanf("%d",&d);
chazhao(d);
while (1)
{
char ch;
system("cls");
jiemian();
ch = getch();
switch (ch)
{
case '1':
init();
break;

case '2':
delete();
break;

case '3':
amend();
break;

case '4':
printf("1. 按编号查找\n2. 按名称查找\n");
ch = getch();
if(ch == '1')
{
search();
}
else if(ch == '2')
{
search_by_name();
}
break;

case '5':
output();
break;

case '6':
return 0;
break;
}
printf("按任意键继续...\n");
getch();
}
return 0;
}
...全文
评论 4
收藏 已收藏
分享
返回顶部
树濑杨先生
C语言 70,039 成员 243,237 内容