谁知道这个咋改,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;
^~~~
我的解答思路和尝试过的方法
就是想单纯试试学的链表的插入和删除,
用了结构,感觉写的对的啊,是不是电脑编程环境的问题,因为之前就感觉本电脑编程环境有点问题,请多多指教,感谢!
我想要达到的结果
调试好就行