同时含有二叉树和链表——Exercise of 6-3

同时含有二叉树和链表——Exercise of 6-3_第1张图片

这第一层馍是行云流水的数据结构—链表:

这第二层馅是妙趣横生的Oriented English Grammar

这第三层馍是苍翠挺拔的数据结构—二叉树

一、没有馅,馍就是普通膜

    鸡汤卤汁卤的:

Every day I remind myself that my inner and outer life are based on the labors of other men,living and deaded,and I must exert myself in order to give in the same measures as i have received and still receiving.

Analysis of Oriented Grammar : 日后分析

二、夹馍—链接、二叉树

三、本源

    其实呢,就是给编程爱好者朋友彻头彻尾的解了道题,题设是:编写一个交叉数据结构引用程序,打印文档中所有单词的列表,并且每个单词还有一个列表用来记录出现过该单词的行号。当然,我这里用的文档中的内容简单,所以对应的getword()函数就简单一些,主要是说明底层数据结构逻辑。

代码如下:

#include 
#include 
#include 
#include 
#define MAXWORD 100
struct LinkList
{
    int LNum;
    struct LinkList*next;
};
typedef struct LinkList*LkList;
struct Tree
{
    char*word;
    LkList ptr;
    struct Tree*Left;
    struct Tree*Right;
};
typedef struct Tree*Tr;

void xprint(Tr root);
Tr addtree(Tr,char*,int);
int getword(char*,int);
int main()/*complete*/
{
    char word[MAXWORD];
    char w ;
    Tr root = NULL;
    int cond ;
    int lNum=1;
    while((w=getword(word,MAXWORD))!=EOF)
    {
        if((cond = strcmp(word,"end"))==0)
            break;
        if(isalpha(w))
            root = addtree(root,word,lNum);
        if(w=='\n')
            lNum++;
    }
    xprint(root);
}
void xprint(Tr root)/*assistant function*/
{
    if(root!=NULL)
    {
        xprint(root->Left);
        printf("%s\n",root->word);
        LkList temp = root->ptr;
        while(temp!=NULL)
        {
            printf("%d",temp->LNum);
            temp = temp->next;
        }
        printf("\n");
        xprint(root->Right);
    }
}
int getword(char*w,int lim)/*successfully tested */
{
    char* tw = w;
    char c;
    while(isspace(c=getchar())&&c!='\n')
        ;
    if(c != EOF)
    {
        *tw++ =  c;
    }
    else if(!isalpha(c))
    {
        *tw = '\0';
        return c;
    }
    for(; --lim > 0 ; tw++)
    {
        if(!isalnum(*tw = getchar()))
        {
            ungetch(*tw);
            break ;
        }
    }
    *tw = '\0';
    return c;
}
Tr talloc(void);
char*strdup(char*);
LkList lalloc(void);
void addLKList(LkList,int);
Tr addtree(Tr root,char*word,int lNum)/*successfully tested*/
{
    char* w = word ;
    int cond ;
    int num = lNum;
    if(root == NULL)
    {
        root = talloc();
        root->ptr=lalloc();
        root->ptr->next=NULL;
        root->ptr->LNum=num;
        root->word = strdup(w);
        root->Left = NULL;
        root->Right = NULL;
    }
    else
    {
        cond = strcmp(w,root->word);
        if(cond > 0)
        {
            root->Right = addtree(root->Right,w,num);
        }
        else if(cond < 0)
        {
            root->Left = addtree(root->Left,w,num);
        }
        else
        {
            addLKList(root->ptr,num);
        }
    }
    return root ;
}
Tr talloc(void)/*assistant function*/
{
    return (Tr)malloc(sizeof(struct Tree));
}
char*strdup(char*word)/*assistant function*/
{
    char*tw = (char*)malloc(strlen(word)+1);
    if(tw!=NULL)
        strcpy(tw,word);/*copying s2 to s1*/
    return tw;
}
LkList lalloc(void)
{
    return (LkList)malloc(sizeof(struct LinkList));
}
LkList lalloc(void);
void addLKList(LkList list, int lNum)
{
    LkList temp = list ;
    int num = lNum;
    while(temp->next!=NULL)
    {
        temp = temp->next;
    }
    if(temp->LNum!=num)
    {
        temp->next = lalloc();
        temp->next->LNum=num;
        temp->next->next=NULL;
    }
}


总结:通过这个练习,重新认识了isspace()函数,之前通过字面翻译和语句的用途,以为isspace()只是一个空格过滤函数。

查阅AIP后得知:int isspace(int c) :检查所传的字符是否是空白的字符。

标准的空白字符包括:

' '     (0x20)    space (SPC) 空格符
'\t'    (0x09)    horizontal tab (TAB) 水平制表符    
'\n'    (0x0a)    newline (LF) 换行符
'\v'    (0x0b)    vertical tab (VT) 垂直制表符
'\f'    (0x0c)    feed (FF) 换页符
'\r'    (0x0d)    carriage return (CR) 回车符
 

你可能感兴趣的:(同时含有二叉树和链表——Exercise of 6-3)