zoukankan      html  css  js  c++  java
  • 笔试编程题1

    1,不用库函数实现strcpy,并说明strcpy返回char*的原因

    为了实现链式表达式。 例如 int length = strlen( strcpy( strDest, “hello world”) );

    /*将字符串s1复制到s2*/
    char*
    mystrcpy(char *s1, const char *s2) { if(s1==NULL || s2==NULL) return -1; char *r=s1; while((*s1++ = *s2++) != ''); return r; }

    2,实现strcmp

    int mystrcmp(const char *s1, const char *s2)
    {
        while( (*str1==*str2) && (*str1 != '') )
        {
            str1++;
            str2++;
        }
    
        if(*str1 > *str2) return 1;
        else if(*str1 < *str2) return -1;
        else return 0;
    }

    3,实现strstr

    /*查找字符串s1中字符串s2所在的位置*/
    char
    *mystrstr(const char *s1, const char* s2) { int i=0, j=0, temp=0; for(i=0; s1[i]!=''; i++) { temp = i; j = 0; while(s1[i++] ==s2[j++]) { if(s2[j] == '') return &s1[temp]; } i = temp; } return NULL; }

    4,找出两个字符串的最大公共字符串

    int GetCommon(char *s1, const char *s2, char **r)
    {
        int len1 = strlen(s1);
        int len2 = strlen(s2);
        int maxlen = 0;
        int i=0, j=0;
        
        for(i=0; i<len1; i++)
        {
            for(j=0; j<len2; j++)
            {
                if(s2[i] == s2[j])
                {
                    int as=i, bs=j,count=1;
                    while((as+1<len1) && (bs+1<len2) && (s1[++as]==s2[++bs]))
                        count++;
                    if(count>maxlen)
                    {
                        maxlen = count;
                        *r = s1+1;
                    }
                }
            }
        }
    }

    5,链表逆序

    typedef struct node 
    {
        struct node *next;
        int data;
    }Node;
    
    typedef struct queue
    {
        Node *front; 
        Node *rear;
        int num;
    }Queue;
    
    int QueueReverse(Queue *queue)
    {
        Node *node = queue->front;
        Node *nodeb;
        if(QueueIsEmpty(queue))
        {
            printf ("queue is empty
    ");
            return -1;
        }
    
        queue->rear = queue->front;
        queue->front = NULL;
    
        while(node != NULL)
        {
            nodeb = node;
            node = node->next;
            nodeb->next = queue->front;
            queue->front = nodeb;
        }
    
        return 0;
    }

    6,判断链表是否有环

     

    7,合并两个没有交集的有序链表 

    typedef node
    {
        int data;
        struct node *next;
    }Node, *List;
    
    //s1,s2没有头结点
    List Mergelist_L(List s1, List s2)
    {
        Node node = {0,NULL};
        Node *head = &node;   //head为合并后的头结点
        Node *rear = head;
    
        while( s1 && s2)
        {
            if(s1->data < s2->data)
            {
                rear->next = s1;
                rear = rear->next;
                s1 = s1->next;
            }
            else
            {
                rear->next = s2;
                rear = rear->next;
                s2 = s2->next;
            }
            rear->next = s1? s1:s2; //s1为空的话rear->next=s2
        }
    
        head = head->next;  //去掉头结点
        return head;
    }

     

  • 相关阅读:
    2018-8-10-win10-uwp-win2d-使用-Path-绘制界面
    2018-8-10-win10-uwp-win2d-使用-Path-绘制界面
    PHP money_format() 函数
    PHP metaphone() 函数
    PHP md5_file() 函数
    PHP md5() 函数
    PHP ltrim() 函数
    查看统计信息
    CF960F Pathwalks_权值线段树_LIS
    hdu 5691 Sitting in line 状压动归
  • 原文地址:https://www.cnblogs.com/zhoutian220/p/4069912.html
Copyright © 2011-2022 走看看