温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

c++带头节点的循环链表及两个循环链表的合并方法是什么

发布时间:2021-12-08 14:34:44 来源:亿速云 阅读:126 作者:iii 栏目:大数据

本篇内容主要讲解“c++带头节点的循环链表及两个循环链表的合并方法是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“c++带头节点的循环链表及两个循环链表的合并方法是什么”吧!

#include <iostream>
using namespace std;
bool cir=0;
#if 1  // 0 为队列一样的链表
typedef struct cirlist
{
    int data;
    struct cirlist *next;
}CIR_LIST;
CIR_LIST* create_list()
{
    CIR_LIST* list = new (CIR_LIST);
    list->data = 999;
    list->next = list;
}
void list_append(CIR_LIST** list,int data) //链表的后面加入数据
{
    CIR_LIST* newlist = new(CIR_LIST);
    newlist->data = data;
    newlist->next =(*list)->next;
    (*list)->next = newlist;
    *list = newlist;  // 类似队列存储 out 1 2 3 4
}
void list_delete(CIR_LIST* list, int data)
{
    CIR_LIST* front = NULL;
    CIR_LIST* now = list->next->next;
    while(now !=list->next)
    {
        if(data ==now->data)
        {
            if(now ==list->next->next)
            {
                list->next->next = now->next;
                delete(now);
                now = list->next->next ;
              //  break;
            }
            else
            {
                front->next = now->next;
                delete(now);
                now = front->next;
               // break;
            }
        }
        else
        {
            front = now;
            now = now->next;
        }
    }
}
void print(CIR_LIST* list)
{
    CIR_LIST*node = list->next;
    //    while(node != list) //4 3 2 1
    //    {
    //        cout<< node->data <<" ";
    //        node = node->next;
    //    }
    while(node->next != list->next)  //1 2 3 4
    {
        cout<< node->next->data <<" ";
        node = node->next;
    }
    cout<<endl;
}
CIR_LIST* destroy_node(CIR_LIST* node)
{
    CIR_LIST* next = node->next;
    delete(node);
    node=NULL;
    return next;
}
void destroy_list(CIR_LIST* list)
{
    CIR_LIST* node = list->next->next;
    while(node !=list->next)
    {
        node = destroy_node(node);
    }
}
CIR_LIST* list_add(CIR_LIST*list, CIR_LIST*list1)
{
    CIR_LIST* head =list->next;
    list->next = list1->next->next;
    CIR_LIST* head1 = list1->next;
    list1->next =  head;
    delete(head1);
    return list1;
}
int list_size(CIR_LIST* list)
{
    int i=0;
    CIR_LIST* node = list->next;
    while(node->next !=list->next)
    {
        i++;
        node=node->next;
    }
    return i;
}
int main ()
{
    CIR_LIST* list = create_list();
    list_append(&list, 1);
    list_append(&list, 2);
    list_append(&list, 3);
    list_append(&list, 4);
    cout<<"list size:"<<" ";
    cout<<list_size(list)<<" ";
    cout<<"print the list:"<<endl;
    print(list);
    CIR_LIST* list1 = create_list();
    list_append(&list1, -1);
    list_append(&list1, -2);
    list_append(&list1, -3);
    list_append(&list1, -4);
    cout<<"list size:"<<" ";
    cout<<list_size(list1)<<" ";
     cout<<"print the list1:"<<endl;
    print(list1);
    CIR_LIST*list3 =  list_add(list, list1);
    cout<<"list size:"<<" ";
    cout<<list_size(list3)<<" ";
    cout<<"print the list3:"<<endl;
    print(list3);
    cout<<"delete 1 and -4:"<<endl;
    list_delete(list3,1);
    list_delete(list3,-4);
    print(list3);
    destroy_list(list3);
    int a=1;
    return 0;
}
#else
typedef int ElemType;
typedef struct Node
{
    ElemType elem;
    struct Node *next;
}Node,*linklist;
//创建循环链表
Node *createList(Node *head,int n)
{
    Node *p;
    int a[]={1,2,3,4,5,6,7,8,9};
    for(int i=0;i<n;i++)
    {
        p=new Node;
        p->elem=a[i];
        p->next=head->next;
        head->next=p;
    }
    return head;
}
//遍历循环链表
void printList(Node *head)
{
    Node *p;
    p=head->next;
    while(p!=head)
    {
        cout<<p->elem<<" ";
        p=p->next;
    }
    cout<<endl;
}
Node* destroy_node(Node *node)
{
    Node* next = node->next;
    delete(node);
    return next;
}
void destroy_list(Node *head)
{
    Node *p;
    p=head->next;
    while(p!=head)
    {
        p=destroy_node(p);
    }
}
int main()
{
    Node *head,*p,*q;
    head=new Node;
    head->elem = 0;
    head->next=head;
    createList(head,4);
    printList(head);
    destroy_list(head);
    return 0;
}
#endif

到此,相信大家对“c++带头节点的循环链表及两个循环链表的合并方法是什么”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++
AI