链表的递归倒置,程序

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

#define LIST_NUM    (5)                     //定义创建链表长度

struct LNode {
    int data;
    struct LNode *next;
};                                          //链表结构体

/*********************创建链表**********************/
struct LNode *Create(int num)
{
    int data;
    int i;
    struct LNode *head, *new, *temp;
    head = NULL;
    for(i = 0; i < num; i++) {
        new = (struct LNode*)malloc(sizeof(struct LNode));
        printf("%s: input intger:", __func__);
        scanf("%d", &data);
        new->data = data;
        if(head == NULL) {
            head = new;
            temp = new;
        } else {
            temp->next = new;
            temp = new;
        }
    }
    temp->next = NULL;
    return head;
}

/**********************打印链表信息*******************/
void Print(struct LNode *head)
{
    struct LNode *current;
    current = head;
    printf("%s: output LNode: ", __func__);
    while(current != NULL) {
        printf("%d ", current->data);
        current = current->next;
    }
    putchar('\n');
}

/**********************判断链表是否为空****************/
int Isempty(struct LNode *head)
{
    if(head->next == NULL) {
        return 1;
    } else {
        return 0;
    }
}

/*********************往链表中增加节点******************/
struct LNode *Insert(struct LNode *head, int pos, int data)
{
    int i;
    struct LNode *current, *new, *temp;
    new = (struct LNode*)malloc(sizeof(struct LNode));
    if(new == NULL) {
        printf("%s: malloc error\n", __func__);
    }
    new->data = data;
    current = head;

    if( (pos > 0) && (pos < Getlen(head)) ) {
        for(i = 0; i < pos; i++) {
            current = current->next;
        }
        temp = current->next;
        current->next = new;
        new->next = temp;
    }
    if(pos == 0) {
        new->next = current;
        head = new;
    }
    return head;
}

/*********************删除链表中的某个节点**************/
struct LNode *Delte(struct LNode *head, int pos)
{
    int i;
    struct LNode *current, *temp;
    current = head;
    if( (pos > 0) && (pos < Getlen(head)) ) {
        for(i = 0; i < pos; i++) {
            temp = current;
            current = current->next;
        }
        temp->next = current->next;
        free(current);
    }
    if(pos == 0) {
        head = current->next;
        free(current);
    }
    return head;
}

/********************使用递归逆置链表******************/
struct LNode *Conver(struct LNode *head)
{
    struct LNode *current, *temp;
    if(head->next == NULL) {
        printf("%s: end\n", __func__);
        return head;
    }

    current = Conver(head->next);
    temp = head->next;
    temp->next = head;
    head->next = NULL;
    printf("%s: temp and current\n", __func__);
    return current;
}

/*****************获取链表长度************************/
int Getlen(struct LNode *head)
{
    int len = 0;
    struct LNode *current;
    current = head;
    while(current->next != NULL) {
        len++;
        current = current->next;
    }
    if(len) {
        len++;
    }
    return len;
}

int main(int argc, char **argv)
{
    struct LNode *head, *current;

    head->next = NULL;

    if(Isempty(head)) {
        printf("%s: head is empty\n", __func__);
        printf("%s: list len is %d\n", __func__, Getlen(head));
    } else {
        printf("%s: head is exist\n", __func__);
        printf("%s: list len is %d\n", __func__, Getlen(head));
    }

    head = Create(LIST_NUM);
    if(Isempty(head)) {
        printf("%s: head is empty\n", __func__);
        printf("%s: list len is %d\n", __func__, Getlen(head));
    } else {
        printf("%s: head is exist\n", __func__);
        printf("%s: list len is %d\n", __func__, Getlen(head));
    }

    current = head;
    Print(current);

    current = Insert(head, 0, 10);              //插入数据为10的数据作为链表头
    Print(current);

    current = Delte(current, 5);         //删除第五个数据(0开始)
    Print(current);

    current = Conver(current);          //逆置
    Print(current);

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值