測試框架
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"SeqList.h"
using namespace std;
struct Teacher
{
int age;
char name[64];
};
void main()
{
int ret = 0;
int i = 0;
Seqlist* list = NULL;
Teacher t1, t2, t3, t4, t5, t6;
t1.age = 21;
strcpy(t1.name,"li1");
t2.age = 22;
strcpy(t2.name, "li2");
t3.age = 23;
strcpy(t3.name, "li3");
t4.age = 24;
strcpy(t4.name, "li4");
t5.age = 25;
strcpy(t5.name, "li5");
list = List_Create(10);
if (list==NULL)
{
return;
}
List_Inster(list,&t1,0);//頭插法
List_Inster(list, &t2, 0);
List_Inster(list, &t3, 0);
List_Inster(list, &t4, 0);
List_Inster(list, &t5, 0);
for ( i = 0; i < List_Length(list); i++)
{
Teacher* tmp =(Teacher*)List_Get(list,i);
if (tmp==NULL)
{
return;
}
printf("tmp->age : %d\n", tmp->age);
printf("tmp->name : %s\n", tmp->name);
}
while (List_Length(list)>0)
{
List_Delete(list,0);
}
system("pause");
}
編寫的頭文件
#pragma once
#ifdef _MYSEQLIST_H_
#define _MYSEQLIST_H_
#endif // _MYSEQLIST_H_
//線性鏈表的順序存儲
//void指針表示未知類型的指針,可以將任意類型的指針直接賦值給void指針,好比是C#、Java中的object引用,可以把任意 類型的對象賦值給它。但把void類型賦值給特定類型的指針時,就需要進行強制轉換,因爲C++爲類型語言,儘可能保證類型安全,如果使用了強制類型轉 換,編譯器就認爲程序員知道他(她)在幹什麼,程序也應該負起這樣做的責任。
typedef void Seqlist;
typedef void SeqListNode;
Seqlist * List_Create(int capacity);
void List_Destory(Seqlist* list);
void List_Clear(Seqlist* list);
int List_Length(Seqlist* list);
int List_Capacity(Seqlist* list);
SeqListNode* List_Get(Seqlist* list, int pos);
int List_Inster(Seqlist* list, SeqListNode* node, int pos);
int List_Delete(Seqlist* list,int pos);
對頭文件的實現
#include"SeqList.h"
#include<stdio.h>
#include<stdlib.h>
using namespace std;
struct TSeqList
{
int length;
int capacity;
unsigned int **node;
};
/*創建鏈表併爲鏈表分配空間*/
Seqlist* List_Create(int capacity)
{
TSeqList* tmp = NULL;
tmp = (TSeqList*)malloc(sizeof(TSeqList));
if (tmp==NULL)
{
return NULL;
}
tmp->node =(unsigned int**)malloc(sizeof(unsigned int*)*capacity);
tmp->length = 0;
tmp->capacity = capacity;
return tmp;
}
/*銷燬鏈表並釋放鏈表的內存空間*/
void List_Destory(Seqlist* list)
{
TSeqList* tlist = NULL;
if (list==NULL)
{
printf("List_Destory() err:");
return;
}
tlist = (TSeqList*)list;
if (tlist->node!=NULL)
{
free(tlist->node);
}
if (tlist->length!=0)
{
free(tlist);
}
return ;
}
/*清空鏈表,回到初始鏈表的狀態*/
void List_Clear(Seqlist* list)
{
TSeqList* tlist = NULL;
if (list==NULL)
{
printf("List_Clear() err:");
return;
}
tlist = (TSeqList*)list;
tlist->length = 0;
return ;
}
/*鏈表長度大小*/
int List_Length(Seqlist* list)
{
TSeqList* tlist = NULL;
if (list==NULL)
{
printf("List_Length() err:");
return -1;
}
tlist = (TSeqList*)list;
return tlist->length;
}
/*鏈表大小獲取*/
int List_Capacity(Seqlist* list)
{
TSeqList* tlist = NULL;
if (list==NULL)
{
printf("List_Capacity() err:");
return -1;
}
tlist = (TSeqList*)list;
return tlist->capacity;
}
/*元素獲取*/
SeqListNode* List_Get(Seqlist* list, int pos)
{
TSeqList* tlist = NULL;
if (list == NULL||pos<0)
{
printf("List_Get() err:");
return NULL;
}
tlist = (TSeqList*)list;
return tlist->node[pos];
}
/*元素插入*/
int List_Inster(Seqlist* list, SeqListNode* node, int pos)
{
TSeqList* tlist = NULL;
if (list == NULL || pos<0||node==NULL)
{
printf("List_Inster() err:");
return -1;
}
int i = 0;
SeqListNode* ret = NULL;
tlist = (TSeqList*)list;
//判斷是不是鏈表中已經滿了
if (tlist->length==tlist->capacity)
{
printf("List_Inster() err: yiman");
return -2;
}
//容錯修正,比如 6歌長度 容量爲20,用戶在10位置插入,則進行容錯
if (pos>=tlist->length)
{
pos = tlist->length;
}
//數據元素後移 頭插法
for ( i = tlist->length; i > pos; i--)
{
tlist->node[i] = tlist->node[i-1];
}
//插入元素
tlist->node[i] = (unsigned int *)node;
tlist->length++;
return 0;
}
/*元素刪除*/
int List_Delete(Seqlist* list, int pos)
{
TSeqList* tlist = NULL;
if (list==NULL||pos<0)
{
printf("List_Delete() err: ");
return -1;
}
tlist = (TSeqList*)list;
SeqListNode* ret = NULL;
ret = tlist->node[pos];
int i = 0;
for ( i =pos+1; i <tlist->length; i++)
{
tlist->node[i - 1] = tlist->node[i];
}
tlist->length--;
return NULL;
}