帮忙改正一个小程序

来源:百度知道 编辑:UC知道 时间:2024/06/15 17:19:28
#include<iostream>
using namespace std;
#include<malloc.h>
typedef struct node
{
int data;
struct node *next;
}sqlist;
void function(sqlist *q,int array[],int n)
{
int i;
sqlist *r;
for(i=0;i<n;i++)
{
r=(sqlist *)malloc(sizeof(sqlist));
r->next=NULL;
r->data=array[i];
q->next=r;
q=r;
}
}
int main()
{
sqlist *s;
int array[10];
int i,temp,k=1;
s=(sqlist *)malloc(sizeof(sqlist));
s->next=NULL;
cout<<"fullfill the array!"<<endl;
for(i=0;i<10;i++)
{
cin>>array[i];
}
function(s,array,10);
while(s->next!=NULL)
{
if(s->next->data>s->data)
{
temp=k;
}
k++;
}
cout<<k<<endl;
return 0;
}

次程序的目的是通过一次遍历

#include<iostream>
using namespace std;
#include<malloc.h>
typedef struct node
{
int data;
struct node *next;
}sqlist;
void function(sqlist *q,int array[],int n)
{
int i;
sqlist *r;
for(i=0;i<n;i++)
{
r=(sqlist *)malloc(sizeof(sqlist));
r->next=NULL;
r->data=array[i];
q->next=r;
q=r;
}
}
int main()
{
sqlist *s;
int array[10];
int i,temp,k=1;
s=(sqlist *)malloc(sizeof(sqlist));
s->next=NULL;
cout<<"fullfill the array!"<<endl;
for(i=0;i<10;i++)
{
cin>>array[i];
}
function(s,array,10);
while(s->next!=NULL)
{
if(s->next->data>s->data)
{
temp=k;
}
k++;
s=s->next; //这里换成下一个指针
}
cout<<temp<<endl; //输出最大序号
return 0;
}