如果我是一个编程很厉害的人就好了 2022-11-24 16:47 采纳率: 27.3%
浏览 26
已结题

入门链表和结构运行错误,求解

谁知道这个咋改,c++,链表入门
#include <iostream>
using namespace std;
struct student
{
    int no;
    char name;
    float weight;
    student *next;
};


int main()
{
struct student s1={

   1,'li',55.0};
struct student s2={2,'wang',50.0};
struct student s3={3,'liu',60.5};
int *p=0;

student *head=NULL;*p=NULL;
s1.next=&s2;
s2.next=&s3;
s3.next=NULL;
head=&s1;
p=head;

cin>>no;
student*p=head;
while(p!=NULL)
{
    if(p->no==no)
    {cout<<p->no<<" "<<p->name<<" "<<p->weight<<endle;
    break;}
    p=p->next;
}//查找

*p=0;int*p1=0;
while(p!=NULL){
p1=p;
p=p->next;
if(p->no==no)break;}
p1->next=p->next;
p=NULL;//删除

    return 0;
}

运行结果及报错内容

生成已完成,但出现错误。

 *  终端进程启动失败(退出代码: -1)。 
 *  终端将被任务重用,按任意键关闭。 

 *  正在执行任务: C/C++: g++.exe 生成活动文件 

正在启动生成...
C:\mingw64\bin\g++.exe -fdiagnostics-color=always -g C:\Users\HUAWEI\Desktop\abc.cpp -o C:\Users\HUAWEI\Desktop\abc.exe
C:\Users\HUAWEI\Desktop\abc.cpp:14:6: warning: multi-character character constant [-Wmultichar]
    1,'li',55.0};
      ^~~~
C:\Users\HUAWEI\Desktop\abc.cpp:15:22: warning: multi-character character constant [-Wmultichar]
 struct student s2={2,'wang',50.0};
                      ^~~~~~
C:\Users\HUAWEI\Desktop\abc.cpp:16:22: warning: multi-character character constant [-Wmultichar]
 struct student s3={3,'liu',60.5};
                      ^~~~~
C:\Users\HUAWEI\Desktop\abc.cpp: In function 'int main()':
C:\Users\HUAWEI\Desktop\abc.cpp:14:15: error: narrowing conversion of '27753' from 'int' to 'char' inside { } [-Wnarrowing]
    1,'li',55.0};
               ^
C:\Users\HUAWEI\Desktop\abc.cpp:15:33: error: narrowing conversion of '2002873959' from 'int' to 'char' inside { } [-Wnarrowing]
 struct student s2={2,'wang',50.0};
                                 ^
C:\Users\HUAWEI\Desktop\abc.cpp:16:32: error: narrowing conversion of '7104885' from 'int' to 'char' inside { } [-Wnarrowing]
 struct student s3={3,'liu',60.5};
                                ^
C:\Users\HUAWEI\Desktop\abc.cpp:19:23: warning: converting to non-pointer type 'int' from NULL [-Wconversion-null]
 student *head=NULL;*p=NULL;
                       ^~~~
C:\Users\HUAWEI\Desktop\abc.cpp:24:3: error: cannot convert 'student*' to 'int*' in assignment
 p=head;
   ^~~~
C:\Users\HUAWEI\Desktop\abc.cpp:26:6: error: 'no' was not declared in this scope
 cin>>no;
      ^~
C:\Users\HUAWEI\Desktop\abc.cpp:27:9: error: conflicting declaration 'student* p'
 student*p=head;
         ^
C:\Users\HUAWEI\Desktop\abc.cpp:17:6: note: previous declaration as 'int* p'
 int *p=0;
      ^
C:\Users\HUAWEI\Desktop\abc.cpp:30:11: error: request for member 'no' in '* p', which is of non-class type 'int'
     if(p->no==no)
           ^~
C:\Users\HUAWEI\Desktop\abc.cpp:31:15: error: request for member 'no' in '* p', which is of non-class type 'int'
     {cout<<p->no<<" "<<p->name<<" "<<p->weight<<endle;
               ^~
C:\Users\HUAWEI\Desktop\abc.cpp:31:27: error: request for member 'name' in '* p', which is of non-class type 'int'
     {cout<<p->no<<" "<<p->name<<" "<<p->weight<<endle;
                           ^~~~
C:\Users\HUAWEI\Desktop\abc.cpp:31:41: error: request for member 'weight' in '* p', which is of non-class type 'int'
     {cout<<p->no<<" "<<p->name<<" "<<p->weight<<endle;
                                         ^~~~~~
C:\Users\HUAWEI\Desktop\abc.cpp:31:49: error: 'endle' was not declared in this scope
     {cout<<p->no<<" "<<p->name<<" "<<p->weight<<endle;
                                                 ^~~~~
C:\Users\HUAWEI\Desktop\abc.cpp:31:49: note: suggested alternative: 'rename'
     {cout<<p->no<<" "<<p->name<<" "<<p->weight<<endle;
                                                 ^~~~~
                                                 rename
C:\Users\HUAWEI\Desktop\abc.cpp:33:10: error: request for member 'next' in '* p', which is of non-class type 'int'
     p=p->next;
          ^~~~
C:\Users\HUAWEI\Desktop\abc.cpp:39:6: error: request for member 'next' in '* p', which is of non-class type 'int'
 p=p->next;
      ^~~~
C:\Users\HUAWEI\Desktop\abc.cpp:40:7: error: request for member 'no' in '* p', which is of non-class type 'int'
 if(p->no==no)break;}
       ^~
C:\Users\HUAWEI\Desktop\abc.cpp:41:5: error: request for member 'next' in '* p1', which is of non-class type 'int'
 p1->next=p->next;
     ^~~~
C:\Users\HUAWEI\Desktop\abc.cpp:41:13: error: request for member 'next' in '* p', which is of non-class type 'int'
 p1->next=p->next;
             ^~~~

生成已完成,但出现错误。

 *  终端进程启动失败(退出代码: -1)。 
 *  终端将被任务重用,按任意键关闭。 

 *  正在执行任务: C/C++: g++.exe 生成活动文件 

正在启动生成...
C:\mingw64\bin\g++.exe -fdiagnostics-color=always -g C:\Users\HUAWEI\Desktop\abc.cpp -o C:\Users\HUAWEI\Desktop\abc.exe
C:\Users\HUAWEI\Desktop\abc.cpp:16:6: warning: multi-character character constant [-Wmultichar]
    1,'li',55.0};
      ^~~~
C:\Users\HUAWEI\Desktop\abc.cpp:17:22: warning: multi-character character constant [-Wmultichar]
 struct student s2={2,'wang',50.0};
                      ^~~~~~
C:\Users\HUAWEI\Desktop\abc.cpp:18:22: warning: multi-character character constant [-Wmultichar]
 struct student s3={3,'liu',60.5};
                      ^~~~~
C:\Users\HUAWEI\Desktop\abc.cpp: In function 'int main()':
C:\Users\HUAWEI\Desktop\abc.cpp:16:15: error: narrowing conversion of '27753' from 'int' to 'char' inside { } [-Wnarrowing]
    1,'li',55.0};
               ^
C:\Users\HUAWEI\Desktop\abc.cpp:17:33: error: narrowing conversion of '2002873959' from 'int' to 'char' inside { } [-Wnarrowing]
 struct student s2={2,'wang',50.0};
                                 ^
C:\Users\HUAWEI\Desktop\abc.cpp:18:32: error: narrowing conversion of '7104885' from 'int' to 'char' inside { } [-Wnarrowing]
 struct student s3={3,'liu',60.5};
                                ^
C:\Users\HUAWEI\Desktop\abc.cpp:21:23: warning: converting to non-pointer type 'int' from NULL [-Wconversion-null]
 student *head=NULL;*p=NULL;
                       ^~~~
C:\Users\HUAWEI\Desktop\abc.cpp:26:3: error: cannot convert 'student*' to 'int*' in assignment
 p=head;
   ^~~~
C:\Users\HUAWEI\Desktop\abc.cpp:28:6: error: 'no' was not declared in this scope
 cin>>no;
      ^~
C:\Users\HUAWEI\Desktop\abc.cpp:29:9: error: conflicting declaration 'student* p'
 student*p=head;
         ^
C:\Users\HUAWEI\Desktop\abc.cpp:19:6: note: previous declaration as 'int* p'
 int *p=0;
      ^
C:\Users\HUAWEI\Desktop\abc.cpp:32:11: error: request for member 'no' in '* p', which is of non-class type 'int'
     if(p->no==no)
           ^~
C:\Users\HUAWEI\Desktop\abc.cpp:33:15: error: request for member 'no' in '* p', which is of non-class type 'int'
     {cout<<p->no<<" "<<p->name<<" "<<p->weight<<endle;
               ^~
C:\Users\HUAWEI\Desktop\abc.cpp:33:27: error: request for member 'name' in '* p', which is of non-class type 'int'
     {cout<<p->no<<" "<<p->name<<" "<<p->weight<<endle;
                           ^~~~
C:\Users\HUAWEI\Desktop\abc.cpp:33:41: error: request for member 'weight' in '* p', which is of non-class type 'int'
     {cout<<p->no<<" "<<p->name<<" "<<p->weight<<endle;
                                         ^~~~~~
C:\Users\HUAWEI\Desktop\abc.cpp:33:49: error: 'endle' was not declared in this scope
     {cout<<p->no<<" "<<p->name<<" "<<p->weight<<endle;
                                                 ^~~~~
C:\Users\HUAWEI\Desktop\abc.cpp:33:49: note: suggested alternative: 'rename'
     {cout<<p->no<<" "<<p->name<<" "<<p->weight<<endle;
                                                 ^~~~~
                                                 rename
C:\Users\HUAWEI\Desktop\abc.cpp:35:10: error: request for member 'next' in '* p', which is of non-class type 'int'
     p=p->next;
          ^~~~
C:\Users\HUAWEI\Desktop\abc.cpp:41:6: error: request for member 'next' in '* p', which is of non-class type 'int'
 p=p->next;
      ^~~~
C:\Users\HUAWEI\Desktop\abc.cpp:42:7: error: request for member 'no' in '* p', which is of non-class type 'int'
 if(p->no==no)break;}
       ^~
C:\Users\HUAWEI\Desktop\abc.cpp:43:5: error: request for member 'next' in '* p1', which is of non-class type 'int'
 p1->next=p->next;
     ^~~~
C:\Users\HUAWEI\Desktop\abc.cpp:43:13: error: request for member 'next' in '* p', which is of non-class type 'int'
 p1->next=p->next;
             ^~~~

生成已完成,但出现错误。

 *  终端进程启动失败(退出代码: -1)。 
 *  终端将被任务重用,按任意键关闭。 

 *  正在执行任务: C/C++: g++.exe 生成活动文件 

正在启动生成...
C:\mingw64\bin\g++.exe -fdiagnostics-color=always -g C:\Users\HUAWEI\Desktop\abc.cpp -o C:\Users\HUAWEI\Desktop\abc.exe
C:\Users\HUAWEI\Desktop\abc.cpp:16:6: warning: multi-character character constant [-Wmultichar]
    1,'li',55.0};
      ^~~~
C:\Users\HUAWEI\Desktop\abc.cpp:17:22: warning: multi-character character constant [-Wmultichar]
 struct student s2={2,'wang',50.0};
                      ^~~~~~
C:\Users\HUAWEI\Desktop\abc.cpp:18:22: warning: multi-character character constant [-Wmultichar]
 struct student s3={3,'liu',60.5};
                      ^~~~~
C:\Users\HUAWEI\Desktop\abc.cpp: In function 'int main()':
C:\Users\HUAWEI\Desktop\abc.cpp:16:15: error: narrowing conversion of '27753' from 'int' to 'char' inside { } [-Wnarrowing]
    1,'li',55.0};
               ^
C:\Users\HUAWEI\Desktop\abc.cpp:17:33: error: narrowing conversion of '2002873959' from 'int' to 'char' inside { } [-Wnarrowing]
 struct student s2={2,'wang',50.0};
                                 ^
C:\Users\HUAWEI\Desktop\abc.cpp:18:32: error: narrowing conversion of '7104885' from 'int' to 'char' inside { } [-Wnarrowing]
 struct student s3={3,'liu',60.5};
                                ^
C:\Users\HUAWEI\Desktop\abc.cpp:21:23: warning: converting to non-pointer type 'int' from NULL [-Wconversion-null]
 student *head=NULL;*p=NULL;
                       ^~~~
C:\Users\HUAWEI\Desktop\abc.cpp:26:3: error: cannot convert 'student*' to 'int*' in assignment
 p=head;
   ^~~~
C:\Users\HUAWEI\Desktop\abc.cpp:28:6: error: 'no' was not declared in this scope
 cin>>no;
      ^~
C:\Users\HUAWEI\Desktop\abc.cpp:29:9: error: conflicting declaration 'student* p'
 student*p=head;
         ^
C:\Users\HUAWEI\Desktop\abc.cpp:19:6: note: previous declaration as 'int* p'
 int *p=0;
      ^
C:\Users\HUAWEI\Desktop\abc.cpp:32:11: error: request for member 'no' in '* p', which is of non-class type 'int'
     if(p->no==no)
           ^~
C:\Users\HUAWEI\Desktop\abc.cpp:33:15: error: request for member 'no' in '* p', which is of non-class type 'int'
     {cout<<p->no<<" "<<p->name<<" "<<p->weight<<endle;
               ^~
C:\Users\HUAWEI\Desktop\abc.cpp:33:27: error: request for member 'name' in '* p', which is of non-class type 'int'
     {cout<<p->no<<" "<<p->name<<" "<<p->weight<<endle;
                           ^~~~
C:\Users\HUAWEI\Desktop\abc.cpp:33:41: error: request for member 'weight' in '* p', which is of non-class type 'int'
     {cout<<p->no<<" "<<p->name<<" "<<p->weight<<endle;
                                         ^~~~~~
C:\Users\HUAWEI\Desktop\abc.cpp:33:49: error: 'endle' was not declared in this scope
     {cout<<p->no<<" "<<p->name<<" "<<p->weight<<endle;
                                                 ^~~~~
C:\Users\HUAWEI\Desktop\abc.cpp:33:49: note: suggested alternative: 'rename'
     {cout<<p->no<<" "<<p->name<<" "<<p->weight<<endle;
                                                 ^~~~~
                                                 rename
C:\Users\HUAWEI\Desktop\abc.cpp:35:10: error: request for member 'next' in '* p', which is of non-class type 'int'
     p=p->next;
          ^~~~
C:\Users\HUAWEI\Desktop\abc.cpp:41:6: error: request for member 'next' in '* p', which is of non-class type 'int'
 p=p->next;
      ^~~~
C:\Users\HUAWEI\Desktop\abc.cpp:42:7: error: request for member 'no' in '* p', which is of non-class type 'int'
 if(p->no==no)break;}
       ^~
C:\Users\HUAWEI\Desktop\abc.cpp:43:5: error: request for member 'next' in '* p1', which is of non-class type 'int'
 p1->next=p->next;
     ^~~~
C:\Users\HUAWEI\Desktop\abc.cpp:43:13: error: request for member 'next' in '* p', which is of non-class type 'int'
 p1->next=p->next;
             ^~~~


我的解答思路和尝试过的方法

就是想单纯试试学的链表的插入和删除,
用了结构,感觉写的对的啊,是不是电脑编程环境的问题,因为之前就感觉本电脑编程环境有点问题,请多多指教,感谢!

我想要达到的结果

调试好就行

  • 写回答

2条回答 默认 最新

  • 快乐鹦鹉 2022-11-24 17:05
    关注

    用了结构,感觉写的对的啊===快哭了,错的都太基础了啊
    char name; 姓名是字符串,你这定义了一个字符,能对么? 改成 char name[20];
    'wang' --- 这是四个字符,属于字符串,要用双引号。单引号只能是单个字符
    你自己数数,定义了多少个名为p的指针?有int型,有结构型指针。只要保留一个结构型就行了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月9日
  • 已采纳回答 12月1日
  • 创建了问题 11月24日

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?