作者在 2011-02-18 21:24:13 发布以下内容
#define MAXNUM 100
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define FALSE 0
#define TRUE 1
#define SPECIAL 1234567
typedef int DataType;
struct SeqList
{DataType element[MAXNUM];
int n;
};
typedef struct SeqList *PSeqList;
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define FALSE 0
#define TRUE 1
#define SPECIAL 1234567
typedef int DataType;
struct SeqList
{DataType element[MAXNUM];
int n;
};
typedef struct SeqList *PSeqList;
DataType retrieve_seq(PSeqList palist,int p)
{if(p>=0 && p<palist->n)
printf("%d",palist->element[p]);
printf("Not exist.\n");
return(SPECIAL);}
void print(PSeqList palist)
{int i;
printf("此时表中的元素为:");
for(i=0;i<palist->n;i++)
printf("%d",palist->element[i]);
}
PSeqList createNullList_seq(void)
{PSeqList palist;
palist=(PSeqList)malloc(sizeof(struct SeqList));
if(palist!=NULL)
palist->n=0;
else printf("out of space!!\n");
return(palist);
}
int insert_seq(PSeqList palist,int p,DataType x)
{int q;
if(palist->n==MAXNUM)
{printf("Overflow!\n");
return(FALSE);
}
if(p<0||p>palist->n)
{printf("Not exist!\n");
return(FALSE);
}
for(q=palist->n-1;q>=p;q--)
palist->element[q+1]=palist->element[q];
palist->element[p]=x;
palist->n=palist->n+1;
return(TRUE);
}
{int q;
if(palist->n==MAXNUM)
{printf("Overflow!\n");
return(FALSE);
}
if(p<0||p>palist->n)
{printf("Not exist!\n");
return(FALSE);
}
for(q=palist->n-1;q>=p;q--)
palist->element[q+1]=palist->element[q];
palist->element[p]=x;
palist->n=palist->n+1;
return(TRUE);
}
int delete_seq(PSeqList palist,int p)
{int q;
if(p<0||p>palist->n-1)
{printf("NOT exist!\n");
return(FALSE);
}
for(q=p;q<palist->n-1;q++)
palist->element[q]=palist->element[q+1];
palist->n=palist->n-1;
return(TRUE);
}
int locate_seq(PSeqList palist,DataType x)
{int q;
for(q=0;q<palist->n;q++)
if(palist->element[q]==x)
return(q+1);
return(-1);
}
{int q;
for(q=0;q<palist->n;q++)
if(palist->element[q]==x)
return(q+1);
return(-1);
}
void main()
{PSeqList j;
int i,k;
int z, n,x,m;
j=createNullList_seq();
if(j==NULL) exit(1);
printf("请为顺序表输入n个数:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
j->element[i]=i;j->n=n;
print(j);
printf("please input the insert number x:\n");scanf("%d",&x);
printf("please input the insert location i\n");scanf("%d",&i);
k=insert_seq(j,i,x);
if(k==FALSE) exit(1);print(j);
printf("please input the deleted number's location:\n");
scanf("%d",&m);
n=delete_seq(j,m);
if(n==FALSE)exit(1);
print(j);
printf("求x在j所指顺序表中的位置n。输入x:");
scanf("%d",&x);
n=locate_seq(j,x);
printf("n为%d",n);
free(j);
}