数据结构C语言实现系列[5]——串
来源:互联网 发布:matlab找矩阵最小值 编辑:程序博客网 时间:2024/06/01 23:11
/********************************************************
* 串的简单模式匹配 *
********************************************************/
#include <stdio.h>
#include <stdlib.h>
/* 定义单链表结构体 */
struct node
{
char ch;
struct node *next;
};
/* 初始化单链表 */
void init(struct node **h)
{
*h = (struct node*)malloc(sizeof(struct node));
(*h)->next = NULL;
return;
}
/* 将x结点插入到链表后 */
void append(struct node *p, int x)
{
struct node *s;
s = (struct node*)malloc(sizeof(struct node));
s->ch = x;
s->next = NULL;
/* 移动到表尾 */
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为主串, t为模式串 */
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); /* 添加到表尾 */
}
}
display(s);
init(&t);
printf("Please type substring: ");
x = 0;
while (x != ' ')
{
x = getchar();
if (x != ' ')
{
append(t, x); /* 添加到表尾 */
}
}
display(t);
/* 初始化 */
sNext = s->next;
p = sNext;
q = t->next;
i = 1;
/* 从开始字符进行比较 */
while ((p->next != NULL) && (q->next != NULL))
{
/* 进行匹配检验 */
if (p->ch == q->ch)
{
p = p->next;
q = q->next;
}
else
{
sNext = sNext->next;
p = sNext; /* 指向主串中的下一个 */
q = t->next; /* 指针后退重新开始匹配 */
i++; /* 记录位置 */
}
}
/* 输出结果 */
if ((q->next) == NULL && (t->next->ch == s->next->ch))
{
printf("match position: %d", i);
}
else
{
printf("Not match!");
}
printf(" ");
return 0;
}
* 串的简单模式匹配 *
********************************************************/
#include <stdio.h>
#include <stdlib.h>
/* 定义单链表结构体 */
struct node
{
char ch;
struct node *next;
};
/* 初始化单链表 */
void init(struct node **h)
{
*h = (struct node*)malloc(sizeof(struct node));
(*h)->next = NULL;
return;
}
/* 将x结点插入到链表后 */
void append(struct node *p, int x)
{
struct node *s;
s = (struct node*)malloc(sizeof(struct node));
s->ch = x;
s->next = NULL;
/* 移动到表尾 */
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为主串, t为模式串 */
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); /* 添加到表尾 */
}
}
display(s);
init(&t);
printf("Please type substring: ");
x = 0;
while (x != ' ')
{
x = getchar();
if (x != ' ')
{
append(t, x); /* 添加到表尾 */
}
}
display(t);
/* 初始化 */
sNext = s->next;
p = sNext;
q = t->next;
i = 1;
/* 从开始字符进行比较 */
while ((p->next != NULL) && (q->next != NULL))
{
/* 进行匹配检验 */
if (p->ch == q->ch)
{
p = p->next;
q = q->next;
}
else
{
sNext = sNext->next;
p = sNext; /* 指向主串中的下一个 */
q = t->next; /* 指针后退重新开始匹配 */
i++; /* 记录位置 */
}
}
/* 输出结果 */
if ((q->next) == NULL && (t->next->ch == s->next->ch))
{
printf("match position: %d", i);
}
else
{
printf("Not match!");
}
printf(" ");
return 0;
}
- 数据结构C语言实现系列[5]——串
- 数据结构C语言实现系列[1]——线性表
- 数据结构C语言实现系列[2]——栈
- 数据结构C语言实现系列[4]——队列
- 数据结构C语言实现系列[6]——堆
- 数据结构C语言实现系列[7]——二叉树
- 数据结构C语言实现系列[8]——树
- 数据结构C语言实现系列[9]——广义表
- 数据结构C语言实现系列[2]——栈
- 数据结构C语言实现系列[1]——线性表
- 数据结构C语言实现系列——二叉树
- 数据结构C语言实现系列——队列
- 数据结构C语言实现系列——二叉树[转]
- 数据结构C语言实现系列——队列
- 数据结构C语言实现系列——二叉树
- 数据结构C语言实现系列——队列
- 数据结构C语言实现系列——线性表
- 数据结构C语言实现系列——线性表
- 浅析设计模式之FactoryMethod
- 数据结构C语言实现系列[1]——线性表
- 数据结构C语言实现系列[2]——栈
- 数据结构C语言实现系列[3]——关于栈的一些习题
- 数据结构C语言实现系列[4]——队列
- 数据结构C语言实现系列[5]——串
- 数据结构C语言实现系列[6]——堆
- 71%的网络攻击都与同一个黑客工具包有关
- 数据结构C语言实现系列[7]——二叉树
- 数据结构C语言实现系列[8]——树
- 数据结构C语言实现系列[9]——广义表
- 石家庄FB吃喝地点一览
- Unix 的入侵追踪
- .net代码获取网站ChinaRank排名