链表中文件的简单操作

#include<stdio.h>
#include<windows.h>
#include<string.h>

typedef struct student
{
int num;
char name[10];
struct student *next;
}stud;

//创建链表
stud *creat();
//输出到文本文件
void display(stud *h);
//按num查找
void search(stud *h,int number);
//插入函数
stud *Insert(stud *h);
//按链表节点删除
stud* Del(stud *h);
//按num排序
void sort(stud *h);
//主菜单
void menu(void)
{
system("cls");
printf("|==========================================\n");
printf("| student's information list |\n");
printf("|==========================================\n");
printf("| [1] creat linklist |\n");
printf("| [2] search |\n");
printf("| [3] insert |\n");
printf("| [4] delete |\n");
printf("| [5] printf |\n");
printf("| [6] sort |\n");
printf("| [7] display |\n");
printf("|==========================================|\n");
printf("please input your choose(1-6)\n");

}
void main()
{
stud *h=NULL;
int choose,num;
while(1)
{
menu();
scanf("%d",&choose);
switch(choose)
{
case 1:
system("cls");
h=creat();
puts("\nLinklist created successfully!");
system("pause");
getchar();
break;
case 2:
system("cls");
printf("Input the student's num which you want to find!\n");
scanf("%d",&num);
search(h,num);
puts("\npress any key to return...");
getchar();
getchar();
break;
case 3:
system("cls");
Insert(h);
display(h);
puts("\nPress any key to return...");
getchar();
getchar();
break;
case 4:
system("cls");
display(h);
Del(h);
display(h);
puts("\nDelete successfully! press any key to return...");
getchar();
getchar();
break;
case 5:
display(h);
printf("Store successful\n");
puts("\nPress any key to return...");
getchar();
getchar();
break;
case 6:
printf("排序中。。。\n");

sort(h);

system("pause");

printf("排序成功!\n");
break;
case 7:
exit(0);
break;
default:
system("cls");
puts("Illegal letter! Press any key to return...");
menu();
getchar();
}
}
}
stud *creat()
{
FILE *fp;
int n;
int i=0;
stud *h,*p,*s;
if((fp=fopen("D:\\stmessages.txt","a+"))==NULL)
{
printf("open error!");
exit (0);
}
puts("pleasa input Chain length of the table:");
scanf("%d",&n);
if((h=(stud *)malloc(sizeof(stud)))==NULL)
{
printf("no space!");
exit (0);
}
h->next=NULL;
p=h;
for(i=0;i<n;i++)
{
if((s=(stud *)malloc(sizeof(stud)))==NULL)
{
puts("no space!");
}
printf("\nplease input %d student's information:",i+1);
scanf("%d%s",&s->num,&s->name);
fprintf(fp,"%d\t%s\n",s->num,s->name);
p->next=s;
s->next=NULL;
p=s;
}
fclose(fp);
return h;
}
void search(stud *h,int number)
{
stud *p;
p=h->next;
puts("\nprintf search information of the search");
while(p!=NULL)
{
if(p->num==number)
{
printf("%d\t%s",p->num,p->name);
}
p=p->next;
}

}
stud* Insert(stud *h)
{
char InsertName[10];
int n,InsertNum;
int j=0;
stud *p,*q;
p=h;
puts("please input position of the insert、student's num and name\n");
scanf("%d%d%s",&n,&InsertNum,InsertName);
while(p && j<n-1)
{
p=p->next;
j++;
}
q=(stud *)malloc(sizeof(stud));
q->num=InsertNum;
strcpy(q->name,InsertName);
q->next=p->next;
p->next=q;
q=q->next;
return h;
}
stud * Del(stud *h)
{
stud *p,*q;
int n,j=0;
puts("please students's num of your delete:");
scanf("%d",&n);
p=q=h;
while(p && j<n-1)
{
p=p->next;
++j;
}
q=p->next;
p->next=q->next;
return h;
}
void display(stud *h)
{
FILE *fp;
stud *p;
if((fp=fopen("d:\\GTBL2.txt","w"))==NULL)
{
printf("cat open the file!");
exit(0);
}
p=h->next;
while(p!=NULL)
{

fprintf(fp,"%d\t%s\n",p->num,p->name);
p=p->next;
}
fclose(fp);
}
void sort(stud *h)
{
stud *first,*tail,*p_min,*min,*p;
first = NULL;
while (h != NULL)
{
for (p=h,min=h; p->next!=NULL; p=p->next)
{
if (p->next->num < min->num)
{
p_min = p;
min = p->next;
}
}
if (first == NULL)
{
first = min;
tail = min;
}
else
{
tail->next = min;
tail = min;
}
if (min == h)
{
h= h->next;
}
else
{
p_min->next = min->next;
}

}

display(first);

}
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值