C语言学习历程(四)双向循环链表

首先通过定义结构体。

接着是完整的函数:

#include <stdio.h>
#include <stdlib.h>
#define T 1
#define F 0

typedef int Elementype;
typedef int Status;//函数的返回类型

struct Node//链表结点结构体
{
    Elementype value;
    struct Node* next;//自己指向自己的指针
};

Status init(struct Node** p);
Status insert_head(struct Node* p,Elementype value);
void print(struct Node* p);
Status insert_tail(struct Node* p,Elementype value);
Status insert_index(struct Node* p,int index,Elementype value);
Status delete_index(struct Node* p,int index);
Status delete_value(struct Node* p,Elementype value);
Status update_index(struct Node* p,int index,Elementype value);
Status update_value(struct Node* p,Elementype old_value,Elementype new_value);
Status query_index(struct Node* p,int index);
Status query_value(struct Node* p,Elementype value);

int main()
{
    struct Node* head;
    init(&head);

    int i;
    for(i = 0;i < 10;i++)
    {
        insert_head(head,i+1);
    }
    print(head);

    for(i = 0;i < 10;i++)
    {
        insert_tail(head,i+1);
    }
    print(head);

    insert_index(head,2,99);
    print(head);

    delete_index(head,2);
    print(head);

    delete_value(head,1);
    print(head);

    update_index(head,2,99);
    print(head);

    update_value(head,2,99);
    print(head);

    query_index(head,15);

    query_value(head,99);
    query_value(head,100);

    return 0;
}

Status init(struct Node** p)
{
    struct Node* newnode  = (struct Node*)malloc(sizeof(struct Node));
    if(NULL == newnode)
    {
        return F;
    }
    newnode->next = NULL;
    *p = newnode;
    return T;
}

Status insert_head(struct Node* p,Elementype value)
{
    struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
    if(NULL == newnode)
    {
        return F;
    }
    newnode->value = value;
    newnode->next = p->next;
    p->next = newnode;
    return T;
}

void print(struct Node* p)
{
    while(NULL != p->next)
    {
        printf("%d ",p->next->value);
        p = p->next;
    }
    printf("\n");
}

Status insert_tail(struct Node* p,Elementype value)
{
    struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
    if(NULL == newnode)
    {
        return F;
    }
    newnode->value = value;
    newnode->next = NULL;
    while(NULL != p->next)
    {
        p = p->next;
    }
    p->next = newnode;
    return T;
}

int length(struct Node* p)
{
    int len = 0;
    while(NULL != p->next)
    {
        len++;
        p = p->next;
    }
    return len;
}

Status insert_index(struct Node* p,int index,Elementype value)
{
    if(index < 0 || index > length(p) - 1)//     0 <= index <= length-1
    {
        printf("index is error\n");
        return F;
    }

    struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
    if(NULL == newnode)
    {
        return F;
    }
    newnode->value = value;

    int i;
    for(i = 0;i < index;i++)
    {
        p = p->next;
    }//p指向了index的前一个结点

    newnode->next = p->next;
    p->next = newnode;
    return T;
}

Status delete_index(struct Node* p,int index)//p必须指向链表的开头
{
    if(index < 0 || index > length(p) - 1)
    {
        printf("index is error\n");
        return F;
    }
    int i;
    for(i = 0;i < index;i++)
    {
        p = p->next;
    }

    struct Node* temp = p->next;
    p->next = temp->next;//p->next->next;
    free(temp);

    // struct Node* temp = p->next->next;
    // free(p->next);
    // p->next = temp;

    return T;
}

Status delete_value(struct Node* p,Elementype value)
{
    int i = 0;
    struct Node* head = p;//记录链表的头结点
    while(NULL != p->next)
    {
        if(value == p->next->value)
        {
            delete_index(head,i);
        }
        else
        {
            i++;
            p = p->next;
        }
    }
    return T;
}

Status update_index(struct Node* p,int index,Elementype value)
{
    if(index < 0 || index > length(p) - 1)
    {
        printf("index is error\n");
        return F;
    }
    int i;
    for(i = 0;i < index ;i++)
    {
        p = p->next;
    }
    p->next->value = value;
    return T;
}

Status update_value(struct Node* p,Elementype old_value,Elementype new_value)
{
    while(p->next != NULL)
    {
        if(old_value == p->next->value)
        {
            p->next->value = new_value;
        }
        p = p->next;
    }
}

Status query_index(struct Node* p,int index)
{
    if(index < 0 || index > length(p) - 1)
    {
        printf("index is error\n");
        return F;
    }

    int i;
    for(i = 0;i < index;i++)
    {
        p = p->next;
    }

    printf("query_index(%d) : %d\n",index,p->next->value);
    return T;
}

Status query_value(struct Node* p,Elementype value)
{
    printf("the index of query_value(%d) are : ",value);
    int i = 0;
    int flag = 0;
    while(p->next != NULL)
    {
        if(p->next->value == value)
        {
            flag++;
            printf("%d ",i);
        }
        i++;
        p = p->next;
    }
    if(flag == 0)
    {
        printf("NOT FOUND\n");
        return F;
    }
    printf("\n");
    return T;
}

猜你喜欢

转载自blog.csdn.net/Sucresmile/article/details/81394127
今日推荐