类似的程序以前我们上个学期编过。
这不是我编的,是我同学编的,他编得好,给你看看。
你自己看下:
#include "stdio.h"
#include "alloc.h"
#include "dir.h"
/*功能:定义链节结构体*/
struct Chain
{
int data;
struct Chain *next;
};
typedef struct Chain CHAIN;
/*函数声明*/
CHAIN *Creat(void);
CHAIN *Reverse(void);
CHAIN *Delete(void);
CHAIN *Insert(void);
void Printf(CHAIN *H);
void Free(CHAIN *H);
void Save(CHAIN *H);
CHAIN *CreatFromFile(void);
FILE *OpenFile();
/*
函数功能:通过链表方式实现对数据的操作
入口参数:lscreat 实现创建链表
lsdelete 删除打开文件中的数据
lsreverse 反向打开文件中的数据
lsinsert 打开文件中插入数据
函数返回值:无
*/
main()
{
CHAIN *head;
char judge;
char fuction;/*记录功能*/
printf("The fuctions include:\n");
printf("Creat Insert Delete Reverse Lsdisplay\n");
printf("Enter the initial to realize relevant fuction:");
scanf("%c",&fuction);
/*选择执行程序*/
switch(fuction)
{
case 'c':
head=Creat();
break;
case 'd':
head=Delete();
break;
case 'r':
head=Reverse();
break;
case 'i':
head=Insert();
break;
case 'l':
head=CreatFromFile();
break;
default:
printf("Sorry!No such handle!");
exit(0);
}
if(fuction!='l')
printf("After revise,the chain is:\n");
Printf(head);
printf("\nDo you want to save it(Y or N)?");
/*判断是否保存数据,并执行*/
jud:
judge=getch();
if(judge=='y'||judge=='Y')
{
putch(judge);
Save(head);
}
else
{
if(judge=='n'||judge=='N')
{
putch(judge);
}
else
{
printf("\a");
goto jud;
}
}
Free(head);
printf("\n\nProgramme is over!Press any key to continue...");
getch();
}
/*
函数功能:创建链表
函数入口参数:无
函数返回值:结构体指针
*/
CHAIN *Creat(void)
{
CHAIN *H,*p,*q;
int n,i;
printf("Next we will creat a chain!\n");
printf("\nEnter the length of the chain:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the data of the %d node:",i+1);
p=(CHAIN *)malloc(sizeof(CHAIN *));
p->next=NULL;
scanf("%d",&p->data);
if(i==0)
{
H=p; /*处理第一链节*/
q=p;
}
else
{
q->next=p;
q=p;
}
}
return(H);
}
/*
函数功能:打开指定文件
函数入口参数:无
函数返回值:文件把柄
*/
FILE *OpenFile()
{
FILE *fpSrc;
char srcFilename[MAXPATH];
printf("\nThe source filename:");
scanf("%s",srcFilename);
fpSrc=fopen(srcFilename,"r");
if(fpSrc==NULL)
{
perror("Open file failed");
exit(0);/*打开错误,强行退出*/
}
return(fpSrc);
}
/*
函数功能:从文件中读取数据,并创建为链表
函数如口参数:无
函数返回值:结构体指针
*/
CHAIN *CreatFromFile(void)
{
CHAIN *p,*q,*H;
FILE *Fname;
int flag=0;
int ch;
Fname=OpenFile();
while((fscanf(Fname,"%d",&ch))!=EOF)
{
p=(CHAIN *)malloc(sizeof(CHAIN *));
p->next=NULL;
p->data=ch;
if(flag==0)
{
H=p;
q=p;
flag=1;
}
else
{
q->next=p;
q=p;
}
}
fclose(Fname);
return(H);
}
/*
函数功能:反向链表
函数如口参数:无
函数返回值:结构体指针
*/
CHAIN *Reverse(void)
{
CHAIN *p,*q;
FILE *fpSrc;
int ch;
fpSrc=OpenFile();
q=NULL;
while((fscanf(fpSrc,"%d",&ch))!=EOF)
{
p=(CHAIN *)malloc(sizeof(CHAIN *));
p->next=q;
p->data=ch;
q=p;
}
fclose(fpSrc);
return(q);
}
/*
函数功能:删除指定数据
函数如口参数:无
函数返回值:结构体指针
*/
CHAIN *Delete(void)
{
CHAIN *p,*q,*d,*H;
int x;
H=CreatFromFile();
printf("Enter the data that you want delete:");
scanf("%d",&x);
for(q=H,p=H->next;p!=NULL;q=p,p=p->next)
{
if(x==p->data)
{
d=p;
p=p->next;
q->next=p;
free(d);
break;
}
}
if(p!=NULL)
return(H);
else
{
printf("No such data!");
exit(0);/*数据不存在,强行退出*/
}
}
/*
函数功能:插入指定数据
函数如口参数:无
函数返回值:结构体指针
*/
CHAIN *Insert(void)
{
CHAIN *q,*d,*H;
FILE *fpSrc;
char judge;
int node;
int i=1;
H=CreatFromFile();
printf("\nIf you want to choose Y,\nPlease first make sure that the chain has been in order:");
printf("\nDo you want to insert it in order(Y or N)?");
/*判断插入方式*/
jud:
judge=getch();
if(judge=='y'||judge=='Y')/*排序插入*/
{
putch(judge);
printf("\nPlease enter what you want to insert:");
d=(CHAIN *)malloc(sizeof(CHAIN *));
scanf("%d",&d->data);
d->next=NULL;
if(d->data<H->data)
{
d->next=H;
H=d;
}
else
{
for(q=H;q->next!=NULL;q=q->next)
{
if(d->data<q->next->data)
{
d->next=q->next;
q->next=d;
break;
}
}
if(q->next==NULL) q->next=d;
}
}
else
{
if(judge=='n'||judge=='N')/*指定节插入*/
{
putch(judge);
printf("\nEnter before which node to insert:");
scanf("%d",&node);
printf("\nPlease enter what you want to insert:");
d=(CHAIN *)malloc(sizeof(CHAIN *));
scanf("%d",&d->data);
d->next=NULL;
if(node==0)
{
d->next=H;
H=d;
}
else
{
for(q=H;q->next!=NULL;q=q->next)
{
if(i==node)
{
d->next=q->next;
q->next=d;
break;
}
i++;
}
if(q->next==NULL) q->next=d;
}
}
else
{
printf("\a");
goto jud;
}
}
return(H);
}
/*
函数功能:打印链表
函数如口参数:结构体指针
函数返回值:无
*/
void Printf(CHAIN *H)
{
CHAIN *p;
p=H;
while(p!=NULL)
{
printf("%d\t",p->data);
p=p->next;
}
}
/*
函数功能:释放链表
函数如口参数:结构体指针
函数返回值:无
*/
void Free(CHAIN *H)
{
CHAIN *p;
p=H;
while(p!=NULL)
{
p=H;
H=H->next;
free(p);
}
}
/*
函数功能:保存链表
函数如口参数:结构体指针
函数返回值:无
*/
void Save(CHAIN *H)
{
char dstFilename[MAXPATH];
int ch;
FILE *fpDst;
CHAIN *q;
printf("\nEnter where do you want to save:");
scanf("%s",dstFilename);
fpDst=fopen(dstFilename,"w");
if(fpDst==NULL)
{
perror("Open file failed:");
exit(0);
}
for(q=H;q!=NULL;q=q->next)
{
ch=q->data;
if(fprintf(fpDst,"%d ",ch)==EOF)
{
perror("Save failed:");
fflush(fpDst);
exit(0);
}
}
printf("Save succeeded!");
if(fpDst!=NULL)
fclose(fpDst);
}