順序鏈表API

參考資料,編寫目的是爲了鍛鍊自己對於函數的封裝以及基本數據結構的學習能力,記錄之。

能力不足,希望以後繼續改進!!!


頭文件聲明如下:

#pragma once
#ifndef _SeqList_H_
#define _SeqList_H_


 //準備數據類型的封裝
typedef void SeqList;
typedef void SeqListNode;

//創建並且返回一個空的線性表(返回句柄或者內存空間)
SeqList* LinkSeqList_Create(int SeqList_Capacity);

//銷燬一個線性表SeqList
bool SeqList_Destroy(SeqList* SeqList);

//將一個線性表SeqList中的所有元素清空, 線性表回到創建時的初始狀態
bool SeqList_Clear(SeqList* SeqList);

//返回一個線性表SeqList中的所有元素個數
int SeqList_Length(SeqList* SeqList);

//向一個線性表SeqList的pos位置處插入新元素node
int SeqList_Insert(SeqList* SeqList, SeqListNode* node, int pos);

//獲取一個線性表SeqList的pos位置處的元素
int SeqList_Get(SeqList* SeqList, int pos);

//刪除一個線性表SeqList的pos位置處的元素  返回值爲被刪除的元素,NULL表示刪除失敗
SeqListNode* SeqList_Delete(SeqList* SeqList, int pos);

//獲取線性表的容量
int SeqList_Capacity(SeqList* SeqList);


#endif
函數實現如下:

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

#define uint unsigned int

typedef void SeqList;
typedef void SeqListNode;

//鏈表內部結構
typedef struct _SeqList
{
	int capacity;
	int length;
	uint *node;//動態開闢數組內存
}TSeqList;

//創建並且返回一個空的線性表
SeqList* LinkSeqList_Create_demo(int SeqList_Capacity)
{
	/*SeqList*p_head = new SeqList [SeqList_Capacity];
	if (p_head==NULL)
	{
	return NULL;
	}
	return p_head;*/
	TSeqList *ret = NULL;
	if (SeqList_Capacity < 0)
	{
		return NULL;
	}
	ret = (TSeqList*)new TSeqList;
	if (ret == NULL)
	{
		return NULL;
	}
	memset(ret, 0, sizeof(TSeqList));
	//ret->node = (uint *)new  TSeqList[SeqList_Capacity * (uint)];
	ret->node = (uint *)malloc(sizeof(uint)*SeqList_Capacity);
	if (ret->node == NULL)
	{
		return NULL;
	}
	memset(ret->node, 0, sizeof(uint)*SeqList_Capacity);
	ret->capacity = SeqList_Capacity;
	ret->length = 0;
	return ret;
}

SeqList* LinkSeqList_Create(int SeqList_Capacity)
{
	TSeqList *ret = NULL;
	if (SeqList_Capacity < 0)
	{
		return NULL;
	}
	//ret = (TSeqList*)new TSeqList;
	//2.0:一次分配了兩塊內存
	ret = (TSeqList*)malloc(sizeof(TSeqList)+sizeof(uint)*SeqList_Capacity);
	if (ret == NULL)
	{
		return NULL;
	}
	memset(ret, 0, sizeof(TSeqList)+sizeof(uint)*SeqList_Capacity);		
	//ret向後跳SeqList的大小(指針的步長)
	ret->node = (uint*)(ret + 1);	
	ret->capacity = SeqList_Capacity;
	ret->length = 0;
	return ret;
}

//銷燬一個線性表SeqList
bool SeqList_Destroy(SeqList* Seq_List)
{
	if (Seq_List==NULL)
	{
		return false;
	}
	free(Seq_List);
	Seq_List = NULL;
	return true;
}

//將一個線性表SeqList中的所有元素清空, 線性表回到創建時的初始狀態
bool SeqList_Clear(SeqList* Seq_List)
{
	TSeqList *tList = NULL;
	if (Seq_List == NULL)
	{
		return false;
	}
	tList = (TSeqList *)Seq_List;
	tList->length = 0;
	return true;
}

//統計並返回一個線性表SeqList中的所有元素個數(長度)
int SeqList_Length(SeqList* Seq_List)
{
	TSeqList *q_tmp = NULL;
	if (Seq_List == NULL)
	{
		return -1;
	}
	q_tmp = (TSeqList *)Seq_List;
	return q_tmp->length;
}

//向一個線性表SeqList的pos位置處插入新元素node
int SeqList_Insert(SeqList* SeqList, SeqListNode* node, int pos)
{
	int i;
	TSeqList *q_tmp = NULL;
	q_tmp = (TSeqList *)SeqList;
	if (q_tmp == NULL)
	{
		return -1;
	}
	if (pos >=q_tmp->capacity||pos < 0)
	{
		return -2;
	}
	//優化的容錯
	if (pos >= q_tmp ->length)
	{
		pos = q_tmp->length;
	}
	//順序插入
	for (i = q_tmp->length; i > pos; i--)
	{
		q_tmp->node[i] = q_tmp->node[i-1];
	}
	q_tmp->node[pos] = (uint)node;
	q_tmp->length++;

	return 0;
}

//獲取一個線性表SeqList的pos位置處的元素
int SeqList_Get(SeqList* SeqList, int pos)
{
	TSeqList *q_tmp = NULL;
	q_tmp = (TSeqList *)SeqList;
	if (q_tmp == NULL || pos<0||pos>=q_tmp->length)
	{
		return -1;
	}
	int ret = q_tmp->node[pos];
	return ret;
}

//刪除一個線性表SeqList的pos位置處的元素  返回值爲被刪除的元素,NULL表示刪除失敗
SeqListNode* SeqList_Delete(SeqList* SeqList, int pos)
{
	int i=0;
	TSeqList *q_tmp = NULL;
	q_tmp = (TSeqList *)SeqList;
	if (q_tmp == NULL || pos<0 || pos >= q_tmp->length)
	{
		return NULL;
	}
	//提前緩存要刪除的元素
	SeqListNode* tmp = (SeqListNode*)q_tmp->node[pos];

	for ( i = pos+1 ; i <q_tmp -> length; i++)
	{
		q_tmp->node[i - 1] = q_tmp->node[i];
	}
	q_tmp->length--;
	return tmp;
}

//獲取線性表的容量
int SeqList_Capacity(SeqList* Seq_List)
{
	TSeqList *q_tmp = NULL;
	if (Seq_List == NULL)
	{
		return -1 ;
	}
	q_tmp = (TSeqList *)Seq_List;
	return q_tmp->capacity;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章