递增线性表中插入一个元素到适当的位置 这个运行结果不正确看了很长时间不知道哪里的错误求大神指导
#include<iostream>using namespace std;
#include<math.h>
#include<stdlib.h>
#define maxsize 20
int n;
typedef struct node
{ int *data;
int length;
int listsize;
}sqlist;
void createsqlist(sqlist &L)
{ L.data=(int*)malloc(sizeof(int)*maxsize);
if(!L.data)
exit(1);
}
void initsqlist(sqlist &L)
{int i;
int *p;
p=L.data;
cout<<"enter your list's number:"<<"\t";
cin>>n;
cout<<"enter your list's elements:"<<"\t";
for(i=0;i<n;i++,p++)
cin>>*p;
}
void outputsqlist(sqlist &L)
{int *q;
q=L.data;
cout<<"your sqlist is:"<<endl;
for(int i=0;i<n;i++,q++)
cout<<*q<<"\t";
}
void insertsqlist(sqlist &L)
{int c,j;
cout<<"enter you want insert data:"<<"\t";
cin>>c;
if(c<=L.data[0])
j=0;
else
if(c>=L.data[n-1])
j=n;
else
for(int i=0;c>L.data[i];i++)
j=i;
for(;j<n;n--)
L.data[n]=L.data[n-1];
L.data[j]=c;
cout<<L.data[1];
cout<<endl;
n=n+1;
cout<<"inserted list is :"<<endl;
for(int h=0;h<n;h++)
cout<<L.data[h]<<"\t";
cout<<endl;
}
int main()
{sqlist L1;
createsqlist(L1);
initsqlist(L1);
outputsqlist(L1);
insertsqlist(L1);
return 0;
}