zoukankan      html  css  js  c++  java
  • 双向链表的增删改查

    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    using namespace std;
    struct node
    {
        int data;
        node *pre;
        node *next;
    };
    
    void outputList(node *);
    void findData(int, node*);
    node * insertData(int , node *);
    node *deleteData(int , node*);
    int main()
    {
        int n;
        int num;
        node *listHead=NULL;
        node *listTail=NULL;
    
        cin>>n;
    
        for(int i=0; i < n; i++)
        {
    
            cin >> num;
            node *newNode=(node*)malloc(sizeof(node));
    
            if(i==0)
            {
                listHead=newNode;
                listHead->data=num;
                listHead->pre=NULL;
                listHead->next=NULL;
                listTail=listHead;
            }
            else
            {
                newNode->data=num;
                newNode->next=NULL;
                newNode->pre=listTail;
                listTail->next=newNode;
                listTail=newNode;
            }
        }
    
    
        outputList(listHead);
        cout<<"输入的数字";
        cin>>num;
    //    findData(num,listHead);
    //    listHead=deleteData(num,listHead);
       listHead=insertData(num,listHead);
    
        outputList(listHead);
        return 0;
    }
    void outputList(node *head)
    {
        node*curNode=head;
        while(curNode)
        {
              cout<<curNode->data<<" ";
    
              curNode=curNode->next;
        }
    }
    void findData(int n, node* head)
    {
        node* curNode=head;
        while(curNode)
        {
            if(curNode->data==n)
                cout<<"find it!";
            curNode=curNode->next;
        }
    }
    node * insertData(int n, node *head)
    {
        node* curNode=head;
        node* preNode=NULL;
        node* newNode=NULL;
    
        while((curNode != NULL) && (curNode->data<n))
        {
            preNode=curNode;
            curNode=curNode->next;
        }
    
        newNode = new node;
        newNode->data=n;
        if(preNode==NULL)
        {
            newNode->next=curNode;
            newNode->pre=NULL;
            if(curNode != NULL)
                curNode->pre = newNode;
            return newNode;
        }
        if(curNode==NULL)
        {
            newNode->pre=preNode;
            preNode->next=newNode;
            newNode->next=NULL;
            return head;
        }
        else
        {
            preNode->next=newNode;
            newNode->pre=preNode;
            curNode->pre=newNode;
            newNode->next=curNode;
            return head;
        }
    }
    
    node *deleteData(int n, node* head)
    {
        node* curNode = head;
        while(curNode)
        {
            if(curNode->data==n)
            {
                if(curNode->pre == NULL)
                {
                    head=head->next;
                    head->pre=NULL;
                }
                else
                {
                    curNode->pre->next=curNode->next;
                    if(curNode->next != NULL)
                        curNode->next->pre=curNode->pre;
                }
                //cout<<"delete"<<n;
                return head;
            }
            curNode=curNode->next;
        }
        return head;
    }
  • 相关阅读:
    Windows 8.1 应用开发 – 触控操作
    Windows Phone App Studio 无码开发手机应用
    尝试HTML + JavaScript 编写Windows App
    Ubuntu远程连接MySQL(connection refused)解决方法
    [MySQL]查看用户权限与GRANT用法
    mysql 创建用户命令-grant
    搭建memcached使用:/usr/bin/phpize 安装memcached扩展的时候报错
    在linux中使用phpize安装php扩展模块
    跟锦数学全部资料
    33套2020数分高代试题参考解答[2020/04/14更新,更多请关注跟锦数学微信公众号]
  • 原文地址:https://www.cnblogs.com/ZP-Better/p/7050489.html
Copyright © 2011-2022 走看看