IT大道IT大道

首页 >  技术 > 115. 奇数值结点链表

115. 奇数值结点链表

原文 http://www.cnblogs.com/yongwu/p/6275704.html 2017-01-11 23:58:39 0 评论

奇数值结点链表

struct ListNode *readlist()
{
    struct ListNode *TmpCell, *head, *tail;
    int data;
    head = tail = (struct ListNode*)malloc(sizeof(struct ListNode));
    head->next = NULL;
    scanf("%d", &data);
    while (data != -1)
    {
        TmpCell = (struct ListNode*) malloc(sizeof(struct ListNode));
        TmpCell->data = http://www.cnblogs.com/yongwu/p/data;
        tail->next = TmpCell;
        tail = TmpCell;
        scanf("%d", &data);
    }
    tail->next = NULL;
    return head->next;
}

struct ListNode *getodd(struct ListNode **L)
{
    struct ListNode *head, *p, *q, *oddhead, *oddtail, *TmpCell;
    p = head = (struct ListNode*) malloc(sizeof(struct ListNode));
    p->next = *L;
    oddhead = oddtail = (struct ListNode*) malloc(sizeof(struct ListNode));
    oddhead->next = NULL;
    while (p->next != NULL)
    {
        if (p->next->data % 2 != 0)
        {
            q = p->next;
            p->next = q->next;
            TmpCell = (struct ListNode*) malloc(sizeof(struct ListNode));
            TmpCell->data = q->data;
            oddtail->next = TmpCell;
            oddtail = TmpCell;
            free(q);
        }
        else
            p = p->next;
    }
    *L = head->next;
    oddtail->next = NULL;
    return oddhead->next;
}



Tags:C语言程序设计题目集

猜你喜欢

标签列表