順序表——基於C語言

2020-08-12 10:08:27

最近複習了C語言的相關知識,實現了數據結構中的順序表。

下面 下麪是Main.c檔案:

#include "SeqList.h"
#include <stdio.h>

int main()
{
	SeqList* list = SeqListCreate();
	 
	if(list != NULL)
	{
	 	printf("順序表容量爲:%d\n", SeqListCapacity(list));
	  	printf("順序表長度爲:%d\n", SeqListLength(list));
	  
	  	printf("插入狀態:%d\n", SeqListInsert(list, 0, 0));
	  	printf("插入狀態:%d\n", SeqListInsert(list, 1, 1));
	  	printf("插入狀態:%d\n", SeqListInsert(list, 2, 2));
	  	printf("插入狀態:%d\n", SeqListInsert(list, 3, 3));
	  	printf("插入狀態:%d\n", SeqListInsert(list, 9, 4));
	  
	  	printf("此時順序表長度爲:%d\n", SeqListLength(list));
	  
	  	SeqListDelete(list, 0, NULL); 
	  	SeqListDelete(list, 0, NULL);
	  
	  	printf("此時順序表長度爲:%d\n", SeqListLength(list));
	  
	  	printf("刪除狀態:%d\n", SeqListDelete(list, 1, NULL)); 
	  	printf("此時順序表長度爲:%d\n", SeqListLength(list));
	  
	  	list = SeqListDestroy(list); 
	}
	
	return 0;
}

下面 下麪是SeqList.h檔案:

#ifndef __SEQLIST_H__
#define __SEQLIST_H__

typedef int DataType;

#define deBug() printf("File = %s\nLine = %n", __FILE__, __LINE__)

#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE 100

#ifndef __cplusplus
 	typedef int Bool;
 	#define true 1
 	#define false 0
#else
 	typedef bool Bool;
#endif

#define MALLOC(type, size)  (type*)malloc(sizeof(type) * size)
#define FREE(p)    (free(p), p = NULL)

typedef struct
{
 	DataType space[MAXSIZE];
 	int size;
} SeqList;

SeqList* SeqListCreate(void);
SeqList* SeqListDestroy(SeqList *list);
int SeqListLength(SeqList *list);
int SeqListCapacity(SeqList *list);
Bool SeqListInsert(SeqList *list, int i, DataType data);
Bool SeqListDelete(SeqList *list, int i, DataType *data);
Bool SeqListSet(SeqList *list, int i, DataType data);
Bool SeqListGet(SeqList *list, int i, DataType *data);

#endif

下面 下麪是SeqList.c檔案:

#include "SeqList.h"

SeqList* SeqListCreate(void)
{
 	SeqList *ret = MALLOC(SeqList, 1);
 
 	if(ret != NULL) ret->size = 0;
 
 	return ret;
}

SeqList* SeqListDestroy(SeqList *list)
{ 
 	return FREE(list);
}

int SeqListLength(SeqList *list)
{
 	return (list != NULL) ? list->size : -1;
}

int SeqListCapacity(SeqList *list)
{
 	return (list != NULL) ? MAXSIZE : -1;
}

Bool SeqListInsert(SeqList *list, int i, DataType data)
{
 	Bool ret = (list != NULL) && (i >= 0) && (i <= list->size) && (list->size < MAXSIZE);
 
 	if(ret)
 	{
  		int j;
  
  		for(j = list->size - 1; j >= i; j--)
  		{
   			list->space[j + 1] = list->space[j];
  		}
  
  		list->space[j] = data;
  
  		list->size++;
 	}
 
 	return ret;
}

Bool SeqListDelete(SeqList *list, int i, DataType *data)
{
 	Bool ret = (list != NULL) && (i >= 0) && (i < list->size);
 
 	if(ret)
 	{ 
  		if(data != NULL) *data = list->space[i];
  
  		for(; i < list->size - 1; i++)
  		{
   			list->space[i] = list->space[i + 1]; 
  		}
  
  		list->size--;
 	}
 
 	return ret;
}

Bool SeqListSet(SeqList *list, int i, DataType data)
{
 	return ((list != NULL) && (i >= 0) && (i < list->size)) ? (list->space[i] = data, true) : false;
}

Bool SeqListGet(SeqList *list, int i, DataType *data)
{ 
 	return ((list != NULL) && (i >= 0) && (i < list->size) && (data != 0)) ? (*data = list->space[i], true) : false;
}

不知道什麼原因,使用Dev c++編譯這個程式會在free掉順序表時出錯。改使用Qt重新編譯,無錯誤無警告。下面 下麪是執行結果:

在这里插入图片描述