串的块链存储结构解决不了
#include<stdio.h>#include<malloc.h>
#include<stdlib.h>
#include<String.h>
#define CHUNKSIZE 4
char blank='#';//用于填补空余
typedef struct Chunk
{
char ch[CHUNKSIZE];
struct Chunk *next;
}Chunk;
typedef struct
{
Chunk *head,*tail;//串的头和尾指针
int curlen; // 串的当前长度
}LString;
void InitLString(LString &L)
{
L.curlen=0;
L.head=NULL;
L.tail=NULL;
}
int StrAssgin(LString &L,char *chars)
{
int i,j,k,l;
Chunk *p,*q;
i=strlen(chars);
if(!i || strchr(chars,blank))
return 0;
L.curlen=i;
j=i/CHUNKSIZE;
if(i%CHUNKSIZE)
j++;
for(k=0;k<j;k++)
{
p=(Chunk *)malloc(sizeof(Chunk));
if(!p)
return 0;
if(k==0) //第一个块链
L.head=q=p;
else
{
q->next=p;
q=p;
}
for(l=0;l<CHUNKSIZE&&*chars;l++)
*(q->ch+1)=*chars++;
if(!*chars)
{
L.tail=q;
q->next=NULL;
for(;l<CHUNKSIZE;l++)
*(q->ch+1)=blank;
}
}
return 1;
}
void Print(LString &L)
{
int i=0,j;
Chunk *h;
h=L.head;
while(i<L.curlen)
{
for(j=0;j<CHUNKSIZE;j++)
if(*(h->ch+j)!=blank)
{
printf("%c",*(h->ch+j));
i++;
}
h=h->next;
}
printf("\n");
}
int main()
{
char *s1="ABCDEFGHI",*s2="123456",*s3="";
int k;
LString L;
InitLString(L);
k=StrAssgin(L,s1);
if(k==1)
{
printf("串L为:");
Print(L);
}
else
printf("出错了\n");
return 0;
}