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

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

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

单链表:

#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;
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值