数据结构之单链表操作二

来源:互联网 发布:js自动触发a 标签事件 编辑:程序博客网 时间:2024/06/06 18:46

上一篇文章介绍了单链表的一些基本操作,包括节点的创建与销毁,链表的初始化与摧毁,以及头插法和尾插法添加节点和正反向打印链表等,这篇文章深入介绍单链表其他的操作。

链表的中间数据插入

void inse_data(LIST* list, int data){    LIST_NODE* node = create_node(data);    LIST_NODE* p = NULL;    if(list->head)    {        for(p = list->head; p; p = p->next)        {            if(p->data > node->data)            {                node->next = list->head;                list->head = node;                break;            }            if(p->data <= node->data)            {                node->next = p->next;                p->next = node;                break;            }        }    }}

链表的节点位置插入

void inse_node(LIST* list, int data, int num){    int i = 1;    LIST_NODE* p = NULL;    LIST_NODE* node = create_node(data);    if(list->head)    {        p = list->head;        if(num == 0)        {            node->next = list->head;            list->head = node;        }        else if(num > (int)list_size(list))        {            printf("The length is not enough!\n");            exit(0);        }        else        {            for(p; i < num ; ++i)            {                p = p->next;            }            node->next = p->next;            p->next = node;        }    }}

链表的节点位置插入

void delete_node(LIST* list, int num){    int i = 1;    LIST_NODE* p = NULL;    LIST_NODE* p1 = NULL;    if(list->head)    {        p = list->head;        if(num > (int)list_size(list))        {            printf("The length is not enough!\n");            exit(0);        }        else if(num == 1)        {            list->head = list->head->next;            free(p);        }        else        {            p1 = list->head;            int j = 0;            for(i=1; i < num ; ++i)            {                p = p->next;            }            for(j = 1; j < num - 1; ++j)            {                   p1 = p1->next;            }            p1->next = p->next;            free(p);        }    }    else    {        printf("NULL!\n");    }}

链表的冒泡排序

void bubble(LIST* list){    int i;    int j;    LIST_NODE *p = list->head;    LIST_NODE *q = NULL;    //printf("%d\n", (int)list_size(list));    for(i = 0; i < (int)list_size(list); ++i)    {        for(j = 0; j < (int)list_size(list) && p->next; ++j)        {            if(p->data > p->next->data)            {                int swap = p->data;                p->data = p->next->data;                p->next->data = swap;            //  printf("%d %d\n", p->data, p->next->data);            }            p = p->next;        }        p = list->head;    }}

链表的数据有序插入

void list_insert(LIST* list, int data){    LIST_NODE* node = create_node(data);    if(!list->head && !list->tail)    {        list->head = list->tail = node;       }    else if(node->data <= list->head->data)    {        node->next = list->head;        list->head = node;    }    else if(list->tail->data <= node->data)    {        list->tail->next = node;        list->tail = node;    }    else    {        LIST_NODE* find = NULL;        for(find = list->head; find; find = find->next)        {            if(node->data <= find->next->data)            {                node->next = find->next;                find->next = node;                break;            }        }    }}

链表的数据连接

void sort_connect(LIST* list1, LIST* list2){    if(!list1->head || !list1->tail)    {        printf("NULL!\n");        exit(0);    }    if(!list2->head || !list2->tail)    {        printf("list2 is NULL!\n");        exit(1);    }    LIST_NODE* node1 = list1->tail;    LIST_NODE* node = list2->head;    if(node1 && node)    {         node1->next =  node;    }    list1->tail = NULL;    list2->head = NULL;}

求链表的中间值

int list_middle(LIST* list){    LIST_NODE* slow = NULL;    LIST_NODE* fast = NULL;    for(slow = fast = list->head; fast->next && fast->next->next; fast = fast->next->next)    {        slow = slow->next;    }    return slow->data;}

将两个链表连接起来并排序的测试代码

void test9(void){    LIST list1;    LIST list2;    int i;    list_init(&list1);    list_init(&list2);    srand(time(NULL));    for(i = 0; i < 10; ++i)    {        list_append(&list1, rand() % 100);    }    list_print(&list1);    for(i = 0; i < 10; ++i)    {        list_append(&list2, rand() % 100);    }    list_print(&list2);    sort_connect(&list1, &list2);    list_print(&list1);    bubble(&list1);    list_print(&list1);    list_deinit(&list1);    list_deinit(&list2);}

输出结果:

32 49 50 48 45 85 94 5 80 5437 71 9 62 21 98 22 52 61 3432 49 50 48 45 85 94 5 80 54 37 71 9 62 21 98 22 52 61 345 9 21 22 32 34 37 45 48 49 50 52 54 61 62 71 80 85 94 98

这些程序经过测试基本没问题,但是还可能存在小bug,希望读者能及时提醒。定义链表以及节点的结构体在上一篇博客中:http://blog.csdn.net/peter_tang6/article/details/76376346

阅读全文
1 0