ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

双向链表

2021-05-28 23:55:00  阅读:113  来源: 互联网

标签:node head list next 链表 双向 front find


// 双向线性链表


#include <iostream>
using namespace std;
typedef struct node
{
    int data;
    struct node * front;
    struct node *next;
}NODE;
typedef struct doublelist
{
    NODE* head;
    NODE* tail;
}LIST;
LIST* create_list()
{
    LIST* list = new LIST;
    list->head = NULL;
    list->tail = NULL;
    return list;
}
NODE* create_node(int data)
{
    NODE* node = new NODE;
    node->data = data;
    node->front = NULL;
    node->next = NULL;
    return node;
}
void list_append(LIST* list, int data)
{
    NODE* node = create_node(data);
    if(list->tail == NULL)
    {
        list->head = node;
        list->tail = node;
    }
    else
    {
        list->tail->next = node;
        node->front = list->tail;
        list->tail = node;
    }
}
void front_print(LIST* list)
{
    NODE* node = NULL;
    for(node=list->head;node;node=node->next)
    {
        cout<<node->data<<" ";
    }
    cout<<endl;
}


void reverse_print(LIST* list)
{
    NODE* node = NULL;
    for(node =list->tail;node;node=node->front)
    {
        cout<<node->data<<" ";
    }
    cout<<endl;
}
NODE* destroy_node(NODE* node)
{
    NODE* next = node->next;
    delete node;
    return next;
}
void clear(LIST* list)
{
    while(list->head)
    {
        list->head = destroy_node(list->head);
    }
    list->tail = NULL;
}
void destroy_list(LIST* list)
{
    clear(list);
    delete list;
}
void list_insertAfter(LIST* list, int data,int pos) //后插 最起码应该第一个后面
{
    NODE* find = NULL;
    for(find =list->head;find;find=find->next)
    {
        if(!--pos)
        {
            NODE* node =create_node(data);
            if(find->next == NULL)
            {
                node->front =list->tail;
                list->tail->next = node; list->tail = node;
            }
            else
            {
                node->front = find;
                node->next = find->next;
                find->next->front =node;
                find->next = node;
            }
        }
    }
}
void list_delete(LIST* list, int data)
{
    NODE* Front = NULL;
    NODE* node = list->head;
    while(node)
    {
        if(data == node->data)
        {
            if(list->head ==node)
            {
                list->head = node->next;
                node->next->front = NULL;
                delete node;
                node = list->head;
            }
            else
            {
                Front->next = node->next;
                if(node->next != NULL)
                {
                    node->next->front = Front;
                    delete node;
                    node = Front->next;
                }
                else
                {
                    list->tail = node->front;
                    delete node;
                }
            }
        }
        else
        {
            Front = node;
            node = node->next;
        }
    }
}
void list_insertFront(LIST* list,int data,int pos)
{
    //NODE* front = NULL;
    NODE* find = NULL;
    for(find=list->head;find;find=find->next)
    {
        if(!--pos)
        {
            NODE* node = create_node(data);
            if(find==list->head)
            {
                node->next = list->head;
                list->head->front = node;
                list->head = node;
            }
            else
            {
                node->next = find;
                node->front = find->front;
                find->front->next = node;
                find->front = node;
            }
        }
    }
}
int main()
{
    LIST* list = create_list();
    for(int i=10;i<=50;i+=10)
    {
        list_append(list, i);
    }
    //    list_insertAfter(list,55,5);
    //    list_insertAfter(list,15,1);
    list_insertFront(list,15,2);
    list_insertFront(list,50,6);
     list_insertAfter(list,10,7);
    front_print(list);
    list_delete(list,10);
    front_print(list);
    reverse_print(list);
    destroy_list(list);
    return 0;
}


标签:node,head,list,next,链表,双向,front,find
来源: https://blog.51cto.com/u_14582976/2829166

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有