zoukankan      html  css  js  c++  java
  • 双链表

    双链表的创建/删除/插入/打印操作

    #include <iostream>
    using namespace std;
    
    struct node
    {
        int data;
        node *next;
        node *pre;
    };
    //(1)创建双链表
    node *create()
    {
        node *head,*p,*s;
        int x;
        char c;
        head=new node;
        p=head;
        cout<<"please input the datas:"<<endl;
        while(cin>>x)
        {
            s=new node;
            s->data=x;
            p->next=s;
            s->pre=p;
            p=s;
            cin.get(c);
            if(c=='
    ') break;
        }
        head=head->next;
        head->pre=NULL;
        p->next=NULL;
        return head;
    }
    //(2)打印双链表
    void print_list(node *head)
    {
        node *p;
        p=head;
        while(p!=NULL)
        {
            cout<<p->data<<endl;
            p=p->next;
        }
    }
    //(3)双链表的删除节点操作
    node *delete_node(node *head,int num)
    {
        node *p;
        p=head;
    
        while(num!=p->data && p->next!=NULL)
        {
            p=p->next;             //跟单链表删除的区别:不需要一个指针p2来保存p的前驱节点
        }
        if(num==p->data)
        {
            if(p==head)
            {
                head=head->next;
                head->pre=NULL;
                delete p;
            }
            else if(p->next==NULL)
            {
                p->pre->next=NULL;
                delete p;
            }
            else
            {
                p->next->pre=p->pre;
                p->pre->next=p->next;
                delete p;
            }
        }
        else
            cout<<"not found"<<endl;
        return head;
    }
    //(4)双链表的插入操作
    node *insert_node(node *head,int num)
    {
        node *p=head;
        node *p2;
        p2=new node;
        while(p->data<num && p->next!=NULL)
        {
            p=p->next;
        }
        if(p->data>=num)
        {
            if(p==head)
            {
                p2->data=num;
                p->pre=p2;
                p2->next=p;
                p2->pre=NULL;
    
                head=p2;
            }
            else
            {
                p2->data=num;
                p2->pre=p->pre;
                p2->next=p;
                p->pre->next=p2;
            }
        }
        else
        {
            //p->next->data=num;
            //p->next->pre=p;
            //p->next->next=NULL;
            p2->data=num;
            p->next=p2;
            p2->pre=p;
            p2->next=NULL;
        }
        return head;
    }
    
    int main()
    {
        node *p=create();
        print_list(p);
        //node *p1=delete_node(p,1);
        //print_list(p1);
        node *p2=insert_node(p,3);
        print_list(p2);
        return 0;
    }
  • 相关阅读:
    汉字乱码、加密后结果字符串不一致
    msgpack和TParams互相转换
    unigui监听会话开始和结束
    System.JSON.Builders.pas
    保证最终一致性的模式
    使用 Delta Sharing 协议进行数据共享
    dremio 16 升级问题
    graylog 4.0 运行
    supabase 开源firebase 可选工具
    cube.js 最新playground 说明
  • 原文地址:https://www.cnblogs.com/riden/p/4564438.html
Copyright © 2011-2022 走看看