一、关键点
1.指针类型不要直接赋值;
int *a;
//a为指针类型;在未赋地址的情况下不能直接赋值
//错误方式
int b = 1;
*a = b;
//正确方式
int b = 1;
a=&b;
2. free后赋NULL值
free函数只是把指针指向的内存空间释放了,即内存中存储的值,但是并没有将指针的值赋为NULL,指针仍然指向这块内存。而程序判断一个指针是否合法,通常都是使用if语句测试该指针是否为NULL来判断,导致指针成为所谓的“野指针”,诱导误操作
void DestroyList_Sq(SqList *L){
free(L->elem);
L->elem=NULL;
L->length=0;
L->listsize=0;
}
3. realloc 最好声明一个新的指针
if(L->length>=L->listsize){
LElemType_Sq *newbase;
newbase = (LElemType_Sq*)realloc(L->elem,((L->listsize)+LIST_INCREMENT)*sizeof(LElemType_Sq));
if(!newbase){
exit(OVERFLOW);
}
L->elem = newbase;
L->listsize+=LIST_INCREMENT;
}
二、代码
#include <stdio.h>
#include <stdlib.h>
#define LIST_INIT_SIZE 100
#define LIST_INCREMENT 10
#define OVERFLOW -2
#define TRUE 1
#define FALSE 0
#define ERROR 0
#define OK 1
typedef int Status;
typedef int LElemType_Sq;
typedef struct{
LElemType_Sq *elem;
int length;
int listsize;
}SqList;
Status Compare(LElemType_Sq e1,LElemType_Sq e2);
void Visit(LElemType_Sq e);
Status CmpGreater(LElemType_Sq e, LElemType_Sq data);
void PrintElem(LElemType_Sq e);
Status InitList_Sq(SqList *L);
void ClearList_Sq(SqList *L);
void DestoryList_Sq(SqList *L);
Status ListEmpty_Sq(SqList L);
int ListLength_Sq(SqList L);
Status GetElem_Sq(SqList L,int i, LElemType_Sq *e);
int LocalteElem_Sq(SqList L,LElemType_Sq e,Status(Compare)(LElemType_Sq,LElemType_Sq));
Status PriorElem_Sq(SqList L,LElemType_Sq cur_e,LElemType_Sq *pre_e);
Status NextElem_Sq(SqList L,LElemType_Sq cur_e,LElemType_Sq *next_e);
Status ListInsert_Sq(SqList *L,int i,LElemType_Sq e);
Status ListDelete_Sq(SqList *L,int i,LElemType_Sq *e);
Status ListTraverse_Sq(SqList L, void(Visit)(LElemType_Sq));
Status InitList_Sq(SqList *L){
L->elem = (LElemType_Sq*)malloc(sizeof(LElemType_Sq)*LIST_INIT_SIZE);
if(!L->elem){
exit(OVERFLOW);
}
L->length = 0;
L->listsize = LIST_INIT_SIZE;
return TRUE;
}
void ClearList_Sq(SqList *L){
L->length=0;
}
void DestroyList_Sq(SqList *L){
free(L->elem);
L->elem=NULL;
L->length=0;
L->listsize=0;
}
Status ListEmpty_Sq(SqList L){
return L.length==0?TRUE:FALSE;
}
int ListLength_Sq(SqList L){
return L.length;
}
Status GetElem_Sq(SqList L,int i, LElemType_Sq *e){
if(i<1||i>L.length){
return ERROR;
}else{
*e = L.elem[i-1];
return OK;
}
}
int LocateElem_Sq(SqList L,LElemType_Sq e,Status(Compare)(LElemType_Sq,LElemType_Sq)){
for(int i =1;i<=L.length;i++){
if(Compare(e,L.elem[i-1])){
return i;
}
}
return 0;
}
Status PriorElem_Sq(SqList L,LElemType_Sq cur_e,LElemType_Sq *pre_e){
for(int i=2;i<=L.length;i++){
if(L.elem[i-1]==cur_e){
*pre_e = L.elem[i-2];
return OK;
}
}
return ERROR;
}
Status NextElem_Sq(SqList L,LElemType_Sq cur_e,LElemType_Sq *next_e){
for(int i=1;i<L.length;i++){
if(L.elem[i-1]==cur_e){
*next_e = L.elem[i];
return OK;
}
}
return ERROR;
}
Status ListInsert_Sq(SqList *L,int i,LElemType_Sq e){
if(i>L->length+1||i<1){
return ERROR;
}
if(L->length>=L->listsize){
LElemType_Sq *newbase;
newbase = (LElemType_Sq*)realloc(L->elem,((L->listsize)+LIST_INCREMENT)*sizeof(LElemType_Sq));
if(!newbase){
exit(OVERFLOW);
}
L->elem = newbase;
L->listsize+=LIST_INCREMENT;
}
LElemType_Sq *q = &(L->elem[i-1]);
LElemType_Sq *temp = &(L->elem[L->length]);
while(temp>q){
*temp = *(temp-1);
temp--;
}
*q = e;
(L->length)++;
return OK;
}
Status ListDelete_Sq(SqList *L,int i,LElemType_Sq *e){
if(i>L->length||i<1){
return ERROR;
}
LElemType_Sq *p,*tail;
if(L->length==1){
*e = L->elem[0];
L->length=0;
return OK;
}
p=&(L->elem)[i-1];
*e = *p;
tail = &L->elem[L->length-1];
while(p<tail){
*p=*(p+1);
p++;
}
L->length--;
return OK;
}
Status ListTraverse_Sq(SqList L, void(Visit)(LElemType_Sq)){
int i;
for(i=0;i<L.length;i++){
Visit(L.elem[i]);
}
}
Status CmpGreater(LElemType_Sq e, LElemType_Sq data)
{
return data>e ? TRUE : FALSE;
}
void PrintElem(LElemType_Sq e)
{
printf("%d ", e);
}
Status Compare(LElemType_Sq e1,LElemType_Sq e2){
return e1==e2?TRUE:FALSE;
}
void Visit(LElemType_Sq e){
printf("d%",e);
}
int main()
{
SqList L;
int i;
LElemType_Sq e;
printf("▼1\n▲函数 InitList_Sq 测试...\n"); //1.函数InitList_Sq测试
{
printf("初始化顺序表 L ...\n");
InitList_Sq(&L);
printf("\n");
}
printf("▼4\n▲函数 ListEmpty_Sq 测试...\n"); //4.函数ListEmpty_Sq测试
{
ListEmpty_Sq(L) ? printf(" L 为空!!\n") : printf(" L 不为空!\n");
printf("\n");
}
printf("▼10\n▲函数 ListInsert_Sq 测试...\n"); //10.函数ListInsert_Sq测试
{
for(i=1; i<=6; i++)
{
printf("作为示范,在 L 第 %d 个位置插入 \"%d\"...\n", i, 2*i);
ListInsert_Sq(&L, i, 2*i);
}
printf("\n");
}
printf("▼12\n▲函数 ListTraverse_Sq 测试...\n"); //12.函数ListTraverse_Sq测试
{
printf(" L 中的元素为:L = ");
ListTraverse_Sq(L, PrintElem);
printf("\n\n");
}
printf("▼5\n▲函数 ListLength_Sq 测试...\n"); //5.函数ListLength_Sq测试
{
i = ListLength_Sq(L);
printf(" L 的长度为 %d \n", i);
printf("\n");
}
printf("▼11\n▲函数 ListDelete_Sq 测试...\n"); //11.函数ListDelete_Sq测试
{
ListDelete_Sq(&L, 6, &e);
printf("删除 L 中第 6 个元素 \"%d\" ...\n", e);
printf(" L 中的元素为:L = ");
ListTraverse_Sq(L, PrintElem);
printf("\n\n");
}
printf("▼6\n▲函数 GetElem_Sq 测试...\n"); //6.函数GetElem_Sq测试
{
GetElem_Sq(L, 4, &e);
printf(" L 中第 4 个位置的元素为 \"%d\" \n", e);
printf("\n");
}
printf("▼7\n▲函数 LocateElem_Sq 测试...\n"); //7.函数LocateElem_Sq测试
{
i = LocateElem_Sq(L, 7, CmpGreater);
printf(" L 中第一个元素值大于 \"7\" 的元素的位置为 %d \n", i);
printf("\n");
}
printf("▼8\n▲函数 PriorElem_Sq 测试...\n"); //8.函数PriorElem_Sq测试
{
PriorElem_Sq(L, 6, &e);
printf("元素 \"6\" 的前驱为 \"%d\" \n", e);
printf("\n");
}
printf("▼9\n▲函数 NextElem_Sq 测试...\n"); //9.函数NextElem_Sq测试
{
NextElem_Sq(L, 6, &e);
printf("元素 \"6\" 的后继为 \"%d\" \n", e);
printf("\n");
}
printf("▼2\n▲函数 ClearList_Sq 测试...\n"); //2.函数ClearList_Sq测试
{
printf("清空 L 前:");
ListEmpty_Sq(L) ? printf(" L 为空!!\n") : printf(" L 不为空!\n");
ClearList_Sq(&L);
printf("清空 L 后:");
ListEmpty_Sq(L) ? printf(" L 为空!!\n") : printf(" L 不为空!\n");
printf("\n");
}
printf("▼3\n▲函数 DestroyList_Sq 测试...\n"); //3.函数DestroyList_Sq测试
{
printf("销毁 L 前:");
L.elem ? printf(" L 存在!\n") : printf(" L 不存在!!\n");
DestroyList_Sq(&L);
printf("销毁 L 后:");
L.elem ? printf(" L 存在!\n") : printf(" L 不存在!!\n");
printf("\n");
}
return 0;
}