Chinaunix首页 | 论坛 | 博客
  • 博客访问: 128621
  • 博文数量: 55
  • 博客积分: 1870
  • 博客等级: 上尉
  • 技术积分: 540
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-21 20:51
文章分类

全部博文(55)

文章存档

2011年(27)

2009年(3)

2008年(25)

我的朋友

分类: LINUX

2008-04-29 09:07:23

H是一个单链表的头指针,使用指针P,Q使链表反转,写出程序
1 按照题目要求。(只能使用两个指针)
2 代码正确的同时考虑特殊情况。比如H为NULL
3 代码尽量简洁。批改面试题目的人不会有耐心看完冗长的代码的。越短的代码越有吸引力。
4 当然必要的注视也是必须的。
 

头文件

/*
 * =====================================================================================
 *
 * Filename: list.h
 *
 * Description:
 *
 * Version: 1.0
 * Created: 04/28/08 10:49:23
 * Revision: none
 * Compiler: gcc
 *
 * Author: Engelbert (mr), engelbert@yahoo.cn
 * Company: WAVETEK
 *
 * =====================================================================================
 */


#ifndef __LIST_H__
#define __LIST_H__

struct list_node{
    long number;
    float score;
    struct list_node * next;
};

typedef struct list_node LIST_NODE;

#endif

源文件

/*
 * =====================================================================================
 *
 * Filename: list.c
 *
 * Description:
 *
 * Version: 1.0
 * Created: 04/28/08 10:49:15
 * Revision: none
 * Compiler: gcc
 *
 * Author: Engelbert (mr), engelbert@yahoo.cn
 * Company: WAVETEK
 *
 * =====================================================================================
 */



/*-----------------------------------------------------------------------------
 * Include
 *-----------------------------------------------------------------------------*/

#include    <stdio.h>
#include    <stdlib.h>
#include    <malloc.h>
#include    "list.h"

/*-----------------------------------------------------------------------------
 * Variable
 *-----------------------------------------------------------------------------*/

int n=0;

/*-----------------------------------------------------------------------------
 * Function prototype
 *-----------------------------------------------------------------------------*/

LIST_NODE * creat_list ( void );
void print_list ( LIST_NODE * list_head );
LIST_NODE * reverse_list ( LIST_NODE * list_head );

/*
 * === FUNCTION ======================================================================
 * Name: creat_list
 * Description:
 * =====================================================================================
 */


LIST_NODE * creat_list ( void )
{
    LIST_NODE *pre_node,*new_node;

    LIST_NODE *list_head = NULL;

    pre_node = (LIST_NODE *)malloc ( sizeof(LIST_NODE) );
    if ( pre_node == NULL ) {
        fprintf ( stderr, "\ndynamic memory allocation failed\n" );
        exit (EXIT_FAILURE);
    }


    new_node = (LIST_NODE *)malloc ( sizeof(LIST_NODE) );
    if ( new_node == NULL ) {
        fprintf ( stderr, "\ndynamic memory allocation failed\n" );
        exit (EXIT_FAILURE);
    }

    scanf ( "%ld,%f", &(new_node->number),&(new_node->score) );

    while( (new_node->number) != 0 ){
        n++;

        if(list_head == NULL) list_head = new_node;
        else pre_node->next = new_node;

        pre_node = new_node;

        new_node = (LIST_NODE *)malloc ( sizeof(LIST_NODE) );
        if ( new_node == NULL ) {
            fprintf ( stderr, "\ndynamic memory allocation failed\n" );
            exit (EXIT_FAILURE);
        }

        scanf ( "%ld,%f", &(new_node->number),&(new_node->score) );

    }    

    pre_node->next = NULL;

    return(list_head);
}        /* ----- end of function creat_list ----- */



/*
 * === FUNCTION ======================================================================
 * Name: print_list
 * Description:
 * =====================================================================================
 */


void print_list ( LIST_NODE * list_head )
{
    LIST_NODE * p;

    p = list_head;
    
    while ( p != NULL ){
        printf ( "%ld %5.1f\n", p->number,p->score );
        p = p->next;
    }
}        /* ----- end of function print_list ----- */

/*
 * === FUNCTION ======================================================================
 * Name: reverse_list
 * Description:
 * =====================================================================================
 */


LIST_NODE * reverse_list ( LIST_NODE * list_head )
{
    LIST_NODE *pre,*cur;     
    
    if(list_head == NULL)
        return;

    pre = list_head;
    cur = list_head->next;
    pre->next = NULL;

    while(cur != NULL){
        list_head = cur->next;
        cur->next = pre;
        pre = cur;
        cur = list_head;
    }

    list_head = pre;

    return (list_head);
}        /* ----- end of function inverse_list ----- */
/*
 * === FUNCTION ======================================================================
 * Name: main
 * Description:
 * =====================================================================================
 */


int main ( int argc, char *argv[] )
{
    LIST_NODE *list_head;

    printf ( "please insert data!(data1,data2)\n" );

    list_head = creat_list();

    printf ( "print data\n" );

    print_list(list_head);

    list_head = reverse_list(list_head);
    
    printf ( "print reverse data\n" );
    
    print_list(list_head);

    return EXIT_SUCCESS;
}                /* ---------- end of function main ---------- */

阅读(760) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~