经过了一些修改,已经能完成基本操作了。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define chunksize 8
#define null 0
typedef struct chunk{
char ch[chunksize];
struct chunk *next;
}chunk;
typedef struct{
chunk *head,*tail;
int curlen;
}lstring;
//清空
void clear(lstring *s){
chunk *t,*p;
int i = 0;
t = s->head;
printf("当前块链长度为:%d\n",s->curlen);
for( ; s->head != s->tail ; i++){
p = t->next;
s->head = p;
free(t);
t = p;
}
free(s->head);
s->head = s->tail = null;
s->curlen = 0;
printf("清空成功,释放了%d个节点。\n",i+1);
printf("清空后块链长度为:%d\n",s->curlen);
}
//压缩清空
void clear1(lstring *s){
chunk *t,*p;
t = s->head;
for( ; s->head != s->tail ; ){
p = t->next;
s->head = p;
free(t);
t = p;
}
free(s->head);
s->head = s->tail = null;
s->curlen = 0;
}
//插入节点
void add0(lstring *s,char *a){
chunk *p;
p = (chunk *)malloc(sizeof(chunk));
s->head = s->tail = p;
for(int i = 0 ; i<chunksize ; i++){
p->ch[i] = a[i];
}
}
void add1(lstring *s,char *a){
chunk *p;
p = (chunk *)malloc(sizeof(chunk));
for(int i = 0 ; i<chunksize ; i++){
p->ch[i] = a[i];
}
s->tail->next = p;
s->tail = p;
}
//插入s块链
void strinsert(lstring *t,lstring s){
int a,long1,long2,aaa=0,cc,hpu,ruler;
char a1[] = {"########"},a2[] = {"########"};
chunk *lc,*ly,*lz,*lw,*lm;
lc = (chunk *)malloc(sizeof(chunk));
ly = (chunk *)malloc(sizeof(chunk));
lz = (chunk *)malloc(sizeof(chunk));
lm = lw = t->head ;
ruler = t->curlen +1;
printf("请输入您想插入到第几个字符前:(1~%d)\n",t->curlen+1);
scanf("%d",&a);
hpu = a;
if(a>0&&a<t->curlen+2)
{t->curlen += s.curlen;
a -= 1;
long1 = a/chunksize+1;
long2 = a%chunksize;
for(int i = 0 ;long1>2&&i < long1 - 2 ; i++){
lz = lm->next;
lm = lm->next;
}
for(int i = 0 ; i < long1 - 1 ; i++){
lc = lw->next;
lw = lw->next ;
}
//for(int i=0;i<8;i++)
//printf("%c\n",lz->ch[i]);
//for(int i=0;i<8;i++)
//printf("%c\n",lc->ch[i]);
if(!(long1-1))
lc = t->head;
for(int j = 0 ; j < long2 ; j++)
{a1[j] = lc->ch[j];
aaa++;}
cc = aaa;
for(int j = 0 ; j < 8-cc ; j++)
{a2[j] = lc->ch[aaa];
aaa++;}
for(int w = 0 ; w<8 ; w++)
lc->ch[w] = a1[w];
ly->next = lc->next;
lc->next = ly;
for(int w = 0 ; w<8 ; w++)
ly->ch[w] = a2[w];
if(t->head == t->tail)
t->tail=ly;
if(lc == t->tail)
t->tail = ly;
if(hpu==1){
s.tail->next = ly;
t->head = s.head;
}
else if(hpu==9){
s.tail ->next = ly;
t->head->next =s.head ;
}
else if(hpu%8==1){
s.tail->next = ly ;
lz->next = s.head ;
}
else{
s.tail->next= lc->next;
lc->next=s.head;
}
if(hpu==ruler){
t->tail = s.tail ;
s.tail->next = null;
free(ly);
}
}
else
{printf("超范围了,请重新输入\n");
strinsert(t,s); }
}
//将数组中的元素插入块链
void strassign(lstring *s,char *str){
int width,long1,long2,aaa=0;
chunk *t,*p;
char a[chunksize];
t=(chunk *)malloc(sizeof(chunk));
p=(chunk *)malloc(sizeof(chunk));
width=strlen(str);
s->curlen=width;
long1=width/chunksize;
long2=width%chunksize;
if(long1!=0){
for(int i=0;i<long1;i++){
for(int j=0;j<chunksize;j++){
a[j]=str[aaa];
aaa++;
}
if(i==0)
add0(s,a);
else
add1(s,a);
}
if(long2){
for(int i=0;i<chunksize;i++){
if(i<long2)
{a[i]=str[aaa];
aaa++;}
else
a[i]='#';
}
add1(s,a);
}
}
else
{for(int i=0;i<chunksize;i++){
if(i<long2)
{a[i]=str[aaa];
aaa++;}
else
a[i]='#';
}
add0(s,a);}
}
//删除#号
void zip(lstring *t){
chunk *p;
char bbbb[100];
int i=0;
p=t->head;
while(!(p==t->tail)){
for(int j=0;j<8;j++){
if(p->ch[j]!='#')
{bbbb[i]=p->ch[j];
i++;}
}
p=p->next;
}
for(int j=0;j<8;j++){
if(p->ch[j]!='#')
{bbbb[i]=p->ch[j];i++;}}
bbbb[i]='\0';
clear1(t);
strassign(t,bbbb);
}
//显示块链
void appear(lstring s){
chunk *t;
t=s.head;
while(!(t==s.tail)){
for(int jj=0;jj<8;jj++){
printf("%c",t->ch[jj]);
}
printf(" ");
t=t->next;
}
if(s.curlen){
for(int jj=0;jj<8;jj++){
printf("%c",t->ch[jj]);
}}
else
printf("块链内无元素");
printf("\n");
}
int main(){
char str[100],str1[30];
int ruler;
lstring t,s;
printf("1:显示块链\n2:清空块链\n3:插入块链\n4:压缩块链\n5:清空原块链后,创建一个新块链\n\n");
printf("\n----------------------------------------\n\n");
printf("请输入想创建串内的元素(不包括#):\n");
gets(str);
strassign(&t,str);
printf("\n创建成功,创建后的块链为:\n");
appear(t);
printf("\n----------------------------------------\n\n");
do{
printf("请输入你想操作的数字:");
scanf("%d",&ruler);
if(ruler==1)
appear(t);
else if(ruler==2)
clear(&t);
else if(ruler==3)
{if(t.curlen){
printf("输入您想插入的字符串:\n");
fflush(stdin);
gets(str1);
strassign(&s,str1);
strinsert(&t,s);
printf("\n插入成功,插入后的块链为:\n");
appear(t);
}
else
printf("主串内无元素,无法插入\n");
}
else if(ruler==4)
{zip(&t);
printf("\n压缩成功,压缩后的块链为:\n");
appear(t);}
else
{printf("请输入想新创建串内的元素(不包括#):\n");
fflush(stdin);
gets(str);
strassign(&t,str);
printf("\n新创建成功,创建后的块链为:\n");
appear(t);}
printf("\n----------------------------------------\n\n");
}while(ruler);
}