当前位置:编程学习 > 网站相关 >>

单链表创建、排序(升序)

 


代码实现如下:

#include <stdio.h>
#include <stdlib.h>

typedef struct LNode{
    int data;
    struct LNode *next;
}LNode;

LNode *sort_link_list_increasing_order(LNode *pheader)
{
    if(pheader == NULL || pheader->next == NULL || pheader->next->next == NULL)
        return NULL;


            /*未实现*/

    return (LNode *)0;
}


//创建链表
LNode *create_link_list(LNode **pheader)
{
    int i;
    LNode *p,*tmp;
    if(pheader == NULL)
        return NULL;
    tmp = *pheader;
    for (i=1; i<=10; i++) {
        p = (LNode *)malloc(sizeof(LNode));
        if (p == NULL)
            return NULL;
        p->data = i;
        p->next = NULL;
        tmp->next = p;
        tmp = p;
    }
    return *pheader;
}


//删除链表,释放分配的内存
int delete_link_list(LNode *pheader)
{
    if(pheader == NULL)
        return -1;
    free(pheader);
    return 0;
}


//打印链表
int print_link_list(LNode *pheader)
{
    if(pheader == NULL || pheader->next == NULL)
        return -1;
    LNode *p = pheader->next;
    while(p != NULL) {
        printf("%d ",p->data);
        p = p->next;
    }
    printf("\n");
    return 0;
}

int main()
{
    LNode *pheader = NULL;
    pheader = (LNode *)malloc(sizeof(LNode));
    if (pheader == NULL)
        return -1;
    pheader->data = -1;
    pheader->next = NULL;


    pheader = create_link_list(&pheader);
    print_link_list(pheader);
    sort_link_list_increasing_order(pheader);
    print_link_list(pheader);
    delete_link_list(pheader);
    return 0;
}


 

补充:综合编程 , 其他综合 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,