Chinaunix首页 | 论坛 | 博客
  • 博客访问: 226438
  • 博文数量: 42
  • 博客积分: 2618
  • 博客等级: 少校
  • 技术积分: 385
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-26 10:04
文章分类

全部博文(42)

文章存档

2013年(2)

2012年(2)

2011年(3)

2010年(17)

2009年(18)

我的朋友

分类: C/C++

2010-01-08 12:51:12

#include <stdio.h>
#include <malloc.h>

/*
*带表头的单链表
*/

struct list{
        int num;
        struct list *next;
};

void create_list(struct list **list,int num){

        *list = (struct list *)malloc(sizeof(struct list));
        if(NULL == *list){
                printf("malloc failed\n");
        }
        (*list)->next = NULL;
        (*list)->num = num;
}

void destory_node(struct list *list)
{
        if(list)
                free(list);
}

void destory_list(struct list **head)
{
        struct list *ptr = *head;
        struct list *ptr2;

        if(NULL == *head)
                return;
        while(ptr){
                ptr2 = ptr->next;
                printf("destory %d\n",ptr->num);
                destory_node(ptr);
                ptr = ptr2;
        }
        *head = NULL;
}

void insert_list(struct list **head,struct list *new)
{
        struct list *ptr = *head;
        struct list *backup;

        if(NULL == head || NULL == new)
                return ;

        while(ptr && ptr->num < new->num){
                backup = ptr;
                ptr = ptr->next;
        }

        if(ptr == *head){
                new->next = *head;
                *head = new;
        }
        else{
                new->next = backup->next;
                backup->next = new;
        }

}

void print_list(struct list *head)
{
        struct list *ptr = head;

        if(NULL == head)
                return ;
        while(ptr)
        {
                printf("%d\t",ptr->num);
                ptr = ptr->next;
        }
        printf("\n\n");
}

void print_list_convert(struct list *head)
{
        //struct list *ptr = head;
        if(head){
                print_list_convert(head->next);
                printf("%d\t",head->num);
        }
}

void convert_list(struct list **head)
{
        struct list *ptr = *head;
        struct list *index = ptr->next;
        struct list *backup;

        while(index){
                backup = index;
                index = index->next;
                backup->next = ptr;
                if(ptr == *head)
                        ptr->next = NULL;
                ptr = backup;
        }

        *head = ptr;
}

int main(int argc,char *argv[])
{
        struct list *head,*new;

        create_list(&head,-1);

        create_list(&new,10);
        insert_list(&head,new);

        create_list(&new,100);
        insert_list(&head,new);

        create_list(&new,-90);
        insert_list(&head,new);

        create_list(&new,80);
        insert_list(&head,new);

        create_list(&new,5);
        insert_list(&head,new);

        create_list(&new,-1000);
        insert_list(&head,new);

        print_list(head);
        print_list_convert(head);
        printf("\n");

        convert_list(&head);
        print_list(head);

        destory_list(&head);
        print_list(head);

        return 0;
}


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