📄 link1.c
字号:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct{
char name[128];
long no;
char insti[128];
char classes[32];
}INFO;
struct LINK{
INFO data;
struct LINK *next;
};
int Getcommand(void);
struct LINK *Inputlink(void);
void Outputlink(struct LINK *);
struct LINK *Deletelink(struct LINK *,long n);
void Overlink(struct LINK *);
struct LINK *Oppolink(struct LINK *);
void Arrangelink(struct LINK *);
int Bubble(struct LINK *,int);
struct LINK *Joinlink(struct LINK *,struct LINK *);
void save(struct LINK *);
struct LINK *load(void);
main(){
int c;long num;char nums[5];
struct LINK *pHead=NULL,*pTail=NULL,*pSecond=NULL;
while(1){
c=Getcommand();
switch(c){
case 1:
pHead=Inputlink();
break;
case 2:
Outputlink(pHead);
break;
case 3:
printf("\nEnter the no.");
gets(nums);
num=atol(nums);
pHead=Deletelink(pHead,num);
break;
case 4:
pTail=Oppolink(pHead);
Outputlink(pTail);
break;
case 5:
Arrangelink(pHead);
printf("done");
break;
case 6:
pSecond=Inputlink();
pHead=Joinlink(pHead,pSecond);
Arrangelink(pHead);
printf("done");
break;
case 7:
save(pHead);
break;
case 8:
pHead=load();
break;
default:
Overlink(pHead);
Overlink(pTail);
Overlink(pSecond);
return 0;
}
}
}
int Getcommand(void){
char s[32];int a;
do{
printf("\n1:insert a student\n2:print the information\n3:delete a student\n4:print the information in opposite order\n5:arrange the informations with its no.\n6:input the second group and join them together\n7:save to info.txt\n8:load from info.txt\n0:exit");
printf("\n?");
gets(s);
a=atoi(s);
}while (a<0||a>8);
return a;
}
struct LINK *Inputlink(void){
char buf[256];
INFO x;
struct LINK *p=NULL,*q;
while(1){
gets(buf);
if (4!=sscanf(buf,"%s%ld%s%s",x.name,&x.no,x.insti,x.classes))
return p;
q=(struct LINK *)malloc(sizeof(struct LINK));
if (NULL==q)
return q;
q->data=x;
q->next=p;
p=q;
}
}
void Outputlink(struct LINK *p){
printf("\n%15s%10s%15s%10s","Name","No.","Institute","Class");
while (p!=NULL){
printf("\n%15s%10ld%15s%10s",p->data.name,p->data.no,p->data.insti,p->data.classes);
p=p->next;
}
}
struct LINK *Deletelink(struct LINK *p,long n){
struct LINK *q;
if (NULL==p){
printf("Deteled or not found");
return NULL;
}
if (0==(p->data.no-n)){
q=p;
p=p->next;
free (q);
}
else
p->next=Deletelink(p->next,n);
return p;
}
void Overlink(struct LINK *p){
struct LINK *q;
while(p!=NULL){
q=p;
p=p->next;
free (q);
}
}
struct LINK *Oppolink(struct LINK *p){
struct LINK *c=NULL,*b;
while(p!=NULL){
b=malloc(sizeof(struct LINK));
*b=*p;
b->next=c;
c=b;
p=p->next;
}
return c;
}
void Arrangelink(struct LINK *s){
int a=-1;
a=Bubble(s,a);
for(;a>0;a--)
Bubble(s,a);
}
int Bubble(struct LINK *Ptr,int n){
INFO x;
int a;
struct LINK *Current,*Then;
Current=Ptr;
Then=Current->next;
if (n=-1){
for (a=1;Then!=NULL;a++){
if (Current->data.no>Then->data.no){
x=Current->data;
Current->data=Then->data;
Then->data=x;
}
Current=Then;
Then=Then->next;
}
return a;
}
else{
a=n;
for (;Then!=NULL&&a>0;a--){
if (Current->data.no>Then->data.no){
x=Current->data;
Current->data=Then->data;
Then->data=x;
}
Current=Then;
Then=Then->next;
}
}
}
struct LINK *Joinlink(struct LINK *p,struct LINK *q){
struct LINK *c;
c=p;
for (;c->next!=NULL;c=c->next);
c->next=q;
return p;
}
void save(struct LINK *p){
FILE *ptr;
if ((ptr=fopen("info.txt","w"))==NULL)
printf("file could not open.");
while(p!=NULL){
fprintf(ptr,"%s %ld %s %s\n",p->data.name,p->data.no,p->data.insti,p->data.classes);
p=p->next;
}
fprintf(ptr,"over");
fclose(ptr);
}
struct LINK *load(void){
FILE *ptr;
struct LINK *p=NULL,*q;
INFO x;
if ((ptr=fopen("info.txt","r"))==NULL)
printf("file not exist.");
while(1){
if (4!=fscanf(ptr,"%s%ld%s%s",x.name,&x.no,x.insti,x.classes))
return p;
q=(struct LINK *)malloc(sizeof(struct LINK));
if (NULL==q)
return q;
q->data=x;
q->next=p;
p=q;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -