Data structure Learning notes-strings (C language Implementation)

Source: Internet
Author: User

The

string consists of 0 or more characters, which is simply a string . The way a string is stored is somewhat different from a linear table, and he is divided into the following categories: Sequential storage, heap allocation storage, and chained storage. Sequential storage The header element in the array usually holds the string length. Heap allocation storage typically allocates space dynamically. There are two types of chained storage, one for each node (which is a waste of space), the other is for each node to hold more than one character, if the character is not exactly, use # instead of . (the string definition in C has the following forms: string constant "Hello", char array char m[40] = "Hello,world", char pointer char *m = "Hello,world". The string encounters the end of ' a ', and the end of the defined string is added to the system. the storage implementation principle of these kinds of strings is the same, but there are similarities and differences in the way. The following is the basic operation of a string:

  Basic Operations:           init (t , chars)                   initial Condition:  chars is a string constant.                   Operation results: Generates a string T with a value of chars.            connection (s, t)                    initial Condition: string s, t  already exist.                    Operation results: The string t is connected to the string s to form a new string to be stored in S.             getlength (t)                    initial Condition: string T already exists.                    Operation results: Returns the number of elements in a string T, called the string length.             substring  (s, pos, len, sub)                    initial Condition: string s,  already exists,  1≦pos≦strlength (s) and  0≦len≦strlength (s)  –pos+1.                   Operation results: Returns the len  of the first POS character of a string s with a sub string.   

The following is an implementation of chained storage code for strings:

#include <stdio.h> #include <stdlib.h>/serial chain-type storage typedef struct charnode{char data;
struct Charnode *next;
}charnode;
typedef charnode *charlink; There are several forms of string in C: string constant "Hello", char array char m[40] = "Hello,world", char pointer char *m = "Hello,world" void Init (Charlink *c,char s
    []) {Charlink p,q; (*c) = (charlink) malloc (sizeof (Charnode));//Create chain string q=*c;//define pointer to string for (int i=0;s[i]!= '; i++ ') {All defined strings in//c are followed by a ' \
        0 ', he represents the end of p= (Charlink) malloc (sizeof (Charnode));
        p->data=s[i];//here similar to the trailing interpolation method of the list q->next=p;
    Q=p;
} q->next=null;
    //Get string length int getlength (Charlink c) {int i=0;
    Charlink p;
    P=c;
        while (P->next) {p=p->next;
    i++;
return i;
    ///Two strings are spelled into a string void connection (Charlink *c,charlink b) {Charlink p= (*c)->next;//defines a pointer to the first character of C. Charlink p2=b;//defines a pointer to a B charlink p3;//the character node that is used to store in B the Char s;//is used to hold the character data removed from B while (P2->next) {//from the first character node of B
  Start taking p2=p2->next;      s=p2->data;
        while (P->next) {//is judged from next of the first character node of C, if not empty, it is pointed down until P becomes the last character node of C p=p->next;
        } p3= (Charlink) malloc (sizeof (Charnode));
        p3->data=s;
    p->next=p3;//similar linked list of tail interpolation, so that the data inserted in the back row p=p3;
} p->next=null; } void substring (charlink c,int index,int slength,charlink *s) {//when index+slength<= (*s). Length+1 can take Charlink p,
    Z
    *s= (Charlink) malloc (sizeof (Charnode));
    z=*s;
    p=c->next;
    int i=1,j=1;
        Get the first position of the main string to fetch data while (P-&GT;NEXT&AMP;&AMP;I&LT;INDEX) {p=p->next;
    i++;
        }//value assignment while (p&&j<=slength) {Charlink n= (charlink) malloc (sizeof (Charnode));
        n->data=p->data;
        z->next=n;
        Z=n;
        p=p->next;
    j + +;

} z->next=null;
    } void Display (Charlink c) {Charlink p=c->next;
        while (p) {Char q=p->data;
        printf ("%c", Q);
    p=p->next; printf ("\ n ");
    int main () {Charlink c,b,z;
    char c1[20]= "Hello hello";
    Char c2[20]= "World World";
    Char c3[10]= "";
    Init (&AMP;Z,C3);
    Init (&AMP;C,C1);
    Display (c);
    Init (&AMP;B,C2);
    Display (b);
    Connection (&AMP;C,B);
    Display (c);
    printf ("The length of the string is: \n%d\n", GetLength (c));
    SUBSTRING (c,1,5,&z);
    Display (z);
return 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.