RT。
最近比较颓,指针太拉了,所以随便打了个链表板子。
结果。。。
求大家调一调,代码如下:
#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;//这个节点的数
struct node *next;//下一个节点
};
int main()
{
struct *head,*q,*p,*t;
head=NULL;
int n,a;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&n);
p=(struct node *)malloc(sizeof(struct node));
p->data=a;
p->next=NULL;
if(head==NULL)
head=p;
else
q->next=p;
q=p;
}
cin>>a;
t=head;
while(t!=NULL)
{
if(t->next==NULL || t->next->data > a)
{
p=(struct node *)malloc(sizeof(struct node));
p->data=a;
p->next=t->next;
t->next=p;
}
t=t->next;
}
t=head;
while(t!=NULL)
{
printf("%d ",t->data);
t=t->next;
}
return 0;
}
码风应该还好吧,望大家查一下!
CE提示(洛谷ide测的):
/tmp/compiler_00iwb1w7/src: 在函数‘int main()’中:
/tmp/compiler_00iwb1w7/src:19:11: 错误:cannot convert ‘node*’ to ‘int*’ in assignment
19 | p=(struct node *)malloc(sizeof(struct node));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| |
| node*
/tmp/compiler_00iwb1w7/src:20:20: 错误:对成员‘data’的请求出现在‘* p’中,而后者具有非类类型‘int’
20 | p->data=a;
| ^~~~
/tmp/compiler_00iwb1w7/src:21:20: 错误:对成员‘next’的请求出现在‘* p’中,而后者具有非类类型‘int’
21 | p->next=NULL;
| ^~~~
/tmp/compiler_00iwb1w7/src:25:28: 错误:对成员‘next’的请求出现在‘* q’中,而后者具有非类类型‘int’
25 | q->next=p;
| ^~~~
/tmp/compiler_00iwb1w7/src:32:23: 错误:对成员‘next’的请求出现在‘* t’中,而后者具有非类类型‘int’
32 | if(t->next==NULL || t->next->data > a)
| ^~~~
/tmp/compiler_00iwb1w7/src:32:40: 错误:对成员‘next’的请求出现在‘* t’中,而后者具有非类类型‘int’
32 | if(t->next==NULL || t->next->data > a)
| ^~~~
/tmp/compiler_00iwb1w7/src:34:27: 错误:cannot convert ‘node*’ to ‘int*’ in assignment
34 | p=(struct node *)malloc(sizeof(struct node));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| |
| node*
/tmp/compiler_00iwb1w7/src:35:28: 错误:对成员‘data’的请求出现在‘* p’中,而后者具有非类类型‘int’
35 | p->data=a;
| ^~~~
/tmp/compiler_00iwb1w7/src:36:28: 错误:对成员‘next’的请求出现在‘* p’中,而后者具有非类类型‘int’
36 | p->next=t->next;
| ^~~~
/tmp/compiler_00iwb1w7/src:36:36: 错误:对成员‘next’的请求出现在‘* t’中,而后者具有非类类型‘int’
36 | p->next=t->next;
| ^~~~
/tmp/compiler_00iwb1w7/src:37:28: 错误:对成员‘next’的请求出现在‘* t’中,而后者具有非类类型‘int’
37 | t->next=p;
| ^~~~
/tmp/compiler_00iwb1w7/src:39:22: 错误:对成员‘next’的请求出现在‘* t’中,而后者具有非类类型‘int’
39 | t=t->next;
| ^~~~
/tmp/compiler_00iwb1w7/src:44:33: 错误:对成员‘data’的请求出现在‘* t’中,而后者具有非类类型‘int’
44 | printf("%d ",t->data);
| ^~~~
/tmp/compiler_00iwb1w7/src:45:22: 错误:对成员‘next’的请求出现在‘* t’中,而后者具有非类类型‘int’
45 | t=t->next;
| ^~~~
谢谢大家!