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