実験の一線形表の順序の表現と実現
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#define OVERFLOW -2
#define OK 1
#define ERROR 0
#define LIST_INIT_SIZE 1000
#define Elemtype int// typedef int Elemtype
typedef struct
{
Elemtype *elem;
int length;
int listsize;
}sqlist;
int InitList_Sq( sqlist & L,int n )
{ // n=0
L.elem = (Elemtype*)malloc(LIST_INIT_SIZE*sizeof (Elemtype));
if (!L.elem) exit(OVERFLOW);
L.length =n;
L.listsize = LIST_INIT_SIZE;
return OK;
}// , 、 、 :
int ListInsert_Sq(sqlist &L, int i, Elemtype e)
{
int LISTINCREMENT=i,*q,*p; // L i e
if (i < 1 || i > L.length+1) return ERROR;
if (L.length >= L.listsize)
{
int *newbase = (Elemtype *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof (Elemtype));
if (!newbase) exit(OVERFLOW);
L.elem = newbase;
L.listsize += LISTINCREMENT;
}
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_Sq (sqlist &L, int i, Elemtype &e)
{ //
Elemtype *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;
}
int locate_sq(sqlist L ,Elemtype x) //
{
int i;
for(i=0;i<L.length;i++)
if(L.elem[i]==x)
return i+1;
return 0;
}
int main()
{
sqlist l;
int n,num,i,x,e;
scanf("%d",&n);
InitList_Sq(l,n);
for (i=1;i<=n;i++)
{
scanf("%d",&num);
ListInsert_Sq(l,i+1,num);
}
for (i=1;i<=n;i++)
printf("%d ",*(l.elem+i));
printf("
");
scanf("%d",&x);
ListDelete_Sq (l, x,e);
for (i=1;i<n;i++)
printf("%d ",*(l.elem+i));
printf("
");
scanf("%d",&x);
printf("%d
",locate_sq(l,x)-1);
return 0;
}