int check(Hnode *Hhead,int m,int n) /*check():在单链表中检查第m行第n列位置的字符,若为常规字符,则返回该字符*/ {
int i;
Hnode *q; node *p;
q=Hhead;
for(i=1;i
p=q->next;/*获取第m个节点的数据域*/
for(i=1;i p=p->next; if(p->ch==13) return -1; /*若第m行,第n列的字符为回车键,则返回-1*/ if(p->ch>=32&&p->ch<127) return p->ch; /*若第m行,第n列的字符为常规字符,则返回该字符*/ else return 0; /*若第m行,第又非常规字符,则返回0*/ } int judge(Hnode *Hhead,int m) /*judge():返回第m行中的常规字符总的个数,不包括回车符*/ { Hnode *q; node *p; int i,num=0; q=Hhead; for(i=1;i q=q->nextl; if(q==NULL) return -1; /*返回-1,表示第m行不存在*/ p=q->next; while(p->next!=NULL) { p=p->next; num++; /*统计第m行的字符个数*/ } /*行尾字符还没有判断,接下来判断行尾字符*/ if(p->ch==13&&num==0) return 0; /*返回0,表示当前行只有一个回车字符*/ if(p->ch>=32&&p->ch<127) return num+1; /*返回num+1,表示当前行的最后一个字符为常规字符*/ if(p->ch==13&&num!=0) return num; /*返回num,表示当前行的最后一个字符为回车符,不计算在内*/ else return num;/*返回num,表示当前行中只有一个字符,且没有回车符*/ } int del(Hnode *Hhead,int m,int n) /*del():删除第m行,第n列位置的字符*/ { Hnode *q,*q1; node *p1,*p2,*tail; int i ,j,flag=0; q=Hhead; if(n==0&&m==1) return; /*第1行,第0列不存在*/ if(n==0&&m>1) /*若为第0列字符,但行必须大于1,执行向上行移处理*/ { m=m-1; gotoxy(judge(Hhead,wherey()-1)+1,m);/*移至第m-1行,第76列*/ flag=1; /*移位的标志置1*/ } for(i=1;i for(i=1;i p2=p1->next; /*p2指向列单链表中的第n个元素*/ if(n==1) /*若是删除第m行第1列的字符*/ { q->next=p1->next; free(p1); } else { p1->next=p2->next; /*在单链表中删除第m行第n列的元素*/ free(p2); } return flag; /*返回0:表示没有换位,返回1:表示有换位*/ } void insert(Hnode *Hhead,int m,int n, char a) /*第m行,第n列的位置之前一个位置,插入单字符*/ { int i; Hnode *q; node *p,*p1,*p2; q=Hhead; for(i=1;i p1=q->next; for(i=1;i p1=p1->next; p=(node *)malloc(sizeof(node)); /*创建一个新的列单链表节点*/ p->ch=a; /*给此节点的数据域赋值*/ if(n==1) /*插入之前,若只有一个字符在行中,则插在此节点之前*/ { p->next=q->next; q->next=p; } else { p->next=p1->next; /*在第m行,第n列的字符前,插入一字符*/ p1->next=p; } test(Hhead,m); /*在插入新元素后,检验并处理单链表中第m行开始的元素,使其满足规则*/ } /*执行insert()后,检验第n行及后面的数据,使其满足规则*/ int test(Hnode *Hhead,int n) { int i=0; node *p1,*p2,*tail,*temp1,*temp2; Hnode *q,*q1; q=Hhead; for(i=1;i q=q->nextl; } tail=p1=q->next; if(p1==NULL) { return ; /*若此行没有任何字符,则返回*/ } while(tail->next!=NULL) /*定位至列单链表中的最后一个元素*/ { tail=tail->next; } { p1=p1->next; } for(i=1;i<80;i++) p2=p1->next; p1->next=NULL; /*在此行的最后一个字符的前一个字符处断行,因为插入在此行插入了一个新的字符*/ if(tail->ch!=13) /*若此行行尾不是回车键*/ { if(q->nextl==NULL)/*若p1的数据域为回车符且行单链表中只有n个节点*/ q1=(Hnode *)malloc(sizeof(Hnode)); q1->nextl=NULL; q1->next=p2; /*新行单链表节点保存此行多出的字符*/ { q->nextl=q1; } else { q=q->nextl; tail->next=q->next;/*将多出的字符与下一行的字符相连*/ q->next=p2; test(Hhead,++n); } } else { q1=(Hnode *)malloc(sizeof(Hnode)); q1->nextl=q->nextl; q1->next=p2; q->nextl=q1; } } /*从任意文本文件中读取文件内容,保存至行单链表和列单链表形式的数据结构中*/ void opens(Hnode *Hp) { FILE* fp; Hnode *q11,*q22; node *p11,*p22,*hp; char temp; int count=0,flags=1; char filename[10]; /*保存文件名*/ clrscr();/*清屏*/ printf(\输入文件名格式*/ scanf(\输入文件名*/ fp=fopen(filename,\以只读方式打开文件,filename必须要存在*/ if(fp==NULL)/*打开文件失败*/ { textbackground(2); textcolor(13); cprintf(\ getchar(); exit(0) ; } q11=Hp; while(!feof(fp)) { count=0;flags=1; q22=(Hnode *)malloc(sizeof(Hnode));/*新建一个行单链表中的节点*/ p11=(node *)malloc(sizeof(node)); /*新建一个列单链表中的节点*/ while((temp=fgetc(fp))!=10&&count<=80&&!feof(fp)) /*循环结束,表示在单链表中一行处理完毕,开始新行*/ { p22=(node *)malloc(sizeof(node));/*新建一个列单链表中的节点*/ if(flags==1) { hp=p22; flags=0; } /*hp保存列单链表中的首节点的地址*/ p22->ch=temp; p22->next=NULL; p11->next=p22; p11=p22; count++; } if(temp==10) { /*若为换行符,将其转换为回车符,因为在程序中,是按回车符处理的*/ p22=(node *)malloc(sizeof(node)); p22->ch=13; p22->next=NULL; p11->next=p22; p11=p22; } if(!feof(fp))/*若没此条件,文件最后一行会处理两次.*/ { q22->next=hp; q22->nextl=NULL; /*将存储了字符的新列单链表与行单链表中的新节点建立关联*/ q11->nextl=q22; q11=q22; }