制作strcpy函数在C链表 [英] Making strcpy function with linked list in c

查看:365
本文介绍了制作strcpy函数在C链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用链表我自己的strcpy函数,但不能得到该怎么办。
如果不使用链表它可以像这样

 的char * cp2014strcpy(字符* dest_ptr,为const char * src_ptr){
    字符* strresult = dest_ptr;    如果((NULL = dest_ptr)及!及(NULL = src_ptr)!){
        而(NULL!= src_ptr){
            * dest_ptr ++ = * src_ptr ++;
        }
        * dest_ptr = NULL;
    }    返回strresult;
}

但我不能得到如何使用链表做的strcpy。


解决方案

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;typedef结构节点{
    焦炭CH;
    结构节点*接下来的;
} LL_str;LL_str * LL_new(CHAR CH){
    LL_str * S =的malloc(sizeof的(* S)); //检查省略
    仲> CH = CH;
    仲>接着= NULL;
    返回S;
}LL_str * s_to_LL(为const char * S){
    LL_str *顶* CURR;
    如果(!小号||!* S)
        返回NULL;
    CURR =畅销= LL_new(* S);
    而(* + S){
        CURR = curr->接着= LL_new(* S);
    }
    返回顶部;
}LL_str * LL_strcpy(常量LL_str * S){// LL_strdup?
    LL_str *顶* CURR;
    如果(!S)
        返回NULL;
    CURR =畅销= LL_new(S-GT&; CH);
    S = S->接下来,
    而(多个){
        CURR = curr->接着= LL_new(S-> CH);
        S = S->接下来,
    }
    返回顶部;
}无效LL_println(常量LL_str * S){
    而(多个){
        的putchar(S-GT&; CH);
        S = S->接下来,
    }
    的putchar('\\ n');
}无效LL_drop(LL_str * S){
    如果(多个){
        LL_drop(S->下面);
        免费(S);
    }
}INT主(INT ARGC,CHAR *的argv []){
    LL_str * S = s_to_LL(世界,你好!);
    LL_str * D = LL_strcpy(S);    LL_println(四);
    LL_drop(多个);
    LL_drop(四);
    返回0;
}

I was making my own strcpy function using linked list but couldn't get how to do. Without using linked list it could be like this

char* cp2014strcpy(char * dest_ptr, const char * src_ptr) {
    char* strresult = dest_ptr;

    if((NULL != dest_ptr) && (NULL != src_ptr)) {
        while (NULL != src_ptr) {
            *dest_ptr++ = *src_ptr++;
        }
        *dest_ptr = NULL;
    }

    return strresult;
}

but I couldn't get how to make strcpy using linked list.

解决方案

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

typedef struct node {
    char ch;
    struct node *next;
} LL_str;

LL_str *LL_new(char ch){
    LL_str *s = malloc(sizeof(*s));//check omitted
    s->ch = ch;
    s->next = NULL;
    return s;
}

LL_str *s_to_LL(const char *s){
    LL_str *top, *curr;
    if(!s || !*s)
        return NULL;
    curr = top = LL_new(*s);
    while(*++s){
        curr = curr->next = LL_new(*s);
    }
    return top;
}

LL_str *LL_strcpy(const LL_str *s){//LL_strdup ??
    LL_str *top, *curr;
    if(!s)
        return NULL;
    curr = top = LL_new(s->ch);
    s=s->next;
    while(s){
        curr = curr->next = LL_new(s->ch);
        s=s->next;
    }
    return top;
}

void LL_println(const LL_str *s){
    while(s){
        putchar(s->ch);
        s = s->next;
    }
    putchar('\n');
}

void LL_drop(LL_str *s){
    if(s){
        LL_drop(s->next);
        free(s);
    }
}

int main(int argc, char *argv[]){
    LL_str *s = s_to_LL("Hello world!");
    LL_str *d = LL_strcpy(s);

    LL_println(d);
    LL_drop(s);
    LL_drop(d);
    return 0;
}

这篇关于制作strcpy函数在C链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆