对通过用户输入的值,并遍历一次,将用户输入的值倒过来,单向链表进行逆向

来源:互联网 发布:c语言流程图例题 编辑:程序博客网 时间:2024/05/29 16:11

思路1:头插法–在头结点插入next对象

typedef struct node_{    char c;    struct node_* next;}abc;//释放链表void Free(abc* head){    abc* p;    while(head != NULL) {        p = head;        head = head->next;        free(p);    }       if(head == NULL) printf("free succes\n");   }//头插入abc* SetHead(abc* head){    abc* a,*b,*c;    a = head;    c = (abc*)malloc(sizeof(abc));    if(a->next == NULL) {        c->next = NULL;        a->next = c;        return a;    }       c->next = a->next;    a->next = c;    //    free(c);    return a;}void main(){    abc* p,*d;    p = (abc*)malloc(sizeof(abc));    p->next = NULL;    char cc[100];    printf("please input string:");    scanf("%s",cc);    printf("cc = [%s] size = [%d] [%d]\n",cc,strlen(cc),sizeof(cc));    static int i=0;    char* pi = cc;     while(*pi){        i++;        i++;        p = SetHead(p);        p->next->c = *pi;        pi++;    }    d = p;    i=0;    printf("d=");    while(d->next != NULL) {        i++;        printf("%c",d->next->c);        d = d->next;    }    printf("\n");    Free(p);}结果:please input string:0123456789cc = [0123456789] size = [10] [100]d=9876543210
0 0
原创粉丝点击