Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

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

$
0
0

/*********************************************************************************
 *      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() ----- */

作者:sonbai 发表于2013-3-12 16:08:28 原文链接
阅读:0 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>