数据结构教程 | 串的存储

本文深入探讨了三种不同的字符串存储方式:固定长度顺序存储、堆分配存储及链片存储。通过具体的代码实例,详细讲解了每种存储方式的实现方法及应用场景,为读者提供了全面的字符串存储知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Data Structure: The Storage of String


1 Introduction

If we want to store a string, there will be 3 storage structure contained:

  • Fixed length sequential storage: Static array, the same as ordinary array.
  • Heap allocation storage: Using dynamic array to store the string.
  • Chain pieces storage: Using link list to store the string.

2 Three ways to store the string

2.1 Fixed length sequential storage

int main()
{
    char str[40]="Fixed length sequential storage";
    printf("%s\n",str);
    return 0;
}

2.2 Heap allocation storage

We use ‘malloc’ to allocate space for dynamic array, for example:

char * a = (char*)malloc(5*sizeof(char));

We have applied for a heap storage space with the size of 5×sizeof(char), the dynamic array could change its length:

a = (char*)realloc(a, 10*sizeof(char));

Now we enlarge the space to the size of 10×sizeof(char)

Here’s an example, we connect two strings, using heap allocation storage:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    char * a1=NULL;
    char * a2=NULL;
    a1=(char*)malloc(4*sizeof(char));
    strcpy(a1, "abcd");				// Copy the string to a1
    a2=(char*)malloc(3*sizeof(char));
    strcpy(a2, "efg");
    int lengthA1=strlen(a1);
    int lengthA2=strlen(a2);

    // Store the concated string in a1
    if (lengthA1<lengthA1+lengthA2) {
        a1=(char*)realloc(a1, (lengthA1+lengthA2)*sizeof(char));
    }
   
    for (int i=lengthA1; i<lengthA1+lengthA2; i++) {
        a1[i]=a2[i-lengthA1];
        printf("%s	\n",a1);

    }

    free(a1);
    free(a2);
    return 0;
}

Output:
在这里插入图片描述

2.3 Chain pieces storage

In this part, when we design the structure of nodes in the link list, we can set the number of elements stored in the node.
For example, if we store one element in each node:
在这里插入图片描述
We can also store more elements in each node:
在这里插入图片描述
Here’s the code implementation:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define linkNum 3		// Set the number of elements stored in each node
typedef struct Link {
    char a[linkNum]; 
    struct Link * next; 
}link; 
link * initLink(link * head, char * str);
void displayLink(link * head);
int main()
{
    link * head = NULL;
    head = initLink(head, "abcdefghijklmnopqr");
    displayLink(head);
    return 0;
}

link * initLink(link * head, char * str) {
    int length = strlen(str);
    int num = length/linkNum;		// The number of nodes
    if (length%linkNum) {
        num++;
    }
    // Create and initialize the first node
    head = (link*)malloc(sizeof(link));
    head->next = NULL;
    link *temp = head;
    // Initalize the link list
    for (int i = 0; i<num; i++)
    {
        int j = 0;
        for (; j<linkNum; j++)
        {
            if (i*linkNum + j < length) {
                temp->a[j] = str[i*linkNum + j];
            }          
            else
                temp->a[j] = '#';
        }
        if (i*linkNum + j < length)
        {
            link * newlink = (link*)malloc(sizeof(link));
            newlink->next = NULL;
            temp->next = newlink;
            temp = newlink;
        }
    }
    return head;
}

void displayLink(link * head) {
    link * temp = head;
    while (temp) {
        for (int i = 0; i < linkNum; i++) {
            printf("%c", temp->a[i]);
        }
        temp = temp->next;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值