第16天 对链串的学习

今天 冷静下来感觉状态 dai 得一批

主要是对串的基本操作

!!

 

代码如下----练习一会单发

#define ElemType char
#define MAXSIZE  500
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<windows.h>

//非紧缩格式顺序串类型说明
typedef struct lNode {
	ElemType date;
	struct lNode* next;
}LinkStrNode;

//生成串
void StrAssign(LinkStrNode*& s, ElemType 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->date = 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->date = r->date;
		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->date = p->next->date;
			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->date = r->next->date;
			//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->date);
		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;
	ElemType str1[] = "aaaaaa";
	ElemType str2[] = "ssssss";
	StrAssign(s, str1);
	StrAssign(e, str2);
	//InsStr(s, 2, e); //子串的插入
	//DelStr(s, 2, 4); //子串的删除
	RepStr(s, 2, 5, e);
	DisStr(s);
		
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值