资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

C++双向链表怎么实现

本篇内容主要讲解“C++双向链表怎么实现”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C++双向链表怎么实现”吧!

10年积累的成都网站设计、成都做网站经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站制作后付款的网站建设流程,更有泌阳免费网站建设让你可以放心的选择与我们合作。

C++双向链表怎么实现

#include 
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<data<<" ";
    }
    cout<tail;node;node=node->front)
    {
        cout<data<<" ";
    }
    cout<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;
}

到此,相信大家对“C++双向链表怎么实现”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!


文章标题:C++双向链表怎么实现
当前URL:http://cdkjz.cn/article/ihjdhe.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220