C语言实现一般线性表的顺序存储,以下是在VS2013编译通过的,欢迎讨论。有关数据结构的概念可看我的另一篇文章http://blog.csdn.net/linuxlsl/article/details/45649655
一、头文件ArrList.h
#ifndef _ARRLIST_H
#define _ARRLIST_H
typedef struct
{
int x;
int y;
}Point;
typedef struct
{
Point *pt;//节点
int length;//链表当前长度
int size;//链表总容量
}ArrList;
Point *CreateArrList(int nLength); //创建长为 nLength 的线性表
void DestroyArrList(ArrList *list); //销毁线性表
void ClearArrList(ArrList *list); //置空线性表
int IsEmptyArrList(ArrList *list); //检测线性表是否为空
int GetArrListLength(ArrList *list); //获取线性表长度
int GetArrListSize(ArrList *list); //获取线性表总容量
Point* GetArrListElement(ArrList *list, int pos); //获取线性表中第pos个元素
int InsertToArrList(ArrList *list, int pos,Point* pt);//插入一个元素 pt 到线性表 nPos 处
Point* DeleteFromArrList(ArrList *list, int pos);//从线性表上删除一个元素
#endif
二、实现文件ArrList.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ArrList.h"
//创建长为 nLength 的线性表
Point* CreateArrList(int nLength)
{
ArrList *ret = NULL;
if (nLength < 0)
return NULL;
ret = (Point*)malloc(sizeof(ArrList)+sizeof(Point)*nLength);
if (ret == NULL)
return NULL;
ret->pt = (Point*)(ret + 1);//ret向后跳sizeof(ArrList)
ret->length = 0;
ret->size = nLength;
return ret;
}
//销毁线性表
void DestroyArrList(ArrList *list)
{
if (list == NULL)
return;
free(list);
return;
}
//置空线性表
void ClearArrList(ArrList *list)
{
if (list == NULL)
return;
list->length = 0;
}
//检测线性表是否为空
int IsEmptyArrList(ArrList *list)
{
return list->length == 0 ? 1 : 0;
}
//获取线性表长度
int GetArrListLength(ArrList *list)
{
if (list == NULL)
return -1;
return list->length;
}
//获取线性表总容量
int GetArrListSize(ArrList *list)
{
if (list == NULL)
return -1;
return list->size;
}
//获取线性表中第pos个元素
Point* GetArrListElement(ArrList *list, int pos)
{
Point* ret = NULL;
if (list == NULL || pos<0 || pos >= list->length)
{
return NULL;
}
ret = &list->pt[pos];
// pt = &list->pt[pos];
// pt->x = list->pt[pos].x;
// pt->y = list->pt[pos].y;
return ret;
}
//插入一个元素 pt 到线性表 nPos 处
int InsertToArrList(ArrList *list, int pos, Point *pt)
{
int i = 0;
if (list == NULL || pt == NULL)
{
return -1;
}
//查看是不是满了
if (list->length >= list->size)
{
return -2;
}
//位置错误判断
if (pos<0 || pos >= list->size)
{
return -3;
}
//优化
if (pos >= list->length)
{
pos = list->length;
}
//插入算法
//从pos位置处开始,把数组后面的元素依此后移
for (i = list->length; i>pos; i--)
{
//把前的元素往后移
list->pt[i] = list->pt[i - 1];
}
//循环跳出以后,pos正好是,要出入的位置
list->pt[pos] = *pt;
list->length++;
return 0;
}
//从线性表上删除一个元素
Point* DeleteFromArrList(ArrList *list, int pos)
{
int i = 0;
Point* ret = NULL;
if (list == NULL || pos<0 || pos >= list->length)
{
return -1;
}
ret = &(list->pt[pos]);
//删除算法
for (i = pos; i<list->length; i++)
{
list->pt[i] = list->pt[i+1];
}
list->length--;
return ret;
}
三、测试文件ArrListTest.c
#include<stdio.h>
#include "stdlib.h"
#include "string.h"
#include "ArrList.h"
int main()
{
int i = 0;
ArrList *list = NULL;
Point P1, P2, P3;
P1.x = 0;
P1.y = 0;
P2.x = 2;
P2.y = 4;
P3.x = 3;
P3.y = 3;
list = CreateArrList(10);
printf("len:%d\n", GetArrListLength(list));
printf("size:%d\n", GetArrListSize(list));
InsertToArrList(list, 0, &P1);
InsertToArrList(list, 0, &P2);
InsertToArrList(list, 0, &P3);
printf("len:%d\n", GetArrListLength(list));
//循环遍历
for (i = 0; i < GetArrListLength(list); i++)
{
Point* ret = GetArrListElement(list, i);
if (ret != NULL)
{
printf("P(%d,%d)\n",ret->x ,ret->y);
}
}
for (i = 0; i < GetArrListLength(list); i++)
{
DeleteFromArrList(list, 0);
printf("len:%d\n", GetArrListLength(list));
}
DestroyArrList(list);
system("pause");
return 0;
}
四、运行结果: