一道上级面试题: 接受单词并按字典序排序

来源:互联网 发布:ubuntu运行程序 编辑:程序博客网 时间:2024/06/04 18:48

#include<stdio.h>
#include <string.h>
#include <stdlib.h>


#define  LISTMAXSIZE    64
#define  STRINGMAXSIZE  1024

struct wordList{  //列表结构
    char * befor;
    char * body;
    char * next;
};

void change(struct wordList * frist , struct wordList * second )
{
    char * tempBefor;
    char * tempBody;
    char * tempNext;

    tempBefor = frist->befor;
    tempBody = frist->body ;
    tempNext = frist->next;

    frist->befor = second->befor;
    frist->body = second->body ;
    frist->next = second->next;

    second->befor = tempBefor;
    second->body = tempBody ;
    second->next = tempNext;

    return ;
}

int myStrcmp (char * str1, char * str2) // 类似于str1 - str2
{
    int len1 = strlen(str1);
    int len2 = strlen(str2);
       
    int set = 0;
    int minSet = (len1 < len2) ? len1 : len2;
    int result  = 0;

    while(set <= minSet)
    {
        result = *(str1 + set) - *(str2 + set);
        if(result == 0)
        {
            set++;
        }
        else
        {
            return result;
        }
    }
   
    return len1 - len2;
}

int main()
{
    int flag = 2;
    int i = 0;
    int j = 0;

    char  myString [STRINGMAXSIZE] = "sfsfsdf saaaa asdad dfg ";
    int myStrSet = 0;
    char  myStringTemp [STRINGMAXSIZE] = {0};
    int myStrTempSet = 0;

    char ch ;
    char * head = NULL;

    struct wordList * myList [LISTMAXSIZE ] = {0};
    int myListSet = 0;


    while( (ch = myString[ myStrSet ++] )!= '/0')
    {
        if( ch != ' ' )
        {
            myStringTemp[myStrTempSet++] = ch;
            flag = 1;

        }
        else if(flag == 1)
        {
            myStringTemp[myStrTempSet] = '/0';   

            if(myListSet == 0)
            {
                myList[myListSet] = malloc(sizeof (struct wordList) + 1);
                myList[myListSet]->befor = NULL;
                myList[myListSet]->body =  strdup(myStringTemp);
            }
            else
            {
                myList[myListSet] = malloc(sizeof (struct wordList) + 1);
                myList[myListSet]->befor = myList[myListSet-1];
                myList[myListSet]->body =  strdup(myStringTemp);
            }

            myListSet++;
            myStrTempSet = 0;
            flag = 2;
            memset(myStringTemp,0, STRINGMAXSIZE);
        }
    }

    for(i = 0 ; i < myListSet-1; i++)
    {
        for(j = i+1 ; j < myListSet; j++)
        {
            if(  myStrcmp( (myList[i]->body) , (myList[j]->body)) < 0 )// >  降序  < 升序
            {
                continue;
            }
            else
            {
                change(myList[i] , myList[j]);
            }

            head = myList[i];
           
        }

        if(i == 0)
        {
            head = myList[i];
        }
        else
        {
            myList[i-1]->next = myList[i];
        }
    }

    for(i = 0 ; i < myListSet; i++)
    {
        printf("%s/n", (myList[i]->body));
    }


    return 0;
}


原创粉丝点击