- 论坛徽章:
- 0
|
#include <stdio.h>
#include <stdlib.h>
#include <error.h> /*自定义的报错信息*/
#define ListSize 100 /*表空间*/
typedef int DataType;
typedef struct{
DataType data[ListSize];
int length;
}Seqlist;
//初始化表
void InitList(Seqlist *L){
L->length = 0;
}
//插入运算
void InsertList(Seqlist *L, DataType x, int i){
int j;
if(i < 1 || i > L->length+1)
error("position error.");
if(L->length >= ListSize)
error("overflow");
for ( j = L->length-1; j >= i-1; j--)
L->data[j+1] = L->data[j];
L->data[i-1] = x;
L->length++;
}
void DisplayList(Seqlist *L){
int i;
for (i = 0; i <= L->length-1; i++)
printf("第%d个元素是:%d\n", i+1, L->data);
}
int main(void) {
Seqlist mylist; //如果此处用Seqlist *mylist; 下面的InitList(mylist)就有问题,应该是本人对指针的理解还太浅了。请帮忙解释一下。
InitList(&mylist);
int i;
for (i = 1; i <=100; i++){
InsertList(&mylist, 100, i);
}
DisplayList(&mylist);
InsertList(&mylist, 101, 2); //测试溢出
}
本人的编译器是 eclipse cdt + mingw |
|