线性表的链式存储——C语言实现

时间:2021-09-17 14:53:45
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();
} t->capacity = max;
t->length = ;
t->space = (void **)malloc(sizeof(void *)*max); if (t->space==NULL)
{
printf("初始化失败\n");
exit();
}
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 = ;
memset(slist->space, , sizeof(void *)*slist->capacity); return;
} //返回一个线性表list中的所有元素个数
int List_Length(List* list)
{
if (list == NULL)
{
printf("err");
return -;
} TSeqList* slist = (TSeqList*)list;
return slist->length;
return ;
} //向一个线性表list的pos位置处插入新元素node
int List_Insert(List* list, ListNode* node, int pos)
{
TSeqList* tlist = (TSeqList *)list;
int ret = ;
if (list == NULL || node == NULL)
{
ret = -; //输入参数有误
return ret;
}
if (tlist->capacity == tlist->length)
{
ret = -; //空间已满
return ret;
}
if (pos > tlist->length||pos<)
{
pos = tlist->length + ;
} //假设有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-; i--)
{
tlist->space[i] = tlist->space[i - ];
} tlist->space[pos - ] = 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< || pos>tlist->length)
{
return NULL;
} return tlist->space[pos - ];
} //删除一个线性表list的pos位置处的元素 返回值为被删除的元素,NULL表示删除失败
ListNode* List_Delete(List* list, int pos)
{
TSeqList* tlist =(TSeqList *)list; ListNode *p; if (list == NULL)
{
return NULL;
} if (pos< || pos>tlist->length)
{
return NULL;
}
//先取出元素,然后位移
p = tlist->space[pos - ];
for (int i = pos; i < tlist->length; i++)
{
tlist->space[i - ] = 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[];
}Teacher; int main()
{
List *list = List_Create();
int ret = ;
int count = ;
Teacher t1,t2,t3;
t1.age = ;
strcpy(t1.name, "张三"); t2.age = ;
strcpy(t2.name, "李四"); t3.age = ;
strcpy(t3.name, "王五"); //向一个线性表list的pos位置处插入新元素node
ret = List_Insert(list, (void *)&t1, );
if (ret!=)
{
printf("err\n");
}
ret = List_Insert(list, (void *)&t2, );
if (ret != )
{
printf("err\n");
}
ret = List_Insert(list, (void *)&t3, );
if (ret != )
{
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 = ; i < ; i++)
{
ListNode *p = List_Get(list, i+);
printf("%d\t%s\n", ((Teacher *)(p))->age, ((Teacher *)(p))->name);
}
//删除一个线性表list的pos位置处的元素 返回值为被删除的元素,NULL表示删除失败 for (int i = ; i < ; i++)
{
ListNode *p = List_Delete(list,i+);
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 ;
} int main02()
{
int *a = malloc(sizeof(int));
free(a);
printf("hello\n");
system("pause");
return ;
}