c语言实现基本的数据结构(六) 串
#include <stdio.h>
#include <tchar.h>
#include <stdlib.h> // TODO: 在此处引用程序需要的其他头文件 struct String{
char* ch;
int length;
}; bool Assign_String(String* str, char* chars);
bool Destroy_String(String* str);
bool Clear_String(String* str);
void Print_String(String str);
bool Insert_String(String* str, String T, int locate);
bool Copy_String(String* str, String T);
int Index_String(String str, String T); //给字符串重新赋值
bool Assign_String(String* str,char* chars){
if (str->ch) free(str->ch);
int i;
char* c;
for (i = , c = chars; *c; c++, i++);//获取字符串长度
if (!i) { str->ch = NULL; str->length = ; return true; }
else{
str->ch = (char*)malloc(i*sizeof(char));
for (int j = ; j < i; j++){
str->ch[j] = chars[j];
}
str->length = i;
return true;
}
}
//销毁字符串
bool Destroy_String(String* str){
if (str)
{
free(str);
str = NULL;
return true;
}
else
return false;
}
//清空字符串
bool Clear_String(String* str){
while (str->length){
str->ch[str->length] = NULL;
str->length--;
}
return true;
}
//打印
void Print_String(String str){
if (str.length)
{
for (int i = ; i < str.length;i++)
{
printf("%c", str.ch[i]); }
printf("\n");
}
}
//在原字符串str的第locate个元素前插入子字符串T
bool Insert_String(String* str, String T, int locate){
if (locate< || locate>str->length) return false;
if (T.length){
if (!(str->ch = (char*)realloc(str->ch,(str->length + T.length)*sizeof(char)))) exit(-);
for (int i = str->length - ; i >= locate; i--)
str->ch[i + T.length] = str->ch[i];
for (int i = ; i < T.length; i++)
str->ch[locate + i] = T.ch[i];
str->length += T.length;
}
return true;
}
//把T的值赋给str
bool Copy_String(String* str, String T){
if (str->ch) free(str->ch);
if (!&T) { str->ch = NULL; str->length = ; return true; }
else{
str->ch = (char*)malloc(T.length*sizeof(char));
for (int i = ; i < T.length; i++){
str->ch[i] = T.ch[i];
}
str->length = T.length;
}
return true;
}
//在原字符串str中搜索子字符串T的位置
//查找失败返回-1
//正常返回index为第一次出现T首字符的位置
int Index_String(String str, String T){
int index = -;
int key = ;
int Hl = ;
if (T.length&&str.length>=T.length){
for (int i = ; i <= str.length - T.length; i++){
if (str.ch[i] == T.ch[key]){
key++;
Hl++;
if (Hl >= T.length) { printf("出来了\n"); index = i - T.length+; return index; }
}
else{
Hl = key = ;
}
printf("i=%d,str是%c,T是%c,核对T的第%d个字符,已满足字符数%d\n", i, str.ch[i], T.ch[key], key, Hl);
}
}
return index;
}
c语言实现基本的数据结构(六) 串的更多相关文章
- hdu 3336:Count the string(数据结构,串,KMP算法)
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 数据结构(C语言版)-第4章 串、数组和广义表
补充:C语言中常用的串运算 调用标准库函数 #include<string.h> 串比较,strcmp(char s1,char s2) 串复制,strcpy(char to,char f ...
- 《数据结构与算法分析:C语言描述》复习——第六章“排序”——冒泡排序
2014.06.17 01:04 简介: 冒泡排序是O(n^2)级别的交换排序算法,原理简单,属于必知必会的基础算法之一. 思路: 排序要进行N轮,每一轮从尾部逐个向前扫描,遇到逆序对就进行交换.确保 ...
- Go语言备忘录:基本数据结构
本文内容是本人对Go语言的变量.常量.数组.切片.映射.结构体的备忘录,记录了关键的相关知识点,以供翻查. 文中如有错误的地方请大家指出,以免误导!转摘本文也请注明出处,多谢! 参考书籍<Go语 ...
- R语言入门 :基本数据结构
1.向量 向量是R语言中最基本的数据类型,在R语言中没有单独的变量. (1) 创建向量 R语言中可以用 = 或者 <- 来赋值. 向量名 <- 向量 或 向量名 = 向量 向量的创建方 ...
- C#数据结构之串
串(string)是n(n>=0)个字符组成的有限序列. 由于串中的字符都是连续存储的,在C#中有恒定不变的特性.一经创建就保持不变. 为了区别C#中的string,因此以stringDS类模拟 ...
- 算法与数据结构(六) 迪杰斯特拉算法的最短路径(Swift版)
上篇博客我们详细的介绍了两种经典的最小生成树的算法,本篇博客我们就来详细的讲一下最短路径的经典算法----迪杰斯特拉算法.首先我们先聊一下什么是最短路径,这个还是比较好理解的.比如我要从北京到济南,而 ...
- 【Java】 大话数据结构(8) 串的模式匹配算法(朴素、KMP、改进算法)
本文根据<大话数据结构>一书,实现了Java版的串的朴素模式匹配算法.KMP模式匹配算法.KMP模式匹配算法的改进算法. 1.朴素的模式匹配算法 为主串和子串分别定义指针i,j. (1)当 ...
- 数据结构(C语言)分享笔记:数据结构的逻辑层次、存储层次
[1] 严格意义上数据结构的概念 数据结构,一个简单的定义:相互之间存在一种或多种特定关系的数据元素的集合.即:数据结构 = 元素集合 + 元素间关系的集合 . 在讨论数据结构时,可以基于两个不同的层 ...
随机推荐
- [Linux] Ubuntu下载jdk 和 idea
分享一下Ubuntu如何配置JDK 并且下载Idea软件 配置JDK 1. 进入oracle:https://www.oracle.com/technetwork/java/javase/downlo ...
- 洛谷P4995 跳跳!题解
求关注,求赞,求评论QAQ 题目:https://www.luogu.org/problemnew/show/P4995 简单描述一下吧,就是说有n块石头,起始可以跳到任何一块上面,接着也是,只不过每 ...
- Excel催化剂开源第7波-VSTO开发中Ribbon动态加载菜单
在VS开发环境中,特别是VSTO的开发,微软已经现成地给开发者准备了设计器模式的功能区开发,相对传统的VBA.ExcelDna和其他方式的COM加载项开发来说,不需要手写xml功能区,直接类似拖拉窗体 ...
- springboot基础(随笔)
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot ...
- C# 针对特定的条件进行锁操作,不用lock,而是mutex
背景:用户领取优惠券,同一个用户需要加锁验证是否已经领取,不同用户则可以同时领取. 上代码示例: 1.创建Person类 /// <summary> /// Person类 /// < ...
- JAVA面试题 线程的生命周期包括哪几个阶段?
面试官:您知道线程的生命周期包括哪几个阶段? 应聘者: 线程的生命周期包含5个阶段,包括:新建.就绪.运行.阻塞.销毁. 新建:就是刚使用new方法,new出来的线程: 就绪:就是调用的线程的star ...
- vijos p1217 乒乓球
注意数组越界.#include<iostream> #include<cmath> using namespace std; char letter[10001]; void ...
- MyBatis在Spring环境下的事务管理
MyBatis的设计思想很简单,可以看做是对JDBC的一次封装,并提供强大的动态SQL映射功能.但是由于它本身也有一些缓存.事务管理等功能,所以实际使用中还是会碰到一些问题--另外,最近接触了JFin ...
- 10分钟了解分布式CAP、BASE理论
CAP理论 2000年7月,Eric Brewer教授提出CAP猜想:2年后,Seth Gilbert和Nancy Lynch从理论上证明了CAP:之后,CAP理论正式成为分布式计算领域的公认定理. ...
- 【Android】Error:Execution failed for task ':app:lint'
详细信息如下: Error:Execution failed for task ':app:lint'. > Lint found errors in the project; aborting ...