#include<stdio.h>
#include<stdlib.h>
typedef struct String{
char ch;
struct String *next;
}node,*string;
bool IsEmpty(string s)
{
if(s->next==NULL)
return true;
else
return false;
}
string CreatNullString()
{
string s;
s=(string)malloc(sizeof(node));
if(s!=NULL)
s->next=NULL;
return s;
}
void StringAssign(string s)
{
int i;
char ch[100];
printf("请输入字符串:\n");
gets(ch);
string p,r;
p=s;
for(i=0;ch[i]!='\0';i++)
{
r=(string)malloc(sizeof(node));
r->ch=ch[i];
p->next=r;
p=r;
p->next=NULL;
}
}
void putString(string s)
{
string p=s->next;
while(p)
{
printf("%c",p->ch);
p=p->next;
}
}
void insertString(string s)
{
char ch[100];
int i,k;
string q,r,p=s->next;
printf("\n请输入要插入的位置:");
scanf("%d",&i);
printf("请输入要插入的字符串:\n");
getchar();
gets(ch);
for(k=0;k<=i-1;k++)
{
q=p;
p=p->next;
}
for(k=0;ch[k]!='\0';k++)
{
r=(string)malloc(sizeof(node));
r->ch=ch[k];
r->next=p;
q->next=r;
q=r;
}
}
void DeleteString(string s)
{
string r,q,p=s;
int i,k,len;
printf("请输入删除起点及长度:\n");
scanf("%d %d",&i,&len);
for(k=0;k<=i-1;k++)
{
q=p;
p=p->next;
}
r=q;
for(k=0;k<len;k++)
{
r->next=p->next;
q=p;
p=p->next;
free(q);
}
}
int main()
{
string s1=CreatNullString();
StringAssign(s1);
putString(s1);
insertString(s1);
putString(s1);
DeleteString(s1);
putString(s1);
return 0;
}
串的链式存储
最新推荐文章于 2022-10-16 22:51:05 发布