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;
    }
  • 相关阅读:
    LeetCode Path Sum
    实训篇-Html-表单练习
    实训篇-Html-frameset框架集
    实训篇-Html-超链接练习
    实训篇-Html-列表练习
    实训篇-Html-表格练习2
    实训篇-Html-表格练习1
    实训篇-Html-超链接a标签使用
    实训篇-Html-多媒体标签
    实训篇-Html-标题,段落,字体
  • 原文地址:https://www.cnblogs.com/ZP-Better/p/7050489.html
Copyright © 2011-2022 走看看