双向链表的增删改查

#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;
}

 

转载于:https://www.cnblogs.com/ZP-Better/p/7050489.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值