链串的基本操作源代码
#include "stdlib.h"
#include "stdio.h"
typedef struct lNode {
char data;
struct lNode* next;
}LinkStrNode;
//生成串
void StrAssign(LinkStrNode*& s, char str[])
{
LinkStrNode *r,*p;
s = (LinkStrNode *)malloc(sizeof(LinkStrNode));
r = s;
r->next = NULL;
for (int i = 0; str[i] != '\0'; ++i)
{
p = (LinkStrNode*)malloc(sizeof(LinkStrNode));
p->data = str[i];
p->next = r->next;
r->next = p;
r = p;
}
//r->next = NULL;
}
//销毁串
void DestroyStr(LinkStrNode*& s)
{
LinkStrNode* r = s, * p = s->next;
for (; p != NULL;)
{
free(r);
r = p;
p = p->next;
}
free(r);
}
//串的复制
void StrCopy(LinkStrNode*& s, LinkStrNode *t)
{
LinkStrNode* r=t->next,*p;
s = (LinkStrNode*)malloc(sizeof(LinkStrNode));
p = s;
for (; r != NULL;)
{
LinkStrNode* w;
w = (LinkStrNode*)malloc(sizeof(LinkStrNode));
w->data = r->data;
p->next = w;
p= w;
r = r->next;
}
p->next = NULL;
}
//判断串是否相等---strcmp
//求串长-----------strlen
int StrLen(LinkStrNode* s)
{
int i = 0;
LinkStrNode* r = s->next;
while (r != NULL)
{
i++;
r = r->next;
}
return i;
}
//串的连接---------strcat
//求子串
LinkStrNode *SubStr(LinkStrNode*& s, int i, int j)
{
LinkStrNode* r,*p;
p = s->next;
r = (LinkStrNode*)malloc(sizeof(LinkStrNode));
r->next = NULL;
if (s->next == NULL || i > j || i<0 || j>StrLen(s))
{
return r;
}
int k=0;
do
{
k++;
if (k >= i)
{
LinkStrNode* q;
q = (LinkStrNode*)malloc(sizeof(LinkStrNode));
q->data = p->next->data;
q->next = r->next;
r->next = q;
r = q;
}
p = p->next;
} while (k!=j);
return r;
}
//子串的插入
LinkStrNode* InsStr(LinkStrNode*& s1, int i, LinkStrNode* s2)
{
LinkStrNode* r=s1->next,*p;
p = (LinkStrNode*)malloc(sizeof(LinkStrNode));
p->next = NULL;
if (s1 == NULL || i < 0)
{
return p;
}
int j = 0;
LinkStrNode *e1=s2,*e2=s2->next;
do
{
e2 = e2->next;
} while (e2->next != NULL);
do
{
j++;
if (j == i)
{
//LinkStrNode* w;
//w = (LinkStrNode*)malloc(sizeof(LinkStrNode));
//w->data = r->next->data;
//w->next = r->next;
//r->next = w;
//r = w;
e2 ->next= r->next->next;
r->next = s2->next;
break;
}
r = r->next;
} while (s2->next!=NULL);
return s1;
}
//输出串
void DisStr(LinkStrNode* s)
{
LinkStrNode* r = s->next;
int i = 0;
while (r != NULL)
{
i++;
printf("第%d个元素: %c\n", i, r->data);
r = r->next;
}
}
//子串的删除
LinkStrNode* DelStr(LinkStrNode* s, int i, int j)
{
LinkStrNode* r;
r = (LinkStrNode*)malloc(sizeof(LinkStrNode));
r->next = NULL;
if (i > j || i<0 || j>StrLen(s) || s->next == NULL)
{
return r;
}
int k = 0;
r = s;
LinkStrNode* w;
w = (LinkStrNode*)malloc(sizeof(LinkStrNode));
do
{
k++;
if (k == i)
{
w = r;
}
r = r->next;
} while (k!=j);
LinkStrNode* p;
p = r->next->next;
w->next = p;
return r;
}
//子串的替换
LinkStrNode* RepStr(LinkStrNode* s, int i, int j, LinkStrNode* e)
{
LinkStrNode* r;
r = (LinkStrNode*)malloc(sizeof(LinkStrNode));
if (i > j || i<0 || j>StrLen(s))
{
r->next = NULL;
return r;
}
LinkStrNode* w,*q;
w = (LinkStrNode*)malloc(sizeof(LinkStrNode));
q= (LinkStrNode*)malloc(sizeof(LinkStrNode));
r = s;
int k = 0;
do
{
k++;
if (k == i)
{
w = r;
}
r = r->next;
} while (k!=j);
q = e->next;
int a = 0;
while (q != NULL)
{
a++;
q = q->next;
}
if (a == 0)
{
w->next = r->next->next;
}
else
{
w->next = e->next;
q = w->next->next;
}
return s;
}
int main()
{
LinkStrNode* s, * e;
char str1[] = "aaaaaa";
char str2[] = "ssssss";
StrAssign(s, str1);
StrAssign(e, str2);
//InsStr(s, 2, e); //子串的插入
//DelStr(s, 2, 4); //子串的删除
RepStr(s, 2, 5, e);
DisStr(s);
}