链表

#include<bits/stdc++.h>
using namespace std;
struct node{
    int n;
    node *pnext;
};
node* create(){
    node *p=NULL;
    p=(node*)malloc(sizeof(p));
    return p;
}
void add(node* pp){
    node *p=NULL;
    node *tem=pp;
    int n;
    cin>>n;
    for(int i=1;i<=n;++i){
        p=(node*)malloc(sizeof(p));
        cin>>p->n;
        p->pnext=NULL;
        tem->pnext=p;
        tem=tem->pnext;
    }
}
node* Find(node *pp){
    node *p=(node *)malloc(sizeof(p));
    p=pp;//Good
    cout<<"输入要查找删除的数:"<<endl;
    int m;
    cin>>m;
    while(p->pnext!=NULL){//Great
        if(p->pnext->n==m) {
            return p;
        }
        p=p->pnext;
    }
    return NULL;
}
//void Delete(node *pp){
//    //node *p=Find();
//    //node *p=(node*)malloc(sizeof(p));
//    node *tem=pp;
//    pp=pp->pnext;
//    cout<<"输入要删除的数:"<<endl;
//    int m;
//    cin>>m;
//    while(pp!=NULL){
//        if(pp->n==m){
//            tem->pnext=pp->pnext;
//            free(pp);
//            pp=tem->pnext;
//        }
//        pp=pp->pnext;
//        tem=tem->pnext;
//    }
//}
void Delete(node *pp){
    node *pd=Find(pp);
    if(pd==NULL) {
        cout<<"没有查找到,无法删除"<<endl;
        return;
    }
    node *q=pd->pnext;
    pd->pnext=q->pnext;
    free(q);
}
void show(node *pp){
    node *p=(node*)malloc(sizeof(p));
    p=pp->pnext;
    while(p!=NULL){
        cout<<p->n<<' ';
        p=p->pnext;
    }
    cout<<endl;

}
int main(){
    node *p=NULL;
    p=create();//pp是头节点的指针
    add(p);
    show(p);
    Delete(p);
    show(p);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值