6.3w+
社区成员
#define list_size 5
#define increment 2
#define ok 1
#define overflow -1
#define error -2
#include <iostream>
#include <malloc.h>
#include <stdlib.h>
using namespace std;
typedef struct
{
int *elem;
int length;
int listsize;
}sqlist;
int initlist(sqlist &L)
{
L.elem=(int *)malloc(list_size*sizeof(int));
if(!L.elem) exit(overflow);
L.length=0;
L.listsize=list_size;
return ok;
}
int listinsert(sqlist &L,int i,int e)
{
int *p,*q,*newbase;
if(i <1 || i>L.length+1) return error;
if(L.length>=L.listsize)
{
newbase=(int *)realloc(L.elem,(L.listsize+increment)*sizeof(int));
if(!newbase)exit(overflow);
L.listsize+=increment;
}
q=&(L.elem[i-1]);
for(p=&(L.elem[L.length-1]); p>=q; --p)
*(p+1)=*p;
*q=e;
++L.length;
return ok;
}
int listdelete(sqlist &L,int i,int &e)
{
int *p,*q;
if((i <1) || (i>L.length))return error;
p=&(L.elem[i-1]);
e=*p;
q=L.elem+L.length-1;
for(++p;p <=q;++p)
*(p-1)=*p;
--L.length;
return ok;
}
void printlist(sqlist &L)
{
int i;
cout <<"***************************\n";
cout <<"The element of the sqlist are:" <<endl;
for(i=0;i <L.length;i++)
cout<<L.elem[i]<<" "; //链表里面的输出元素;
cout <<"\n***************************\n";
return;
}
void main()
{
int i,j,e,m,n; //n的类型是什么,
sqlist L1;
initlist(L1);
cout <<"Please output the length of the sqlist:\n";
cin>>L1.length;
cout <<"Please insert the element of the sqlist:\n";
for(i=0;i <L1.length;i++)
cin>>L1.elem[i];
cout <<"Please insert the value of j and e :\n";
cin>>j;
cin>>e;
listinsert(L1,j,e); //j表示的是下标,位置吧,
cout <<"please delete the value of m and n:\n";
cin>>m;
cin>>n; //m一样表示的是下标,位置吧,
listdelete(L1, m, n);
//printlist(&L1);
printlist(L1);
}
void main()
{
int i,j,e,m,*n;
sqlist L1;
//initlist(&L1);
initlist(L1); //函数传参有问题,引用,
cout <<"Please output the length of the sqlist:\n";
cin>>L1.length;
cout <<"Please insert the element of the sqlist:\n";
for(i=0;i <L1.length;i++)
cin>>L1.elem[i];
cout <<"Please insert the value of j and e :\n";
//istinsert(&L1,j,e);
listinsert(L1,j,e);
cin>>j;
cin>>e;
cout <<"please delete the value of m and n:\n";
//istdelete(&L1,m,&n);
listdelete(L1,m,n);
cin>>m;
cin>>n;
//printlist(&L1);
printlist(L1);
}