数据结构---顺序表

来源:互联网 发布:淘宝怎样和卖家联系 编辑:程序博客网 时间:2024/06/05 05:24

采用指针来实现

链式存储的链表


ChainListType *ChainListAddEnd(ChainListType *head,DATA data)

{
    ChainListType *node,*h;
    if(!(node =(ChainListType *)malloc(sizeof(ChainListType)) ))
{
    printf("malloc error");
    return NULL;
}

    node->data = data;
    node->next = NULL;
    
    if(head == NULL)
    {
        head = node;
        return head;
    }
    h = head;
    while(h->next != NULL)
    h = h->next;
    
    h->next = node;
    return head;

}

ChainListType *ChainListAddFirst(ChainListType *head,DATA data)
{
    ChainListType *node,*h;
    if(!(node =(ChainListType *)malloc(sizeof(ChainListType)) ))
{
    printf("malloc error");
    return NULL;
}

    node->data = data;
    node->next = head;
    head = node;
    return head;
}

ChainListType *ChainListInsert(ChainListType *head,char *findkey,DATA data)
{
    ChainListType *node,*node1;
    if(!(node = (ChainListType *)malloc(sizeof(ChainListType) ) ))
{
    printf("malloc error");
    return 0;
}

    node->data = data;
    node1 = ChainListFind(head,findkey);
    if(node1)
    {
        node->next = node1->next;
        node1->next = node;

    }
    else
    {
        free(node);
        printf("not find position");
    }
    
    return head;    
}

ChainListType *ChainListFind(ChainListType *head,char *key)
{
    ChainListType *h;
    h = head;
    while(h)
    {
        if(strcmp(h->data.key,key) == 0 )
         return h;
        h = h->next;
    }
    return NULL;
}

int ChainListDelete(ChainListType *head,char *key)
{
    ChainListType *node,*h;
    node=h=head;
    while(h)
    {
        if(strcmp(h->data.key,key) == 0)
        {
            node->next = h->next;
            free(h);

            return 1;    
        }
        else
        {
            node = h;
            h = h->next;

        }
    }
    return 0;
}

int ChainListLength(ChainListType *head)
{
    ChainListType *h;
    int i = 0;
    h = head;
    while(h)
    {
        i++;
        h = h->next;
    }
    return i;
}
0 0
原创粉丝点击