链表编程练习

深刻感觉,基础很重要,闲暇时间,做简单链表编程练习。

#ifndef __LIST_H_
#define __LIST_H_

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

typedef struct LinkNodeList
{
    int data;
    struct LinkNodeList* next;
} LinkNode;

LinkNode* LinkListCreate(const int n);
LinkNode* LinkListReverse(LinkNode* head);
LinkNode* LinkListReverse1(LinkNode* head);
int IsPalindrome(LinkNode* head);
#endif

 

#include "list.h"

int a[5] = {1, 2, 5, 2, 1};

/*创建链表 n:链表结点个数*/
LinkNode* LinkListCreate(const int n)
{
    int i;
    LinkNode* head;
    LinkNode* pre;
    head = (LinkNode*)malloc(sizeof(LinkNode));
    head->next = NULL;
    pre = head;
    LinkNode* p;
    for (i = 0; i < n; i++)
    {
        p = (LinkNode*)malloc(sizeof(LinkNode));
        p->data = a[i];
        p->next = NULL;
        pre->next = p;
        pre = p;
    }
    return head;
}
/*翻转链表 n:链表头节点*/
LinkNode* LinkListReverse(LinkNode* head)
{
    if ((NULL == head) || (NULL == head->next))
    {
        return NULL;
    }
    LinkNode* pre = NULL;
    LinkNode* Next = NULL;
    LinkNode* Cur = head->next;
    while (Cur != NULL)
    {
        Next = Cur->next;
        Cur->next = pre;
        pre = Cur;
        Cur = Next;
    }
    head->next = pre;
    return head;
}

/*递归翻转链表 n:链表头节点*/
LinkNode* LinkListReverse1(LinkNode* head)
{
    LinkNode* new_head;
    if ((NULL == head) || (NULL == head->next))
    {
        return head;
    }
    else
    {
        new_head = LinkListReverse1(head->next);
        head->next->next = head;
        head->next = NULL;
        return new_head;
    }
}

/*判断是否是回文*/
int IsPalindrome(LinkNode* head)
{
    if (NULL == head)
    {
        return -1;
    }
    /*快慢指针*/
    LinkNode* fast = head->next;
    LinkNode* slow = head->next;
    while (fast != NULL && fast->next != NULL)
    {
        
        fast = fast->next->next;
        slow = slow->next;
    }
    //printf("slow data%d\n", slow->data);
    LinkNode* cur = slow;
    LinkNode* next = NULL;
    LinkNode* pre = NULL;
    while (cur != NULL)
    {
        next = cur->next;
        cur->next = pre;
        pre = cur;
        cur = next;
    }
    
    LinkNode* p1 = head->next;
    LinkNode* p2 = pre;
    while(p1 != NULL)
    {
        if((p1->data) != (p2->data))
        {
            return -1;
        }
        //printf("p1 data%d\n", p1->data);
        //printf("p2 data%d\n", p2->data);
        p1 = p1->next;
        p2 = p2->next;
        
    }
    
    return 0;
}
int main()
{
    LinkNode* head = LinkListCreate(5);
    printf("%d\n", IsPalindrome(head));
    // LinkNode* p = head->next;
    // while(p != NULL)
    // {
        // printf("%d\n", p->data);
        // p = p->next;
    // }
    // LinkNode* p1 = LinkListReverse1(head);
    // while(p1 != NULL)
    // {
        // printf("%d\n", p1->data);
        // p1 = p1->next;
    // }
    while(head != NULL)
    {
        LinkNode* f = head;
        head = head->next;
        free(f);
    }
    return 0;
}

 

posted @ 2019-03-02 17:32  小时候挺菜  阅读(248)  评论(0编辑  收藏  举报