一线性表

Posted Alone0710

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一线性表相关的知识,希望对你有一定的参考价值。

1. 线性表

  1. 功能:创建、销毁、清空、插入、删除元素、获取某个位置元素、获取线性表的长度
    在这里插入图片描述
  2. 线性表在程序中表现为一种特殊的数据类型

2. 语言描述

//.h
#ifndef _SEQLIST_H_
#define _SEQLIST_H_

typedef void SeqList;
typedef void SeqListNode;

SeqList* SeqList_Create(int capacity);

void SeqList_Destroy(SeqList* list);

void SeqList_Clear(SeqList* list);

int SeqList_Length(SeqList* list);

int SeqList_Capacity(SeqList* list);

int SeqList_Insert(SeqList* list, SeqListNode* node, int pos);

SeqListNode* SeqList_Get(SeqList* list, int pos);

SeqListNode* SeqList_Delete(SeqList* list, int pos);

#endif
#include <stdio.h>
#include <malloc.h>
#include "SeqList.h"

typedef unsigned int TSeqListNode;

typedef struct _tag_SeqList
{
    int capacity;
    int length;
    TSeqListNode* node;
}TSeqList;

SeqList* SeqList_Create(int capacity)
{
    TSeqList* ret = NULL;

    if(capacity >= 0)
    {
        ret = (TSeqList*)malloc(sizeof(TSeqList) + sizeof(TSeqListNode)*capacity);
    }
    if(ret!=NULL)
    {
        ret->capacity = capacity;
        ret->length = 0;
        ret->node = (TSeqListNode*)(ret + 1);//ret+1后,node指针指向sizeof(TSeqListNode)*capacity申请出来的空间
    }

    return ret;
}

void SeqList_Destroy(SeqList* list)
{
    free(list);
}

void SeqList_Clear(SeqList* list)
{
    TSeqList* sList = (TSeqList*)list;
    if(sList != NULL)
    {
        sList->length = 0;
    }
}

int SeqList_Length(SeqList* list)
{
    TSeqList* sList = (TSeqList*)list;
    int ret = -1;
    if(sList != NULL)
    {
        ret = sList->length;
    }
    return ret;
}

int SeqList_Capacity(SeqList* list)
{
    TSeqList* sList = (TSeqList*)list;
    int ret = -1;
    if(sList != NULL)
    {
        ret = sList->capacity;
    }
    return ret;
}

//插入元素(这里存储的是要插入的数据地址,所以能够存储任何类型数据)
int SeqList_Insert(SeqList* list, SeqListNode* node, int pos)
{
    TSeqList* sList = (TSeqList*)list;
    int ret = (sList != NULL);
    int i = 0;

    ret = ret&&(sList->length+1<=sList->capacity);
    ret = ret&&(0<=pos);

    if(ret)
    {
        if(pos >= sList->length)
        {
            pos = sList->length;
        }
        for(i = sList->length;i>pos;i--)//length比下标大1
        {
            sList->node[i] = sList->node[i-1];
        }
        sList->node[i] = (TSeqListNode)node;

        sList->length++;
    }
    return ret;
}

//获取第pos个元素,得到元素后须要类型转换,其实返回的只是void*类型的地址值
SeqListNode* SeqList_Get(SeqList* list, int pos)
{
    TSeqList* sList = (TSeqList*)list;
    SeqListNode* ret = NULL;

    if( (sList!=NULL) && (0<=pos) && (pos<=sList->length) )
    {
        ret = (SeqListNode*)(sList->node[pos]);
    }

    return ret;
}

SeqListNode* SeqList_Delete(SeqList* list, int pos)
{
    TSeqList* sList = (TSeqList*)list;
    SeqListNode* ret = SeqList_Get(list,pos);
    int i=0;

    if(ret != NULL)
    {
        for(i=pos+1;i<sList->length;i++)
        {
            sList->node[i-1] = sList->node[i];
        }
        sList->length--;
    }

    return ret;
}



#include <stdio.h>
#include <stdlib.h>
#include "SeqList.h"
int main()
{

    SeqList* list = SeqList_Create(5);
    int i=0;
    int j=1;
    int k=2;
    int x=3;
    int y=4;
    int z=5;
    int index=0;

    SeqList_Insert(list,&i,0);
    SeqList_Insert(list,&j,0);
    SeqList_Insert(list,&k,0);
    SeqList_Insert(list,&x,0);
    SeqList_Insert(list,&y,0);
    SeqList_Insert(list,&z,0);

    for(index=0;index<SeqList_Length(list);index++)
    {
        int *p = (int*)SeqList_Get(list,index);

        printf("%d\\n",*p);
    }

     printf("\\n");

    while(SeqList_Length(list)>0)
    {
        int *p = (int*)SeqList_Delete(list,0);
        printf("%d\\n",*p);
    }

    SeqList_Destroy(list);

   return 0;
}

3. 小结

采用void作为数据类型,起到了封装作用,保护了数据的安全行,用户无法通过创建的list去修改capacity等数据。

以上是关于一线性表的主要内容,如果未能解决你的问题,请参考以下文章

如何在android中的地图片段内中心线性布局?

博客作业2---线性表

第02次作业-线性表

第02次作业-线性表

线性表——顺序表和链表

第02次作业-线性表