数据结构 ------链式串的基本操作

数据结构 ------链式串的基本操作:

这里采用串的链式存储结构,在明白了单链表的基本原理基础上,再理解串的链式存储以及相关操作就通俗易懂了。

这里要注意的是,p->next:指向第一个字符,刚开始创建的p为头结点,然后需要注意的地方就是这里的 r->next =q; r =q; 这里的意思是将q指针放在r->next的后面,接着r指针移动q指针的位置. 还有一种就是(q->next =r->next,r->next =q;)主要注意 q->next = r->next是指q指针指向的位置与r指针指向的位置一样.

代码如下:

#include <stdio.h>
#include <malloc.h>

typedef struct node{
	char data;
	struct node *next;
}linknode;  

//初始化 
void  initstr(linknode *&b, char str[])
{
	linknode  *p, *r;
	
	//头结点 
	b = (linknode *)malloc(sizeof(linknode));
	r = b;
	
	for(int i=0; i< str[i]!= '\0'; i++)
	{
		p = (linknode *)malloc(sizeof(linknode));
		p->data = str[i];
		
		p->next = r->next; 
		r->next =p;
		r=p;
	}
	r->next =NULL; //最后记得在最后面指向空 
}

//销毁串 
void destroystr(linknode *&b)
{ 
    //注意这里是由前往后删除 
	linknode *pre = b, *p =b->next;
	while(p!=NULL)
	{
		free(pre); //先删除前驱结点 
		pre = p;
		p = p->next;
	}
	free(pre);
}

//尾插法 
void strcpy(linknode *&b, linknode *s)
{
	linknode *p = s->next, *r, *q;
	
	b = (linknode *)malloc(sizeof(linknode));
	r = b;
	while(p!=NULL)
	{
		q= (linknode *)malloc(sizeof(linknode));
		q->data = p->data;
		
		p->next = r->next; 
		r->next = q;
		r = q;
		
		p = p->next;
	}
	r->next =NULL;
}

//判断两字符串是否相等 
bool strequal(linknode *b, linknode *s)
{
	linknode *p = b->next;
	linknode *r = s->next;
	
	while(p!=NULL && r!=NULL && p->data == r->data)
	{
		p = p ->next;
		r = r ->next;
	 } 
	 if(p == NULL && r == NULL)   return true;
	 else return false; 
 } 
 
 
 //求字符串的长度 
 int  strlength(linknode *s)
 {
 	int i =0;
 	linknode *p = s->next;
 	while(p!=NULL)
 	{
 		i++;
 		p = p->next;
	 }
	 return i;
 }
 
 //这里是将两个字符串连接起来 
linknode * strcon(linknode *s, linknode *t)
{
	linknode *str, *p =s->next, *r,*q;
	str = (linknode *)malloc(sizeof(linknode));
	
	//需要两个指针。 
	r = str;
	while(p!=NULL)
	{
		q = (linknode *)malloc(sizeof(linknode));
		q->data = s->data;
		q->next = r->next; 
		r->next = q;
		r = q;
		p = p->next;
	}
	
	p = t->next;
	while(p!=NULL)
	{
		q = (linknode *)malloc(sizeof(linknode));
		q ->data = t->data;
		q->next = r->next;
		r ->next = q;
		r =q;
		 p = p->next;
	}
	r = r->NULL;
	return str;
	
 } 
 
 //求子串
 
 linknode * sonstr(linknode *s, int i, int j) //第i个位置开始的j个字符 
 {
 	 linknode *strs=(linknode *)malloc(sizeof(linknode));
 	 strs->next =NULL;
 	 linknode *r,*p = s->next,*q;
 	 
 	 r =strs;
 	 
 	 for(int k = 0; k<i-1; k++)
       p = p->next;
       
     for(int k=0; k<j; k++)
     {
     	q = (linknode *)malloc(sizeof(linknode));
     	q ->data= p->data;
     	q->next = r->next;
     	r->next = q;
     	r =q;
     	p = p->next;
	 }
	 r->next =NULL;
	 return strs;
 }
  
//字符串内部插入
linknode * insertstr(linknode *a,int j ,linknode *b) //a中第j个位置开始插入 
{
	linknode *str,*q,*r;
	linknode  *p =a->next, *t =b->next;
	str = (linknode *)malloc(sizeof(linknode));
	str->next = NULL;
	
	r =str;
	
	if(j<0 && j > strlength(a)+1)//不加1就是插尾部 
	return NULL;
	
	for(int i=1; i<j; i++)
	 {
	 	q = (linknode *)malloc(sizeof(linknode));
	 	q ->data = p->data;
	 	q->next = r->next;
	 	r->next = q;
	 	r = q;
	 	p = p->next;
	 }
	 
	 while(t!=NULL)
	 {
	 	q =(linknode *)malloc(sizeof(linknode));
	 	q->data = t->data;
	 	q->next = r->next;
	    r->next =q;
	 	r= q;
	    t = t->next;
	 }
	 while(p!=NULL)
	 {
	 	q = (linknode *)malloc(sizeof(linknode));
		q ->data = p->data;
		q->next = r->next;
		r->next = q;
		r= q;
		p = p->next; 
	 }
	 r->next =NULL;
	 return str;
 }


//删除
linknode * delestr(linknode *s,int i ,int j) //从第i个位置开始的j个字符 
{
	linknode *str;
	linknode *r, *p= s->next,*q;
	
	str = (linknode *)malloc(sizeof(linknode));
	str->next =NULL;
	
	r =str;
	
	for(int k = 0; k<i-1; k++)
	{
		q = (linknode *)malloc(sizeof(linknode));
		q ->data = p->data;
		q->next = r->next;
		r->next = q;
		r =q;
		p = p->next;
	}
	for(int k =0; k< j; k++)
	    p = p->next;
	    
    while(p!=NULL)
    {
    	q = (linknode *)malloc(sizeof(linknode));
    	q->data = p->data;
    	q->next = r->next;
    	r ->next =q;
    	r = q;
    	p = p->next; 
	}
	r ->next =NULL;
	return str;
	
}

//替换 
 
 linknode * restr(linknode *s, int i, int j, linknode *t)//s串中第i个位置开始的连续j个字符用t字符串替换。 
 {
 	linknode *str =(linknode *)malloc(sizeof(linknode));
 	str->next = NULL;
 	linknode *p =s->next;
 	linknode *w =t->next;
 	linknode *r,*q;
 	r =str;
 	
    for(int k =0; k<i; k++)
    {
    	q = (linknode *)malloc(sizeof(linknode));
    	q ->data =p->data;
    	q->next = r->next;
    	r->next = q;
    	r =q;
    	p = p->next; 
	}
	for(int k=0; k<j-1; k++)
	{
		p = p->next;
	}
	
	
	while(w!=NULL)
	{
	  q =(linknode *)malloc(sizeof(linknode));
	  q -> data = w ->data;
	  q->next = r->next;
	  r->next = q;
	  r = q;
	  w = w->next; 
	}
	while(p!=NULL)
	{
		q =(linknode *)malloc(sizeof(linknode));
		q->data = p->data;
		q->next = r->next;
		r->next =q;
		r =q ;
		p= p->next; 
	}
	r->next =NULL;
 	return str;
 }
 
 
 //输出
 void show(linknode *s)
 {
 	linknode *p = s->next;
 	while(p!=NULL)
 	{
 		printf("%c",p->data);
 		p = p->next;
	 }
	 printf("\n"); 
  } 
 
 
int main()
{
	linknode *s,*b;
	char str1[] = "allllllal111111";
	char str2[] = "abcdefg";
	initstr(s,str1);
	show(s); 
	
	initstr(b,str2);
	show(b);
	
	int s1 = strlength(s);
	int s2 = strlength(b);

    linknode *str3 = insertstr(s,7,b);
    printf("\n将str2插入str1的第7位置开始:");
	show(str3);
	
	linknode *str4 = delestr(str3,7,s2);
	printf("\n在str3字符串中从第7个位置连续删除 %d 个字符: ",s2);
	show(str4); 
	
	linknode *str5 = restr(str4,5,s2,b);
	printf("\n在str4字符串从第5个位置开始用str2字符串连续替换 %d:  ",s2);
	show(str5);
	
	linknode *str6 = sonstr(str5,3,5);
	printf("\n在str5字符串中获取从第3个位置开始的连续5个字符: ");
	show(str6);
	return 0;
 } 

程序运行如下:
在这里插入图片描述

  • 4
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值