串的簡單模式匹配_第1頁
串的簡單模式匹配_第2頁
串的簡單模式匹配_第3頁
全文預覽已結束

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、/*  *                    串的簡單模式匹配                  * */ #include <stdio.h>#incl

2、ude <stdlib.h>/* 定義單鏈表結構體 */struct node    char ch;    struct node *next;/* 初始化單鏈表 */void init(struct node *h)    *h = (struct node*)malloc(sizeof(struct 

3、node);    (*h)->next = NULL;    return;/* 將x結點插入到鏈表后 */void append(struct node *p, int x)    struct node *s;    s = (struct node*)malloc(sizeof(str

4、uct node);    s->ch = x;    s->next = NULL;    /* 移動到表尾 */    while (p->next != NULL)            p

5、60;= p->next;            p->next = s;        return;void display(struct node *p)    printf("You typed string is: "

6、);    while (p->next != NULL)            printf("%c", p->next->ch);        p = p->next;       

7、; printf(" ");    return;int main(int argc, char *argv)    struct node *t, *s;    /* s為主串, t為模式串 */    struct node *sNext, *p, *q; 

8、;   int i, x = 0;        init(&s);    printf("Please type main string:  ");    while (x != ' ')     

9、60;      x = getchar();        if (x != ' ')                    append(s, x);  

10、60;     /* 添加到表尾 */                display(s);        init(&t);    printf("Please type substring: &qu

11、ot;);    x = 0;    while (x != ' ')            x = getchar();        if (x != ' ')  

12、                  append(t, x);        /* 添加到表尾 */                display(t

13、);        /* 初始化 */    sNext = s->next;    p = sNext;    q = t->next;    i = 1;       

14、60;    /* 從開始字符進行比較 */    while (p->next != NULL) && (q->next != NULL)            /* 進行匹配檢驗 */      &

15、#160; if (p->ch = q->ch)                    p = p->next;            q = q->next; &#

16、160;              else                        sNext = sNext->next;     &

17、#160;      p = sNext;        /* 指向主串中的下一個 */            q = t->next;    /* 指針后退重新開始匹配 */   &

18、#160;        i+;        /* 記錄位置 */                /* 輸出結果 */    if (q->next) = NULL && (t->next->ch = s->next->ch)            printf("match position: %d", i

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論