首页 > 代码库 > 菜鸟之路--线性表__顺序存储

菜鸟之路--线性表__顺序存储

</pre><pre name="code" class="cpp"> #include <stdio.h>
 #include <stdlib.h>
 //链表的定义以及基本操作
 typedef void List;
 typedef void ListNode;
 //创建一个空链表,并返回
 List * List_Create(int capacity);
 //将链表清空
 void  List_Clear(List * list);
 //销毁链表
 void  List_Destroy(List *list);
 //删除指定位置的数据
 ListNode * List_Delete(List *list,int pos);
 //往链表内插入数据
 int List_Insert(List * list,ListNode * node);
 //得到链表中位置为pos的数据
 ListNode * List_Get(List * list,int pos);
 //返回链表的长度
 int List_Length(List *list);


//顺序链表的相关定义
 typedef struct _tag_linklist  TgList;
 typedef int TgListNode;
 struct _tag_linklist{
            int length;
            int capacity;
            TgListNode * node;
  };

  //线性顺序存储链表具体操作的相关实现
 List * List_Create(int capacity)
 {
     TgList * ret = NULL;
     if(capacity>0){
        //注意此处的内存分配大小计算
        ret = (TgList *)malloc(sizeof(TgList)+sizeof(TgListNode) * capacity);
     }
     if(ret != NULL){
        ret->capacity = capacity;
        ret->length = 0;
     //注意此处加1操作
        ret->node = (TgListNode *)(ret+1);
     }
     return ret;
 }
 void  List_Clear(List * list)
 {
     TgList * tlist = (TgList *)list;
     if(tlist != NULL){
        tlist->length = 0;
     }
 }
void  List_Destroy(List *list)
{
    free(list);
}
int List_Insert(List * list,ListNode * node)
{
    TgList * tlist = (TgList *)list;
    if(tlist->length<tlist->capacity){
       tlist->node[tlist->length++] = (TgListNode) node;
       return 1;
    }
    return 0;


}
ListNode * List_Delete(List *list,int pos)
{
    TgList * tlist = (TgList *)list;
    ListNode * tnode = List_Get(list,pos);
    int i;
    if(tlist !=NULL && pos<tlist->length){
       for(i=pos;i<tlist->length-1;i++)
       {
            tlist->node[i] = tlist->node[i+1];
       }
       tlist->length--;
    }
    return tnode;
}
 ListNode * List_Get(List * list,int pos)
 {
     TgList * tlist = (TgList *)list;
     if(tlist != NULL && pos<tlist->length)
     {
         return (ListNode *)tlist->node[pos];
     }
     return NULL;
 }
 int List_Length(List *list)
 {
     TgList * tlist = (TgList *)list;
     if(tlist !=NULL) {
            return tlist->length;
     }
 }
 //返回链表最大容量
 int List_Capacity(List * list)
 {
     TgList * tlist = (TgList *)list;
     if(tlist != NULL){
        return tlist->capacity;
     }
     return -1;
 }
 //添加输出函数用于具体测试
 void List_Print(List *list)
 {
    TgList * tlist = (TgList *)list;
    int i;
    if(tlist != NULL){
       for(int i=0;i<tlist->length;i++) {
            printf("the %d element %d\n",i+1,tlist->node[i]);
       }
    }
 }
 int main()
 {
     int a[5] ={1,2,3,4,5};
     int i;
     List * list = List_Create(5);
     for(i=0;i<5;i++){
        //注意这里要将int强制转换为void *
        List_Insert(list,(void *)a[i]);
     }
     List_Print(list);
     printf("%d\n",List_Length(list));
     printf("%d\n",List_Capacity(list));
     printf("%d\n",List_Delete(list,3));
     List_Print(list);
     printf("%d\n",List_Length(list));
     printf("%d\n",List_Capacity(list));
     printf("%d\n",List_Get(list,3));
     return 0;
 }


菜鸟之路--线性表__顺序存储