search
HomeCommon ProblemIs a string a linear list with special data objects and operations?

Yes, string is a linear list structure with special data objects and operations. The string mentioned in the data structure is a string; the characters in the string have a "one-to-one" logical relationship, so strictly speaking, the string storage structure is a linear storage structure.

Is a string a linear list with special data objects and operations?

The operating environment of this tutorial: Windows 7 system, Dell G3 computer.

The string mentioned in the data structure, that is, a string, is a whole composed of n characters (n >= 0). These n characters can be composed of letters, numbers, or other characters.

In the data structure, strings must be stored in a separate storage structure, which is called a string storage structure.

Strictly speaking, the string storage structure is also a linear storage structure, because the characters in the string also have a "one-to-one" logical relationship. However, unlike the linear storage structure we learned before, the string structure is only used to store character type data.

Special strings

  • Empty string: A string containing zero characters. For example: S = "" (nothing in double quotes), usually expressed directly as Ø.

  • Space string: A string containing only spaces. Note that it is different from the empty string. There is content in the space string, but it contains spaces, and the space string can contain multiple spaces. For example, a = " " (contains 3 spaces).

  • Substring and main string: A string composed of any consecutive characters in the string is called a substring of the string, and the string containing the substring is called the main string.

For example: a = "BEI", b = "BEIJING", c = "BJINGEI". For strings a and b, since b contains the continuous string a,

, it can be said that a is a substring of b, and b is the main string of a; and for c and a, although c also contains all the characters of a, but not consecutive "BEI", so the string c has no relationship with a.

The position of substring in the main string: for string a = "BEI", the position of the first character 'B' in string b is 1, so the position of substring a in main string b = The position in "BEIJING" is 1.

The position of the substring in the main string is different from the storage position of the characters in the array. The position of the substring in the main string starts from 1.

Standard for equality of two strings: If the string values ​​of two strings are exactly the same, then the two strings are equal.

There are three storage structures for string storage

There are three structures for storing strings:

1 Fixed-length sequential storage;

2 Heap allocation storage;

3 Block chain storage.

Fixed-length sequential storage

Use a fixed-length array (i.e., static array) to store strings.

For example: char a[7] = "abcdfg";

When storing a string in this way, you need to estimate the length of the string and apply for enough storage space in advance. If the target string exceeds the length requested by the array, the excess part will be automatically discarded (called "truncation").

For example: char a[3] = "abcdfg";//In fact, only "abc" is stored in the array, and the following ones are truncated. Heap allocated storage

Uses dynamic array to store string

In C language, there is a free storage area called "heap", using the malloc function and Free function management, malloc function is responsible for applying for space, and free function is responsible for releasing space.

For example:

char * a = (char*)malloc(5*sizeof(char));//创建 a 数组,动态申请5个 char 类型数据的存储空间

The advantage of using heap allocation storage is that when it is found that the applied space is not enough, you can re-apply for larger storage space through the realloc() function.

例如:a = (char*)realloc(a, 10*sizeof(char));//前一个参数指申请空间的对象;第二个参数,重新申请空间的大小

The storage space applied for using the malloc function will not be released automatically, and the programmer needs to call the free() function to release it manually. If it is not released manually, it will be recycled by the operating system when the program execution is completely completed.

例如:free(a);//释放动态数组a申请的空间

To give a complete example, the connection strings "abc" and "defg" become "abcdefg";

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    char * a1=NULL;
    char * a2=NULL;
    
    a1=(char*)malloc(3*sizeof(char));
    strcpy(a1, "abc");//将字符串“abc”复制给a1
    
    a2=(char*)malloc(3*sizeof(char));
    strcpy(a2, "defg");
    
    int lengthA1=strlen(a1);
    int lengthA2=strlen(a2);
    if (lengthA1<lengthA1+lengthA2) {
        a1=(char*)realloc(a1, (lengthA1+lengthA2)*sizeof(char));
    }
    int i;
    for (i=lengthA1; i<lengthA1+lengthA2; i++) {
        a1[i]=a2[i-lengthA1];
    }
    printf("%s",a1);
    
    free(a1);
    free(a2);
    return 0;
}

Is a string a linear list with special data objects and operations?

Note: In the program, When we assigned values ​​to a1 and a2, we used the strcpy copy function. You cannot use it directly here: a1 = "abc".

If you do this, the program will compile with an error, telling you that space without malloc cannot be freed.

The reason is: The strcpy function copies the string to the applied storage space, while direct assignment means the string is stored in another memory space (itself a constant, placed in the constant area),

The pointing of pointers a1 and a2 has been changed. In other words, although the storage space dynamically applied for before was applied for, it was lost before it was used.

Block chain storage

Block chain storage actually borrows the storage structure of a linked list to store strings. Under normal circumstances, it is enough to use a single linked list, and there is no need to add a head node.

When building a linked list, each node can store one character or multiple characters.

Is a string a linear list with special data objects and operations?

链表中最后一个结点的数据域不一定全被串值占满,通常会补上 “#” 或者其他特殊的字符和字符串中的字符区分开。

每个结点设置字符数量的多少和存储的串的长度、可以占用的存储空间以及程序实现的功能相关。

如果串包含数据量很大,但是可用的存储空间有限,那么就需要提高空间利用率,相应地减少结点数量(因为多一个节点,就多申请一个指针域的空间)。

而如果程序中需要大量地插入或者删除数据,如果每个节点包含的字符过多,操作字符就会变得很麻烦,为实现功能增加了障碍。

总结

在平时编写程序,经常会用到例如:char *a = ”abcd”;这种方式表示字符串,和上面三种存储方式最主要的区别是:这种方式用于表示常量字符串,只能使用,不能对字符串内容做修改(否则程序运行出错);而以上三种方式都可以对字符串进行删改的操作。

例如:

#include <stdio.h>
int main() {
    char* a="abcd";
    a[1]=&#39;b&#39;;
    return 0;
}

程序编译可以通过,运行失败,改成下面堆分配存储的方式就对了:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
    char * a=(char*)malloc(4*sizeof(char));
    strcpy(a, "abcd");
    a[1]=&#39;e&#39;;
    printf("%s",a);
    return 0;
}

Is a string a linear list with special data objects and operations?

三种存储表示方式中,最常用的是堆分配存储,因为它在定长存储的基础上通过使用动态数组,避免了在操作串时可能因为申请存储空间的不足而丢失字符数据;和块链存储方式相比,结构相对简单,更容易操作。

更多计算机编程相关知识,请访问:编程视频!!

The above is the detailed content of Is a string a linear list with special data objects and operations?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Java中的二叉树结构详解Java中的二叉树结构详解Jun 16, 2023 am 08:58 AM

二叉树是计算机科学中常见的数据结构,也是Java编程中常用的一种数据结构。本文将详细介绍Java中的二叉树结构。一、什么是二叉树?在计算机科学中,二叉树是一种树形结构,每个节点最多有两个子节点。其中,左侧子节点比父节点小,右侧子节点则比父节点大。在Java编程中,常用二叉树表示排序,搜索以及提高对数据的查询效率。二、Java中的二叉树实现在Java中,二叉树

Python 实现栈的几种方式及其优劣Python 实现栈的几种方式及其优劣May 19, 2023 am 09:37 AM

​​想了解更多关于开源的内容,请访问:​​​​51CTO开源基础软件社区​​​​https://ost.51cto.com​​一、栈的概念栈由一系列对象对象组织的一个集合,这些对象的增加和删除操作都遵循一个“后进先出”(LastInFirstOut,LIFO)的原则。在任何时刻只能向栈中插入一个对象,但只能取得或者删除只能在栈顶进行。比如由书构成的栈,唯一露出封面的书就是顶部的那本,为了拿到其他的书,只能移除压在上面的书,如图:栈的实际应用实际上很多应用程序都会用到栈,比如:网络浏览器将最近浏览

PHP8中会支持的数据结构,将为你的代码提供更大空间PHP8中会支持的数据结构,将为你的代码提供更大空间Jun 21, 2023 am 08:13 AM

PHP是一种广泛使用的脚本语言,被广泛用于Web开发,服务器端编程以及命令行编程等。随着PHP不断更新和发展,它也日益成为一个更强大的编程工具,为用户提供了更多的功能和更多的工具来开发高质量的应用程序。其中,数据结构是一个非常重要的领域,一种有效的数据结构可以大大提高程序的性能和可读性。在这篇文章中,我们将讨论PHP8中支持的新数据结构,这些新的数据结构将让

如何解决Java中遇到的代码性能优化问题如何解决Java中遇到的代码性能优化问题Jun 29, 2023 am 10:13 AM

如何解决Java中遇到的代码性能优化问题随着现代软件应用的复杂性和数据量的增加,对于代码性能的需求也变得越来越高。在Java开发中,我们经常会遇到一些性能瓶颈,如何解决这些问题成为了开发者们关注的焦点。本文将介绍一些常见的Java代码性能优化问题,并提供一些解决方案。一、避免过多的对象创建和销毁在Java中,对象的创建和销毁是需要耗费资源的。因此,当一个方法

Java语言中的数据结构与算法介绍Java语言中的数据结构与算法介绍Jun 10, 2023 pm 01:37 PM

随着计算机科学的不断发展,数据结构与算法成为了计算机科学领域中最为基础、重要的模块。数据结构是一种组织和存储数据的方式,它是解决问题的基础。算法则是计算机科学的核心,它是指在计算机程序中解决问题的方法和技术。Java作为一种广泛应用的编程语言,其自带的数据结构和算法库是非常强大的,赋予了开发人员更多的力量。一、数据结构Java中提供了多种数据结构,包括数组

go语言有哪些数据结构go语言有哪些数据结构Dec 16, 2022 pm 02:00 PM

go语言数据结构有四大类:1、基础类型,包括整型(有符号和无符号整数)、浮点数、复数、字符串(由不可变的字节序列构成)、布尔值(只有true和false两个值);2、聚合类型,包括数组、结构体(是由任意个任意类型的变量组合在一起的数据类型);3、引用类型,包括指针、slice(是一个拥有相同元素的可变长度序列)、map、function、channel;4、接口类型。

PHP中的SPL扩展:用于处理集合、队列和堆栈等数据结构PHP中的SPL扩展:用于处理集合、队列和堆栈等数据结构May 11, 2023 pm 04:48 PM

在PHP中,数据结构是常见的编程概念之一。使用数据结构可以更有效地组织和管理数据,提高代码的可读性和可维护性。SPL(StandardPHPLibrary,标准PHP库)扩展是PHP中自带的一个功能强大的库,其中包含了许多常用的数据结构和算法,如集合、队列和堆栈等。本文将介绍SPL扩展,以及它在处理数据结构时的应用。SPL的简介SPL扩展是PHP内置的一

Go语言中的数据结构的实现方式Go语言中的数据结构的实现方式Jun 01, 2023 pm 06:51 PM

Go语言是一种支持并发编程的语言,它的内置数据结构非常丰富,可以满足不同场景下的需求。Go语言中实现数据结构的方式有多种,包括数组、切片、字典、链表和树。数组和切片是最基础的数据结构,它们都可以存储一组相同类型的元素。不同之处在于数组的长度是固定的,而切片则可以动态扩展。Go语言中使用数组和切片可以快速创建数据结构,例如著名的排序算法中的快速排序和归并排序都

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.