此文主要是为了记录一些收获,总结过程中遇到的问题
不说废话,先上源码
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#define MaxSize 10000
#define ERROR 0
#define OK 1
typedef int Status;
typedef int ElemType;
typedef struct {
ElemType data[MaxSize];
int last;
}SqList;
//构造空的顺序表
void InitList(SqList &List)
{
List.last = -1; //为-1表示顺序表为空
}
//判断顺序表为空
bool SqListEmpty(SqList *List)
{
if (List->last == -1)
return true;
return false;
}
//判断顺序表为满
bool SqListFull(SqList* List)
{
if (List->last == MaxSize - 1)
return true;
return false;
}
//查找表内元素并输出元素和位置
Status SearchElem(SqList *List,int e)
{
if (SqListEmpty(List)) {
printf("顺序表为空\n");
return ERROR;
}
int i = 0;
while (i <= List->last)
{
if (List->data[i] == e)
{
printf("查找的元素为:%d 位置为:%d\n", e, i);
return OK;
}
i++;
}
return ERROR;
}
//插入元素
Status Push(SqList *List,int e)
{
if (SqListFull(List))
return ERROR;
List->data[++List->last] = e;
return OK;
}
//删除指定元素
Status Delete(SqList* List, int e)
{
if (SqListEmpty(List))
return ERROR;
int i = 0;
while (i <= List->last)
{
if (List->data[i]==e)
{
while (i <List->last)
{
List->data[i] = List->data[i + 1];
i++;
}
List->last-=1;
}
i++;
}
return OK;
}
//输出表内元素
Status Printf(SqList *List)
{
if (SqListEmpty(List))
return ERROR;
for (int i = 0; i <= List->last; i++)
printf("%d ", List->data[i]);
printf("\n");
return OK;
}
//求表长
void GetLength(SqList* List)
{
printf("表长为%d last指向%d\n",(List->last+1),List->last);
}
int main()
{
SqList List;
InitList(List);
SearchElem(&List,1);
Push(&List,1);
Push(&List, 2);
SearchElem(&List, 2);
Push(&List, 3);
Push(&List, 4);
Push(&List,5);
Delete(&List, 3);
Printf(&List);
GetLength(&List);
return 0;
}
收获——
1:不小心给数组开辟空间,遇到了取消VS中“取消对NULL指针的‘、、、’的引用”的警告:发现创建链表中malloc空间的时候,要先个判断,例如"head==NULL",则分配内存失败,再加上exit或者return的条件。
2.遇到神奇的警告:函数使用堆叠的“40004”字节。请考虑讲一些数据移动到堆,目前还不知道啥意思,但是并没有影响结果。希望有大佬能解答一下这有啥影响。
3.创建函数的功能还是不太会,对传递的参数和指针还是有些不熟。