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

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

程序员文章站 2024-03-16 15:26:46
...

链表的基础操作,增删查插 感觉最重要的画出来,一步一步的分析,不要凭空想象,不要凭空想象,不要凭空想象

循环单链表:

#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 = head;
    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 ) return 0;
    int len = 0;
    list *p = head;
    list *tmp = head->next;
    while(tmp->next)
    {
        len++;
        tmp = tmp->next;
        if(p == tmp)
            break;
    }
    return len;
}

bool add_tail(list * head,list * p)
{
    if(head == NULL || p == NULL)
    {
        printf("list or new node is null \n");
        return false;
    }
    int len = get_list_len(head);
    list *tmp = head;
    while(len--)
    {
        tmp = tmp->next;
    }
    p->next = head;
    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 == head)
        {
            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) - 1;
    struct list *p = head;

    while(i--)
    {
        p = p->next;
    }
    p->next->next = NULL;
    p->next = head;

    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;
    int len = 0;
    if(len = get_list_len(head) < num) return false;

    struct list *p = head->next;
    struct list *tmp = head;
    num--;
    while((--len) && (--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(num--)
        {
            p = p->next;
        }
    }
    else
    {
        while(--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 i = get_list_len(head);
    for ( ;i > 0;p = p->next,--i)
    {
        printf("%d",p->data);
        printf("-->");
    }

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

int main()
{
    struct list* head = init_list();
    int i = 0;
    for (;i < 10;i++)
    {
        add_tail(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,2,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 = head;
    head->prev = head;
    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) return 0;
    int len = 0;
    list *p = head;
    list *tmp = head->next;
    while(tmp->next)
    {
        len++;
        tmp = tmp->next;
        if(p == tmp)
            break;
    }
    return len;
}

bool add_tail(list * head,list * p)
{
    if(head == NULL || p == NULL)
    {
        printf("list or new node is null \n");
        return false;
    }
    int len = get_list_len(head);
    list *tmp = head;
    while(len--)
    {
        tmp = tmp->next;
    }
    p->next = head;
    p->prev = tmp;
    head->prev = p;
    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;
    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 == head)
        {
            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;
    int i = get_list_len(head) - 1;
    while(i--)
    {
        p = p->next;
    }
    p->next->prev = p->prev;
    p->prev->next = p->next;
    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( --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(num--) p = p->next;
    }
    else
    {
        while(--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 != head;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_tail(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;
}