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;
    }
  • 相关阅读:
    嵌套使用Using Statement造成对象被dispose多次 CA2202
    ASP.NET 4.0: 请求验证模式变化导致ValidateRequest=false失效
    IIS 7.0的集成模式和经典模式
    设计模式之—简单工厂设计模式
    c#总结(一)
    数据库分离附加工具
    深入理解C#之 参数传递 ref out params
    ASP.NET MVC 学习笔记(一)
    C#实现根据IP 查找真实地址
    c# 新特性
  • 原文地址:https://www.cnblogs.com/ZP-Better/p/7050489.html
Copyright © 2011-2022 走看看