SeqList.h #ifndef _WBM_LIST_H_ #define _WBM_LIST_H_ typedef void List; typedef void ListNode; //创建并且返回一个空的线性表 List* List_Create(); //销毁一个线性表list void List_Destroy(List* list); //将一个线性表list中的所有元素清空, 线性表回到创建时的初始状态 void List_Clear(List* list); //返回一个线性表list中的所有元素个数 int List_Length(List* list); //向一个线性表list的pos位置处插入新元素node int List_Insert(List* list, ListNode* node, int pos); //获取一个线性表list的pos位置处的元素 ListNode* List_Get(List* list, int pos); //删除一个线性表list的pos位置处的元素 返回值为被删除的元素,NULL表示删除失败 ListNode* List_Delete(List* list, int pos); #endif
SeqList.c #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string.h> #include "memwatch.h" #include"SeqList.h" typedef struct _TSeqList { int capacity; //容量 int length; //长度 void **space; //指针空间 }TSeqList; List* List_Create(int max) { List* list = NULL; TSeqList *t = (TSeqList *)malloc(sizeof(TSeqList)); if (t==NULL) { printf("初始化失败\n"); exit(0); } t->capacity = max; t->length = 0; t->space = (void **)malloc(sizeof(void *)*max); if (t->space==NULL) { printf("初始化失败\n"); exit(0); } list = (List *)t; return list; } //销毁一个线性表list void List_Destroy(List* list) { TSeqList* slist = (TSeqList*)list; if (list == NULL) { return; } if (slist->space != NULL) { free(slist->space); } if (slist != NULL) { free(slist); } return; } //将一个线性表list中的所有元素清空, 线性表回到创建时的初始状态 void List_Clear(List* list) { TSeqList* slist = (TSeqList*)list; if (list == NULL) { return; } slist->length = 0; memset(slist->space, 0, sizeof(void *)*slist->capacity); return; } //返回一个线性表list中的所有元素个数 int List_Length(List* list) { if (list == NULL) { printf("err"); return -1; } TSeqList* slist = (TSeqList*)list; return slist->length; return 0; } //向一个线性表list的pos位置处插入新元素node int List_Insert(List* list, ListNode* node, int pos) { TSeqList* tlist = (TSeqList *)list; int ret = 0; if (list == NULL || node == NULL) { ret = -1; //输入参数有误 return ret; } if (tlist->capacity == tlist->length) { ret = -2; //空间已满 return ret; } if (pos > tlist->length||pos<1) { pos = tlist->length + 1; } //假设有7个元素,要插入第8个位置 ,就相当于插入list[7] 则 i=7;i>=7所以会移动一次。所以不应该等于 //假设有7个元素,要插入第6个位置 ,就相当于list[7]=list[6];list[6]=list[5]; 移动两次 i=7;i<6-1=5; i=7执行一次,i=6执行一次,i=5不会执行 for (int i = tlist->length; i > pos-1; i--) { tlist->space[i] = tlist->space[i - 1]; } tlist->space[pos - 1] = node; tlist->length++; return ret; } //获取一个线性表list的pos位置处的元素 ListNode* List_Get(List* list, int pos) { TSeqList* tlist = (TSeqList *)list; if (list == NULL) { return NULL; } if (pos<1 || pos>tlist->length) { return NULL; } return tlist->space[pos - 1]; } //删除一个线性表list的pos位置处的元素 返回值为被删除的元素,NULL表示删除失败 ListNode* List_Delete(List* list, int pos) { TSeqList* tlist =(TSeqList *)list; ListNode *p; if (list == NULL) { return NULL; } if (pos<1 || pos>tlist->length) { return NULL; } //先取出元素,然后位移 p = tlist->space[pos - 1]; for (int i = pos; i < tlist->length; i++) { tlist->space[i - 1] = tlist->space[i]; } return p; }
main.c #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string.h> #include "memwatch.h" #include "SeqList.h" typedef struct _Teacher { int age; char name[20]; }Teacher; int main() { List *list = List_Create(10); int ret = 0; int count = 0; Teacher t1,t2,t3; t1.age = 18; strcpy(t1.name, "张三"); t2.age = 19; strcpy(t2.name, "李四"); t3.age = 20; strcpy(t3.name, "王五"); //向一个线性表list的pos位置处插入新元素node ret = List_Insert(list, (void *)&t1, 1); if (ret!=0) { printf("err\n"); } ret = List_Insert(list, (void *)&t2, 1); if (ret != 0) { printf("err\n"); } ret = List_Insert(list, (void *)&t3, 1); if (ret != 0) { printf("err\n"); } count = List_Length(list); printf("线性表目前长度为:%d\n",count); //获取一个线性表list的pos位置处的元素 ListNode* List_Get(List* list, int pos); printf("年龄\t姓名\n"); for (int i = 0; i < 3; i++) { ListNode *p = List_Get(list, i+1); printf("%d\t%s\n", ((Teacher *)(p))->age, ((Teacher *)(p))->name); } //删除一个线性表list的pos位置处的元素 返回值为被删除的元素,NULL表示删除失败 for (int i = 0; i < 2; i++) { ListNode *p = List_Delete(list,i+1); if (p != NULL) { printf("%d\t%s\n", ((Teacher *)(p))->age, ((Teacher *)(p))->name); } } List_Clear(list); //销毁一个线性表list List_Destroy(list); printf("hello\n"); //system("pause"); return 0; } int main02() { int *a = malloc(sizeof(int)); free(a); printf("hello\n"); system("pause"); return 0; }
时间: 2024-10-17 07:12:05