一、關鍵點
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 為空I唷!\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;
}