An article to teach you to understand-a string of data structures

An article to teach you to understand-a string of data structures

①The definition of string type

The definition of string is much simpler than that of stack and queue. There are many functions for manipulating strings in the C language, and the corresponding function library is string.h. The development background of the string is given below to deepen the reader's impression. (Not the main point, you can skip it if you already have some understanding)

The object of non-numerical processing on the computer is basically string data. In earlier programming languages,
character strings appeared as constants for input and output. With the development of language processing programs, string processing has emerged.
In this way, character strings appear as a variable type in more and more programming languages, and a
series of character string operations are also produced . Strings are generally referred to as strings for short. In the assembly and language compiler, the source program and the target
program are all character string data. In the transaction processing procedure, the customer's name and address, as well as the name, place of origin, and specifications of the goods,
are generally treated as character strings. Another example is information retrieval systems, text editing programs, question and answer systems, natural language
translation systems, and music analysis programs, all of which use string data as the processing object.
However, the hardware structure of the computer we use nowadays mainly reflects the needs of numerical calculations. Therefore, processing
string data is much more complicated than processing integer and floating-point numbers. Moreover, in different types of applications, the processed
character strings have different characteristics. To effectively realize the character string processing, it is necessary to use a suitable
storage structure according to the specific situation . In this chapter, we will discuss some basic string manipulation operations and several different storage structures.

For the description of the string, this is the definition in the textbook. Although simple, readers should read it carefully.
Insert picture description here
Insert picture description here
Insert picture description here

② Representation and realization of string

For strings, most of the operations on character arrays can be done using functions in the C language function library. The following is based on the string, combined with the structure, and does not apply to the built-in functions of the string.h library. The author has implemented the operation algorithm of heap allocation for your reference, study and use. In the screenshots of the textbook below, the pseudo-code has explained the implementation process of the function in detail, so I won’t repeat it later.
Insert picture description here
Insert picture description here
Regarding the pattern matching algorithm and optimization algorithm about string later, readers who are interested can read and learn by themselves. The following is a screenshot of the operation of the above algorithm:
Insert picture description here

Finally the code

#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct string)
struct string{
    
    
	char *ch;
	int length;
};
int main()
{
    
    
	char s1[5]="123",s2[5]="456";
	struct string *string1,*string2,*string3,*string4;
	struct string *StrAssign(char *chars);	// 生成一个其值等于串常量的串str
	int StrLength(struct string *str);	// 返回str元素的个数,称为串的长度
	int StrCompare(struct string *str1,struct string *str2);	// 若str1大于str2,返回值>0;若str1大于str2,返回值<0;若相等,返回值=0
	void ClearString(struct string *str);	// 将str清为空串
	struct string *Concat(struct string *str1,struct string *str2);	// 将str1和str2合成新串
	void SubString(struct string *str,struct string *str2,int pos,int len);	// 用sub返回串str中第pos个字符起长度为len的字串

	// 测试
	string1 = StrAssign(s1);	// 初始化string1,string2,并将其ch的值分别赋为s1和s2
	string2 = StrAssign(s2);
	string4 = StrAssign(s1);	// 给string4的ch赋上初值
	printf("%s\n",string1->ch);	// 输出string1的ch字符串
	printf("%d\n",StrCompare(string1,string2));	// 比较string1和string2的ch字符串的大小
	string3 = Concat(string1,string2);	// 连接string1和string2
	printf("%s\n",string3->ch);
	SubString(string3,string4,1,2);	// 测试SubString函数
	printf("%s\n",string4->ch);
	ClearString(string1);	// 将string1清为空串

	return 0;	
}

struct string *StrAssign(char *chars)
{
    
    
	// 生成一个其值等于串常量的串str
	struct string *str;
	str = (struct string*)malloc(LEN);
	int i;
	for(i = 0;chars[i] != '\0';i++);
	str->length = i;
	str->ch = chars;
	return str;
}	

int StrLength(struct string *str)
{
    
    
	// 返回str元素的个数,称为串的长度
	return str->length;
}	

int StrCompare(struct string *str1,struct string *str2)
{
    
    
	// 若str1大于str2,返回值>0;若str1大于str2,返回值<0;若相等,返回值=0
	int i;
	for(i = 0;i < StrLength(str1) && i < StrLength(str2);i++){
    
    
		if(str1->ch[i] != str2->ch[i]){
    
    
			return str1->ch[i] - str2->ch[i];
		}
	}
	return str1->ch[i] - str2->ch[i];
}

void ClearString(struct string *str)
{
    
    
	// 将str清为空串
	str->ch = "";
	str->length = 0;
	printf("clear over!\n");
}	

struct string *Concat(struct string *str1,struct string *str2)
{
    
    
	// 将str1和str2合成新串
	struct string *str3;
	int i,j;
	str3 = (struct string*)malloc(LEN);
	str3->ch = str1->ch;
	str3->length = str1->length + str2->length;
	for(i = str1->length,j = 0;j < str2->length;i++,j++){
    
    
		str3->ch[i] = str2->ch[j];
	}
	str3->ch[i] = '\0';
	return str3;
}	

void SubString(struct string *str,struct string *str2,int pos,int len)
{
    
    
	// 用sub返回串str中第pos个字符起长度为len的字串
	int i,j;
	for(i = pos,j = 0;i < pos + len;i++,j++){
    
    
		str2->ch[j] = str->ch[i];
	}
	str2->ch[j] = '\0';
	str2->length = len;
}	

Guess you like

Origin blog.csdn.net/wlfyok/article/details/114241912