Data structure C language implementation series [5] -- string

Source: Internet
Author: User

 

/*************************************** *****************
* Simple string mode matching *
**************************************** ****************/

# Include <stdio. h>
# Include <stdlib. h>

/* Define the structure of a single-chain table */
Struct Node
{
Char ch;
Struct node * next;
};

/* Initialize a single-chain table */
Void Init (struct node ** H)
{
* H = (struct node *) malloc (sizeof (struct node ));
(* H)-> next = NULL;
Return;
}

/* Insert the x node into the linked list */
Void append (struct node * P, int X)
{
Struct node * s;

S = (struct node *) malloc (sizeof (struct node ));
S-> CH = X;
S-> next = NULL;
/* Move to the end of the table */
While (p-> next! = NULL)
{
P = p-> next;
}

P-> next = s;

Return;
}

Void display (struct node * P)
{
Printf ("You typed string is :");
While (p-> next! = NULL)
{
Printf ("% C", p-> next-> CH );
P = p-> next;
}
Printf ("");

Return;
}

Int main (INT argc, char * argv [])
{
Struct node * t, * s;/* s is the main string, and T is the mode string */
Struct node * snext, * P, * q;
Int I, x = 0;

Init (& S );
Printf ("please type main string :");
While (X! = '')
{
X = getchar ();
If (X! = '')
{
Append (S, x);/* Add to the end of the table */
}
}
Display (s );

Init (& T );
Printf ("please type substring :");
X = 0;
While (X! = '')
{
X = getchar ();
If (X! = '')
{
Append (t, x);/* Add to the end of the table */
}
}
Display (t );

/* Initialize */
Snext = s-> next;
P = snext;
Q = T-> next;
I = 1;
/* Compare starting characters */
While (P-> next! = NULL) & (Q-> next! = NULL ))
{
/* Perform matching test */
If (p-> CH = Q-> CH)
{
P = p-> next;
Q = Q-> next;
}
Else
{
Snext = snext-> next;
P = snext;/* points to the next one in the main string */
Q = T-> next;/* the pointer returns and starts matching again */
I ++;/* record location */
}
}
/* Output result */
If (Q-> next) = NULL & (t-> next-> CH = s-> next-> CH ))
{
Printf ("Match position: % d", I );
}
Else
{
Printf ("not match! ");
}
Printf ("");

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.