monday_komorebi 2023-03-08 11:54 采纳率: 100%
浏览 19
已结题

链表问题(基础性的)

麻烦大家帮我看看,学编程没多久,问问这个为什么错的(应该是删除有问题),万分感谢;
另外代码有不好的地方也希望大家指出来,真的想进步一点

#include <iostream>
using namespace std;
struct node{
    int number=0;
    node *next;
};
//后插法  没有用到
void creatList(node *L){
    node *p=L;
    int a=0;
    cin>>a;
    while (a!=-1) {
        node *n=new node ();
        n->next=NULL;
        n->number=a;
        p->next=n;
        
        p=p->next;
        cin>>a;
    }
}

//前插法

void createList2(node *L){

    int a=0;
    cin>>a;
    while (a!=-1) {
       node *n=new node ();
       n->next=nullptr;
       n->number=a;
       n->next=L->next;
       L->next=n;
       cin>>a;
    }
}

void deleteNode(node *L,int a){
   node *p=L;
   while (p->next!=nullptr) {

       if(p->next->number==a){
           p->next=p->next->next;
   }
        p=p->next;
    }
   free(p);

}
void printList(node *L){

    node *p=L;
    while (p->next!=nullptr) {
        cout<<p->next->number<<" ";//易错点
        p=p->next;//易错点
    }
}

int main(){
    node *L=new node ();
    L->next=NULL;
    createList2(L);
    int a=0;
    cin>>a;
    deleteNode(L,a);

    printList(L);

    return 0;
}

img

  • 写回答

2条回答 默认 最新

  • 快乐鹦鹉 2023-03-08 12:00
    关注
    
    
    void deleteNode(node *L,int a){
       node *p=L;
       node *q = NULL;
       while (p->next!=nullptr) {
           if(p->next->number==a){
               q = p->next;
               p->next=p->next->next;
               free(q);
               break;
            }
            p=p->next;
        }
    
     
    }
    
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月16日
  • 已采纳回答 3月8日
  • 创建了问题 3月8日

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号