数据结构01

练习1:在堆区申请两个长度为32的空间,实现两个字符串的比较【非库函数实现】

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
    char *p = (char *)malloc(sizeof(char)*32);
    char *q = (char *)malloc(sizeof(char)*32);

    strcpy(p,"hello world");
    strcpy(q,"hello shanghai");
    str_cmp(p,q);
    return 0;
}

void str_cmp(char*p,char*q){
    int i=0;
    while(p[i]==q[i]){
        if(p[i]!=q[i]){                               
            break;
        }
        i++;
    }
    int sub = p[i]-q[i];
    if(sub>0)
        printf("p>q\n");
    else if(sub<0)
        printf("p<q\n");
    else if(sub==0)
        printf("p=q\n");

    return 0;

}

练习2: 定义函数,在堆区申请空间 两个申请,主函数需要调用2次

#include <stdio.h>
#include <stdlib.h>
   
void* mal(size_t size) {  
    void* ptr = malloc(size);  
    return ptr;  
}  
    
int main() {  
    int* array1 = (int*)mal(10 * sizeof(int));  
    if (array1 != NULL) {  
        free(array1);  
    }    
    double* array2 = (double*)mal(20 * sizeof(double));  
    if (array2 != NULL) {  
        free(array2);  
    }   
    
    return 0;  
}                                                         
                                                          

练习3:定义函数,实现字符串的输入 void input(char *p)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void input(char *p);
int main(int argc, const char *argv[])
{   
    char p[128];
    input(p);
    puts(p);
    return 0;
}

void input(char *p)
{   
    printf("请输入一个字符串\n");
    gets(p);
}                                                     

练习4:调用函数实现字符串比较,在主函数中输出大小

int my_strcmp(const char *s1,const char *s2)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int my_strcmp(const char*s1,const char *s2);
int main(int argc, const char *argv[])
{
    char *p = (char *)malloc(sizeof(char)*32);
    char *q = (char *)malloc(sizeof(char)*32);
    
    strcpy(p,"hello world");
    strcpy(q,"hello shanghai");
    my_strcmp(p,q);
    return 0;
}

int my_strcmp(const char*s1,const char*s2){
    int i=0;
    while(s1[i]==s2[i]){
        if(s1[i]!=s2[i]){
            break;
        }
        i++;
    }
    int sub = s1[i]-s2[i];
    if(sub>0)
        printf("s1>s2\n");
    else if(sub<0)
        printf("s1<s2\n");
    else if(sub==0)                                              
        printf("s1=s2\n");
    
    return 0;
 
}

练习5:定义函数,释放空间

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void Free(char*p);
int main(int argc, const char *argv[])
{   
    char*q = (char*)malloc(sizeof(char)*8);
    strcpy(q,"hello");
    puts(q);
    Free(q);
    return 0;
}   

void Free (char *p){
      if(p!=NULL){
         free(p);
         p=NULL;
      }  
}     

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值