main.c ////////////////////////////////////////////////////////////////////////////////// #include<iostream> #include"string" #include"seqlist.h" using namespace std; //创建节点,遍历输出节点,删除节点 typedef struct Teacher { int age; string name; }Teacher; // //SeqList* SeqList_Create(int capacity); // //void SeqList_Destroy(SeqList* list); // //void SeqList_Clear(SeqList* list); // //int SeqList_Length(SeqList* list); // //int SeqList_Capacity(SeqList* list); // //int SeqList_Insert(SeqList* list, SeqListNode* node, int pos); // //SeqListNode* SeqList_Get(SeqList* list, int pos); // //SeqListNode* SeqList_Delete(SeqList* list, int pos); int main() { Teacher t1, t2, t3, t4; t1.age = 31; t2.age = 32; t3.age = 33; t4.age = 34; SeqList* list; Teacher *it; int reg; list = SeqList_Create(4);//顺序存储必须在先开辟内存。 //进行到每一步,一定要验证 if (list == NULL) { cout << "create fail \n"; return 0; }
//int SeqList_Insert(SeqList* list, SeqListNode* node, int pos);
//因为这里原来的式子node是一个指针,也就是地址,这里的也必须是指针类型的。
// reg = SeqList_Insert(list, (SeqListNode*) t1, 0); 为什么这种的就不对呢? reg = SeqList_Insert(list, (SeqListNode*) &t1, 0); reg = SeqList_Insert(list, (SeqListNode*)&t2, 0); reg = SeqList_Insert(list, (SeqListNode*)&t3, 0); reg = SeqList_Insert(list, (SeqListNode*)&t4, 0); for (int i = 0; i < SeqList_Length(list); i++) { //SeqListNode* SeqList_Get(SeqList* list, int pos); Teacher *temp = (Teacher *)SeqList_Get(list, i); if (temp == NULL) { cout << " failed \n"; return 0; } cout << temp->age << endl; } //SeqListNode* SeqList_Delete(SeqList* list, int pos); while (SeqList_Length(list)) { SeqList_Delete(list, 0); } system("pause"); }
seqlist.h /////////////////////////////////////////////////////////// #pragma once typedef void SeqList; typedef void SeqListNode; SeqList* SeqList_Create(int capacity); void SeqList_Destroy(SeqList* list); void SeqList_Clear(SeqList* list); int SeqList_Length(SeqList* list); int SeqList_Capacity(SeqList* list); int SeqList_Insert(SeqList* list, SeqListNode* node, int pos); SeqListNode* SeqList_Get(SeqList* list, int pos); SeqListNode* SeqList_Delete(SeqList* list, int pos);
seqlist.c //////////////////////////////////////////////// #include "seqlist.h" #include<iostream> #include"string.h" typedef struct Tseqlist { int length; int capacity; int *node; }Tseqlist; SeqList* SeqList_Create(int capacity) { Tseqlist *tem = NULL; int ret = 0; tem = (Tseqlist *)malloc(sizeof(Tseqlist)); if (tem == NULL) { return NULL ; } memset(tem, 0, sizeof(Tseqlist)); tem->node = (int *)malloc(sizeof(int)*capacity); if (tem->node == NULL) { return NULL; } tem->capacity = capacity; tem->length = 0; return tem; } //释放前一步create创建的内存空间 void SeqList_Destroy(SeqList* list) { Tseqlist *D_list = NULL; D_list = (Tseqlist *)list; if (D_list == NULL) { return; } if (D_list->node != NULL) { free(D_list->node); } free(D_list); } //清空就是回到初始状态 这种写法我看不懂,为什么呢? 这样子的话仅仅是自己创建的C_list被初始化,而实际的list依旧没有被初始化 //别人传来的一个seqlist 空类型,但是我知道其实是 Tseqlist类型的 void SeqList_Clear(SeqList* list) { Tseqlist *C_list = NULL; C_list = (Tseqlist *)list; if (C_list == NULL) { return; } C_list->length = 0; } int SeqList_Length(SeqList* list) { Tseqlist *C_list = NULL; if (list == NULL) { return -1; } C_list = (Tseqlist *)list; return (C_list->length); } int SeqList_Capacity(SeqList* list) { Tseqlist *C_list = NULL; if (list == NULL) { return -1; } C_list = (Tseqlist *)list; return (C_list->capacity); } int SeqList_Insert(SeqList* list, SeqListNode* node1, int pos) { Tseqlist *I_list ; if (list == NULL || node1 == NULL || pos < 0 ) { return -1; } I_list = (Tseqlist *)list; //假设插入元素后,超出容量 if (I_list->length > I_list->capacity) { printf("\n 输入的长度超过了容量 \n"); return -1; } // 修正 假设插入的顺序在长度之外 if (pos > I_list->length) { pos = I_list->length; } //正式的插入 for (int i = I_list->length; i > pos; i--) { I_list->node[i] = I_list->node[i - 1]; } I_list->node[pos] = (int ) node1; I_list->length++; return 0; } SeqListNode* SeqList_Get(SeqList* list, int pos) { Tseqlist *I_list ; if (list == NULL || pos < 0) { return NULL; } I_list = (Tseqlist *)list; return (SeqListNode*)I_list->node[pos]; } SeqListNode* SeqList_Delete(SeqList* list, int pos) { Tseqlist *I_list ; if (list == NULL || list == NULL || pos < 0) { return NULL; } I_list = (Tseqlist *)list; for (int i = pos; i < I_list->length; i++) { I_list->node[i] = I_list->node[i + 1]; } I_list->length--; return NULL; }
时间: 2024-10-17 01:49:17