您的位置:首页 > 其它

简单的单链表逆序(list.c)

2013-03-12 16:08 225 查看
/*********************************************************************************

 *      Copyright:  (C) 2013 fulinux<fulinux> 

 *                  All rights reserved.

 *

 *       Filename:  list.c

 *    Description:  This file

 *                

 *        Version:  1.0.0(2013年03月12日~)

 *         Author:  fulinux <fulinux@sina.com>

 *      ChangeLog:  1, Release initial version on "2013年03月12日 12时59分15秒"

 *                

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

#include <stdio.h>

#include <stdlib.h>

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

 *  Description:

 *   Input Args:

 *  Output Args:

 * Return Value:

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

typedef struct _LINK_NODE

{

    int data;

    struct _LINK_NODE *next;

}LINK_NODE;

struct _LINK_NODE var;

LINK_NODE *create_list();

void travel_list(LINK_NODE *head);

LINK_NODE *revers_list(LINK_NODE *head);

int main (int argc, char **argv)

{

    LINK_NODE *head;

    head = create_list();

    travel_list(head);

    head = revers_list(head);

    travel_list(head);

    return 0;

} /* ----- End of main() ----- */

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

 *  Description:

 *   Input Args:

 *  Output Args:

 * Return Value:

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

struct _LINK_NODE *create_list()

{

    int i;

    char a;

    LINK_NODE *head,*rear,*p;

    head = (LINK_NODE *)malloc(sizeof(LINK_NODE));

    rear = head;

    for(i = 0;i < 5;i++)

    {

        p = (LINK_NODE *)malloc(sizeof(LINK_NODE));

        p->data = i;

        rear->next = p;

        rear = p;

    }

    rear->next = NULL;

 list.c                                                                                             

    return head->next;

} /* ----- End of create_list() ----- */

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

 *  Description:

 *   Input Args:

 *  Output Args:

 * Return Value:

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

void travel_list(LINK_NODE *head)

{

    LINK_NODE *p;

    p = head;

    while(p)

    {

        printf("%d",p->data);

        p = p->next;

    }

    printf("\n");

} /* ----- End of trave_list() ----- */

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

 *  Description:

 *   Input Args:

 *  Output Args:

 * Return Value:

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

LINK_NODE *revers_list(LINK_NODE *head)

{

    int i = 1;

    LINK_NODE *tail,*p,*q;

    p = head;

    while(head->next)

    {

        p = head;

        do

        {

           q = p;

           p = p->next;

        }while(p->next);

        if(1 == i)

        {

            i = 0;

            tail = p;

        }

        p->next = q;

        q->next = NULL;

    }

    return tail;

} /* ----- End of revers_list() ----- */
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: