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

全部博文(55)

文章存档

2011年(27)

2009年(3)

2008年(25)

我的朋友

分类: LINUX

2008-05-06 16:56:03

头文件

/*
 * =====================================================================================
 *
 * 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 /* ----- #ifndef __LIST_H__ ----- */


源文件

/*
 * =====================================================================================
 *
 * Filename: list.c
 *
 * Description: the list's creating,printing,reversing,deleting and inserting.
 *
 * Version: 1.0.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 );
LIST_NODE * delete_list ( LIST_NODE * list_head,long number );
LIST_NODE * insert_list ( LIST_NODE *list_head,LIST_NODE *insert );


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


LIST_NODE * creat_list ( void )
{
    LIST_NODE *pre,*cur;

    LIST_NODE *list_head = NULL;

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


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

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

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

        if(list_head == NULL) list_head = cur;
        else pre->next = cur;

        pre = cur;

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

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

    }    

    pre->next = NULL;

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

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


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

    cur = list_head;
    
    while ( cur != NULL ){
        printf ( "%ld %5.1f\n", cur->number,cur->score );
        cur = cur->next;
    }
    
    printf ( "node number:%d\n", n);
}        /* ----- 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)
        exit(0);

    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: delete_list
 * Description:
 * =====================================================================================
 */


LIST_NODE * delete_list ( LIST_NODE * list_head,long number )
{
    LIST_NODE *pre,*cur;

    cur = list_head;
    
    if ( list_head == NULL ) {
        printf ( "error: list is null\n" );
        exit(EXIT_FAILURE);
    }

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

    if ( cur->number == number ) {
        if ( cur == list_head ) {
            list_head = list_head->next;
        }
        else pre->next = cur->next;
    }
    
    n = n-1;
    
    return(list_head);    
}        /* ----- end of function delete_list ----- */

/*
 * === FUNCTION ======================================================================
 * Name: insert_list
 * Description:
 * =====================================================================================
 */


LIST_NODE * insert_list ( LIST_NODE *list_head,LIST_NODE *insert )
{
    LIST_NODE *pre,*cur;

    cur = list_head;

    //list is NULL

    if ( list_head == NULL ) {
        list_head = insert;
        insert->next = NULL;
    }
    //list isn't NULL

    else {
        while ( ( insert->number > cur->number ) && ( cur->next != NULL) ) {
            pre = cur;
            cur = cur->next;
        }

        //the end of list

        if ( ( insert->number > cur->number ) && ( cur->next == NULL ) ) {
            cur->next = insert;
            insert->next = NULL;
        }
        else if ( insert->number <= cur->number ) {
            //the head of list

            if ( cur == list_head ) {
                list_head = insert;
                insert->next = cur;
            }
            else {
                pre->next = insert;
                insert->next = cur;
            }
        }
    }

    n += 1;

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

/*
 * === FUNCTION ======================================================================
 * Name: main
 * Description:
 * =====================================================================================
 */


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

    long num;

    LIST_NODE *insert;

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

    //creat list

    list_head = creat_list();

    printf ( "print data\n" );

    //print list

    print_list(list_head);

    // delete node

    printf( "please input the delete number:");
    scanf ( "%ld", &num );

    while( num != 0 ){

        list_head = delete_list(list_head,num);

        printf ( "print data\n" );

        print_list(list_head);

        printf( "please input the delete number:");

        scanf ( "%ld", &num );

    }


    //insert node

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

    printf ( "please input the add node:" );
    scanf ( "%ld,%f", &insert->number,&insert->score );
 
    while ( insert->number != 0 ) {
        list_head = insert_list(list_head,insert);

        printf ( "print data: \n" );

        print_list(list_head);

        insert = (LIST_NODE *)malloc ( sizeof(LIST_NODE) );
        if ( insert==NULL ) {
            fprintf ( stderr, "\ndynamic memory allocation failed\n" );
            exit (EXIT_FAILURE);
        }
        
        printf ( "please input the add number:" );

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

    }

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

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