DS-007 35 数据结构:串的存储

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;
    }
}

猜你喜欢

转载自blog.csdn.net/Tinky2013/article/details/87548898
35