线性表的顺序存储实际很简单,但是本文努力将其写的更加规范。
代码 C
seqlist.h
#ifndef _SEQLIST_H_
#define _SEQLIST_H_
typedef void List;
typedef void ListNode;
//创建并且返回一个线性表
List* List_Create(int capacity);
//销毁一个线性表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
#include "seqlist.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct SeqList
{
int capacity;
int length;
unsigned int **node;//相当于unsigned int *arrqy[]
}SeqList;
//创建并且返回一个线性表
List* List_Create(int capacity)
{
int ret = 0;
SeqList *seqlist = (SeqList*)malloc(sizeof(SeqList));
if (seqlist == NULL)
{
ret = -1;
printf("fuc List_Create() err:%d\n", ret);
}
memset(seqlist, 0, sizeof(SeqList));
seqlist->node = (unsigned int**)malloc(sizeof(unsigned int*) * capacity);
if (seqlist->node == NULL)
{
ret = -2;
printf("fuc List_Create() err:%d\n", ret);
return NULL;
}
seqlist->capacity = capacity;
seqlist->length = 0;
return seqlist;
}
//销毁一个线性表list
void List_Destroy(List* list)
{
SeqList *temp = NULL;
if (list == NULL)
{
return;
}
temp = (SeqList *)list;
if (temp->node != NULL)
{
free(temp->node);
temp->node = NULL;
}
free(temp);
temp = NULL;
}
//将一个线性表list中的所有元素清空, 线性表回到创建时的初始状态
void List_Clear(List* list)
{
SeqList *temp = NULL;
if (list == NULL)
{
return;
}
temp = (SeqList *)list;
temp->length = 0;
}
//返回一个线性表list中的所有元素个数
int List_Length(List* list)
{
SeqList *temp = NULL;
if (list == NULL)
{
return -1;
}
temp = (SeqList *)list;
return temp->length;
}
//向一个线性表list的pos位置处插入新元素node
int List_Insert(List* list, ListNode* node, int pos)
{
int ret = 0;
int i;
SeqList *temp = NULL;
if (list == NULL || node == NULL|| pos<0)
{
ret = -1;
printf("fuc List_Insert() err:%d\n", ret);
return ret;
}
temp = (SeqList *)list;
//判断是不是满了
if (temp->length >= temp->capacity)
{
ret = -2;
printf("fuc List_Insert() err:%d\n", ret);
return ret;
}
//如果插入的位置如果不连续则修正
if (pos > temp->length)
{
pos = temp->length;
}
//元素后移
for (i = temp->length; i > pos; i--)
{
temp->node[i] = temp->node[i - 1];
}
//插入元素
temp->node[pos] = node;//temp->node是二级指针,temp->node[pos]是一级指针
temp->length++;
return 0;
}
//获取一个线性表list的pos位置处的元素
ListNode* List_Get(List* list, int pos)
{
int ret = 0;
SeqList *temp = NULL;
if (list == NULL || pos<0)
{
ret = -1;
printf("fuc List_Get() err:%d\n", ret);
return NULL;
}
temp = (SeqList *)list;
return (ListNode*)temp->node[pos];
}
//删除一个线性表list的pos位置处的元素返回值为被删除的元素,NULL表示删除失败
ListNode* List_Delete(List* list, int pos)
{
int ret = 0;
int i;
SeqList *temp = NULL;
ListNode *node1 = NULL;
if (list == NULL || pos<0)
{
ret = -1;
printf("fuc List_Get() err:%d\n", ret);
return NULL;
}
temp = (SeqList *)list;
node1 = temp->node[pos];
//元素前移
/*for (i = temp->length; i>pos; i--)
{
temp->node[i - 1] = temp->node[i];//切勿这样写,因为每次前移之后,后面的就会覆盖前面的
}*/
for (i = pos; i < temp->length; i++)
{
temp->node[i] = temp->node[i + 1];//元素前移
}
temp->length--;
return (ListNode*)node1;
}
main.c
#include <stdio.h>
#include <stdlib.h>
#include "seqlist.h"
typedef struct Teacher
{
char name[20];
int age;
}Teacher;
int main(vooid)
{
Teacher t1, t2, t3;
t1.age = 20;
t2.age = 30;
t3.age = 40;
List* mylist = List_Create(10);
if (mylist == NULL)
{
printf("fuc List_Create() err:%d\n");
return -1;
}
int ret1 = List_Insert(mylist, &t1, 0);
int ret2 = List_Insert(mylist, &t2, 0);
int ret3 = List_Insert(mylist, &t3, 0);
for (int i = 0; i < List_Length(mylist);i++)
{
Teacher* re = (Teacher*)List_Get(mylist, i);
printf("年龄%d为age=%d\n",i, re->age);
}
List_Delete(mylist, 0);
printf("在0号位置删除一个元素之后\n");
for (int i = 0; i < List_Length(mylist); i++)
{
Teacher* re = (Teacher*)List_Get(mylist, i);
printf("年龄%d为age=%d\n", i, re->age);
}
List_Clear(mylist);
system("pause");
}
执行结果: