|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
为什么顺序表里面的数字不是0~9而是0~8?
- #include<stdio.h>
- #include<stdlib.h>
- typedef int ElemType;
- typedef struct seqList
- {
- int n;
- int maxLength;
- ElemType *element;
- } SeqList;
- #define ERROR 0
- #define OK 1
- #define Overflow 2 //Overflow 表示上溢
- #define Underflow 3 //Underflow 表示下溢
- #define NotPresent 4 //Notpresent 表示元素不存在
- #define Duplicate 5 //Duplicate 表示有重复元素
- typedef int Status;
- Status Init(SeqList *L,int mSize)
- {
- L->maxLength = mSize;
- L->n = 0;
- L->element = (ElemType*)malloc(sizeof(ElemType)*mSize); //动态生成一维数组空间
- if(!L->element)
- return ERROR;
- return OK;
- }
- Status Find(SeqList L,int i,ElemType *x)
- {
- if(i<0||i>L.n-1)
- return ERROR; //判断元素下标i是否越界
- *x = L.element[i]; //取出element[i]的值通过参数x返回
- return OK;
- }
- Status Insert(SeqList *L,int i,ElemType x)
- {
- int j;
- if(i<-1||i>L->n-1) //判断i下标是否越界
- return ERROR;
- if(L->n==L->maxLength) // 判断顺序表存储空间是否已满
- return ERROR;
- for(j=L->n-1; j>i; j--)
- L->element[j+1]=L->element[j]; //从后往前逐个后移元素
- L->element[i+1]=x; //将新元素放入下标为i+1的位置
- L->n=L->n+1;
- return OK;
- }
- Status Delete(SeqList *L,int i)
- {
- int j;
- if(i<0||i>L->n-1) //小标i是否越界
- return ERROR;
- if(!L->n) //顺序表是否为空
- return ERROR;
- for(j=i+1; j<L->n; j++)
- L->element[j-1]=L->element[j]; //从前往后逐个前移元素
- L->n--; //表长减1
- return OK;
- }
- Status Output(SeqList *L)
- {
- int i;
- if(L->n==0)
- return ERROR;
- for(i=0; i<L->n-1; i++)
- printf("%d ",L->element[i]);
- printf("\n");
- return OK;
- }
- void Destroy(SeqList *L)
- {
- L->n=0;
- L->maxLength=0;
- free(L->element);
- }
- int main()
- {
- int i;
- SeqList list;
- Init(&list,10); //初始化线性表
- for(i=0; i<10; i++)
- Insert(&list,i-1,i); //线性表中插入新元素
- Output(&list);
- Delete(&list,0);
- Output(&list);
- Destroy(&list);
- return 0;
- }
复制代码
不出所料这应该是自己写出来的代码。
首先猜猜你的想法:n, maxlength在使用时是重点。
- typedef struct seqList
- {
- int n; // 已存元素数量
- int maxLength; // 能存元素的最大数量
- ElemType *element;
- } SeqList;
复制代码
然后是:
Insert();函数,你这只是单单的实现的顺序存放,并没有真正的插入功能吧。头插, 中插,尾插。
随后我关键改动几处,自已对比的函数之间的差别。
- #include<stdio.h>
- #include<stdlib.h>
- typedef int ElemType;
- typedef struct seqList
- {
- int n; // 已存元素数量
- int maxLength; // 能存元素的最大数量
- ElemType *element;
- } SeqList;
- #define ERROR 0
- #define OK 1
- #define Overflow 2 //Overflow 表示上溢
- #define Underflow 3 //Underflow 表示下溢
- #define NotPresent 4 //Notpresent 表示元素不存在
- #define Duplicate 5 //Duplicate 表示有重复元素
- typedef int Status;
- Status Init(SeqList *L,int mSize)
- {
- L->maxLength = mSize;
- L->n = 0;
- L->element = (ElemType*)malloc(sizeof(ElemType)*mSize); //动态生成一维数组空间
- if(!L->element)
- return ERROR;
- return OK;
- }
- Status Insert(SeqList *L,int i, ElemType x)
- {
- if(L->n==L->maxLength) // 判断顺序表存储空间是否已满
- return ERROR;
- L->element[i]=x;
- L->n=L->n+1;
- return OK;
- }
- Status Output(SeqList *L)
- {
- int i;
- if(L->n==0)
- return ERROR;
- for(i=0; i<L->n; i++)
- printf("%d ",L->element[i]);
- printf("\n");
- return OK;
- }
- int main()
- {
- int i;
- SeqList list;
- Init(&list,10); //初始化线性表
- for(i=0; i<10; i++)
- Insert(&list,i,i); //线性表中插入新元素
- printf("%d\n", list.n);
- Output(&list);
- return 0;
- }
-
复制代码
|
|