欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

数据结构与算法篇 之单链表与双链表

程序员文章站 2024-03-16 15:21:52
...

链表的基础操作,增删查插

感觉最重要的画出来,一步一步的分析,不要凭空想象,不要凭空想象,不要凭空想象

单链表:

#include <stdio.h>
#include <stdlib.h>
#define bool int
#define false 0
#define true 1

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

list * init_list()
{
    struct list* head = (list*)malloc(sizeof(list));
    head->next = NULL;
    head->data = -1;
    return head;
}

list * new_node(int data)
{
    struct list* head = (list*)malloc(sizeof(list));
    head->next = NULL;
    head->data = data;
    return head;
}

int get_list_len(list *head)
{
    if(head == NULL || head->next == NULL) return 0;
    int len = 1;
    list *tmp = head->next;
    while(tmp->next)
    {
        len++;
        tmp = tmp->next;
    }
    return len;
}

bool add_tail(list * head,list * p)
{
    if(head == NULL || p == NULL)
    {
        printf("list or new node is null \n");
        return false;
    }

    list *tmp = head;
    while(tmp->next != NULL)
    {
        tmp = tmp->next;
    }
    tmp->next = p;

    return true;
}


bool add_head(list *head,list *p)
{
    if(head == NULL || p == NULL)
    {
        printf("list or new node is null\n");
        return false;
    }
    list *tmp = head;

    p->next = tmp->next;
    tmp->next = p;

    return true;
}

bool search_node(list *head,int data,bool flag)
{
    if(head == NULL) return false;

    list *p = head->next;
    list *tmp = head;
    while(1)
    {
        if(data == p->data)
        {
            printf("%d is in the list\n",data);
            if(flag == true)
            {
                 tmp->next = p->next;
                 p->next = NULL;
                 free(p);
            }
            break;
        }
        if(p->next == NULL)
        {
            if(data == p->data)
            {
                printf("%d is in the list\n",data);
                if(flag == true)
                {
                    tmp->next = p->next;
                    p->next = NULL;
                    free(p);

                 }
            }
            else
            {
               printf("%d is not in the list\n",data);
            }
            break;
        }
        tmp = p;
        p = p->next;
    }

}

bool delete_tail_node(list *head)
{
    if (head == NULL) return false;
    int i = get_list_len(head) - 2;
    struct list *p = head->next;
    for(;i>0;p = p->next,--i);
    p->next->next = NULL;
    p->next = NULL;
    free(p->next);
}

bool delete_head_node(list *head)
{
    if(head == NULL) return false;

    struct list *p = head;
    struct list *tmp = p->next;
    tmp = p->next ;
    p->next = p->next->next;
    tmp->next = NULL;
    free(tmp);

}

bool delete_appoint_node(list *head,int num)
{
    if(head == NULL) return false;

    if(get_list_len(head) < num) return false;

    struct list *p = head->next;
    struct list *tmp = head;
    num--;
    while((p->next != NULL) && --num)
    {
        p = p->next;
    }
    tmp = p->next;
    p->next = p->next->next;
    tmp->next = NULL;

    free(tmp);
}

bool insert_tail_or_head(list *head,unsigned int num,list *new,bool flag)
{
    if (head == NULL || new == NULL)
    {
        printf("head or new is null");
        return false;
    }
    if(get_list_len(head) < num) return false;

    struct list *p = head;
    if(flag == true)
    {
        while(p->next != NULL && num--)
        {
            p = p->next;
        }
    }
    else
    {
        while(p->next != NULL && --num)
        {
            p = p->next;
        }
    }
    new->next = p->next;
    p->next = new;

    return true;
}

bool show_list(list* head)
{
    if(head == NULL) return false;
    struct list *p = head->next;
    int data;

    for ( ;p->next;p = p->next)
    {
        printf("%d",p->data);
        printf("-->");
    }
    printf("%d",p->data);

    printf("\n");
    return true;
}

int main()
{
    struct list* head = init_list();
    int i = 0;
    for (;i < 10;i++)
    {
        add_head(head,new_node(i));
    }
    printf("start...\n");
    show_list(head);
    printf("end...\n");

    int len = get_list_len(head);
    printf("list len %d\n",len);

    delete_tail_node(head);
    show_list(head);

    delete_head_node(head);
    show_list(head);

    delete_appoint_node(head,2);
    show_list(head);

    len = get_list_len(head);
    printf("list len %d\n",len);

    insert_tail_or_head(head,2,new_node(23),true);
    show_list(head);
    search_node(head,4,true);
    show_list(head);
    return 1;
}

双链表:

#include <stdio.h>
#include <stdlib.h>
#define bool int
#define false 0
#define true 1

typedef struct list
{
    int data;
    struct list *next;
    struct list *prev;
}list;

list * init_list()
{
    struct list* head = (list*)malloc(sizeof(list));
    head->next = NULL;
    head->prev = NULL;
    head->data = -1;
    return head;
}

list * new_node(int data)
{
    struct list* head = (list*)malloc(sizeof(list));
    head->next = NULL;
    head->prev = NULL;
    head->data = data;
    return head;
}

int get_list_len(list *head)
{
    if(head == NULL || head->next == NULL) return 0;
    int len = 1;
    list *tmp = head->next;
    while(tmp->next)
    {
        len++;
        tmp = tmp->next;
    }
    return len;
}

bool add_tail(list * head,list * p)
{
    if(head == NULL || p == NULL)
    {
        printf("list or new node is null \n");
        return false;
    }

    list *tmp = head;
    while(tmp->next != NULL)
    {
        tmp = tmp->next;
    }
    tmp->next = p;
    p->prev = tmp;
    return true;
}


bool add_head(list *head,list *p)
{
    if(head == NULL || p == NULL)
    {
        printf("list or new node is null\n");
        return false;
    }
    list *tmp = head;

    if(tmp->next == NULL)
    {
        tmp->next = p;
        p->prev = tmp;
        p->next = NULL;
        return true;
    }
    p->next = tmp->next;
    p->prev = tmp;
    tmp->next->prev = p;
    tmp->next = p;

    return true;
}

bool search_node(list *head,int data,bool flag)
{
    if(head == NULL) return false;

    list *p = head->next;
    while(1)
    {
        if(data == p->data)
        {
            printf("%d is in the list\n",data);
            if(flag == true)
            {
                 p->next->prev = p->prev;
                 p->prev->next = p->next;

                 p->next = NULL;
                 p->prev = NULL;
                 free(p);
            }
            break;
        }
        if(p->next == NULL)
        {
            if(data == p->data)
            {
                printf("%d is in the list\n",data);
                if(flag == true)
                {
                     p->next->prev = p->prev;
                     p->prev->next = p->next;

                     p->next = NULL;
                     p->prev = NULL;
                     free(p);
                 }
            }
            else
            {
               printf("%d is not in the list\n",data);
            }
            break;
        }
        p = p->next;
    }

}

bool delete_tail_node(list *head)
{
    if (head == NULL) return false;

    struct list *p = head->next;
    for(;p->next;p = p->next);
    p->prev->next = NULL;
    p->prev = NULL;
    p->next = NULL;
    free(p);
}

bool delete_head_node(list *head)
{
    if(head == NULL) return false;

    struct list *p = head;
    struct list *tmp = head->next;
    p->next->next->prev = p;
    p->next = p->next->next;
    tmp->next = NULL;
    tmp->prev = NULL;
    free(tmp);

}

bool delete_appoint_node(list *head,int num)
{
    if(head == NULL) return false;

    if(get_list_len(head) < num) return false;

    struct list *p = head->next;
    struct list *tmp = head;
    while((p->next != NULL) && --num)
    {
        p = p->next;
    }
    p->next->prev = p->prev;
    p->prev->next = p->next;

    p->next = NULL;
    p->prev = NULL;
    free(p);
}

bool insert_tail_or_head(list *head,unsigned int num,list *new,bool flag)
{
    if (head == NULL || new == NULL)
    {
        printf("head or new is null");
        return false;
    }
    if(get_list_len(head) < num) return false;

    struct list *p = head;
    if(flag == true)
    {
        while(p->next != NULL && num--)
        {
            p = p->next;
        }
    }
    else
    {
        while(p->next != NULL && --num)
        {
            p = p->next;
        }
    }
    new->next = p->next;
    new->prev = p;
    p->next->prev = new;
    p->next = new;

    return true;
}

bool show_list(list* head)
{
    if(head == NULL) return false;
    struct list *p = head->next;
    int data;

    for ( ;p->next;p = p->next)
    {
        printf("%d",p->data);
        printf("-->");
    }
    printf("%d",p->data);

    printf("\n");
    return true;
}

int main()
{
    struct list* head = init_list();
    int i = 0;
    for (;i < 10;i++)
    {
        add_head(head,new_node(i));
    }
    printf("start...\n");
    show_list(head);
    printf("end...\n");

    int len = get_list_len(head);
    printf("list len %d\n",len);

    delete_tail_node(head);
    delete_head_node(head);
    delete_head_node(head);

    show_list(head);
    delete_appoint_node(head,2);
    show_list(head);

    len = get_list_len(head);
    printf("list len %d\n",len);

    insert_tail_or_head(head,2,new_node(23),false);
    show_list(head);
    search_node(head,2,true);
    show_list(head);
    return 1;
}