C语言,回文链表判断,单链表转双链表

/******************************************************************************

只是其中一种实现方式

*******************************************************************************/

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

 struct DoubleListNode {
     int val;
     struct DoubleListNode *prev;
     struct DoubleListNode *next;
 };
 
 struct ListNode {
     int val;
     struct ListNode *next;
 };
 
 struct ListNode* init(int val){
     struct ListNode* head = malloc(sizeof(struct ListNode));
     head->next = NULL;
     head->val = val;
     return head;
 }
 
 void insert(struct ListNode* head, int val){
     struct ListNode* p = head;
     while(p->next){
         p = p->next;
     }
     
     struct ListNode* node = malloc(sizeof(struct ListNode));
     node->next = NULL;
     node->val = val;
     p->next = node;
     
 }
 
 struct DoubleListNode* initDouble(int val){
     struct DoubleListNode*head = malloc(sizeof(struct DoubleListNode));
     head->prev = NULL;
     head->next = NULL;
     head->val = val;
     return head;
 }
 
 void insertDouble(struct DoubleListNode* head, int val){
     if (head == NULL){
         head = malloc(sizeof(struct DoubleListNode));
         head->prev = NULL;
         head->next = NULL;
         head->val = val;
         return;
     }
     struct DoubleListNode* p = head;
     while(p->next){
         p = p->next;
     }
     struct DoubleListNode* doubleNode= malloc(sizeof(struct DoubleListNode));
     doubleNode->prev = p;
     doubleNode->next = NULL;
     doubleNode->val = val;
     p->next = doubleNode;
 }

void print(struct ListNode* head){
    struct ListNode* p = head;
    while(p){
        printf("%d ", p->val);
        p = p->next;
    }
}

void printDNode(struct DoubleListNode* head){
    struct DoubleListNode* p = head;
    while(p){
        printf("%d ", p->val);
        p = p->next;
    }
}

int main()
{
    struct ListNode* head = init(1);
    insert(head,2);
    insert(head,3);
    insert(head,3);
    insert(head,2);
    insert(head,1);
    print(head);
    printf("\n");
    
    struct DoubleListNode* doubleHead = initDouble(head->val);
    
    struct ListNode* p = head;
    struct DoubleListNode* dp = doubleHead;
    while(p->next){
        p = p->next;
        insertDouble(dp,p->val);
    }

    printDNode(doubleHead);

    printf("\n");
    struct DoubleListNode* dTail = doubleHead;
    while(dTail->next){
        dTail = dTail->next;
    }
    int ret = 0;
    while(dp){
        if (dp->val == dTail->val){
            dp = dp->next;
            dTail = dTail->prev;
            if (dp == dTail){
                ret = 1;
                break;
            }
        }else{
            break;
        }
    }
    
    printf("%d\n",ret);
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值