顺序表



操作:

创建线性表

销毁线性表

清空线性表

将元素插入线性表

将元素删除线性表

线性表中对应位置的元素 

获取现象表的长度

获取线性表的最大长度

优点:无需增加额外的空间

  快速获取表中合法的元素

缺点:插入和删除需要移动大量的元素

  线性表长度变化较大时难以确定存储空间容量

SeqList.c

#include <stdio.h>
#include <malloc.h>
#include "SeqList.h"
/*
实现线性表的元素为指针 
如果是整数  TSeqListNode类型修改为unsigned int
如果是字符串 TSeqListNode类型修改为char 
*/ 
typedef unsigned int TSeqListNode; //定义线性表元素 


typedef struct _tag_SeqList
{
    int capacity;        //线性表的容量
    int length;          //当前容量的大小
    TSeqListNode** node;  //线性表的元素为指针,该指针指向 TSeqListNode类型 
} TSeqList;


/*
功能:创建线性表
参数:链表最大元素的个数
返回:
*/
SeqList* SeqList_Create(int capacity) // O(1)
{
    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);//node表示首元素的首地址,线性表的元素为指针,故强制类型转换为指针的指针 
    }
    
    return ret;
}
/*
功能:销毁
参数:
返回:
*/
void SeqList_Destroy(SeqList* list) // O(1)
{
    free(list);
}
/*
功能:清空
参数:
返回:
*/
void SeqList_Clear(SeqList* list) // O(1)
{
    TSeqList* sList = (TSeqList*)list;   //SeqList为void,必须进行转换
    
    if( sList != NULL )
    {
        sList->length = 0;
    }
}
/*
功能:获取线性表长度
参数:
返回:-1表示线性表不存在
*/
int SeqList_Length(SeqList* list) // O(1)
{
    TSeqList* sList = (TSeqList*)list;
    int ret = -1;
    
    if( sList != NULL )
    {
        ret = sList->length;
    }
    
    return ret;
}
/*
功能:获取线性表最大的元素个数
参数:
返回:-1表示线性表不存在
*/
int SeqList_Capacity(SeqList* list) // O(1)
{
    TSeqList* sList = (TSeqList*)list;
    int ret = -1;
    
    if( sList != NULL )
    {
        ret = sList->capacity;
    }
    
    return ret;
}
/*
功能:插入元素 
参数:
返回:成功返回1.失败返回0 
*/
int SeqList_Insert(SeqList* list, SeqListNode* node, int pos) // O(n) 
{
    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--) //移动元素
        {
            sList->node[i] = sList->node[i-1];
        }
        
     sList->node[i] = (TSeqListNode*)node;//线性表元素的地址
        
        sList->length++;
    }
    
    return ret;
}
/*
功能:获取线性表中对应位置的元素 
参数:
返回:线性表中元素的地址,失败返回NULL 
*/
SeqListNode* SeqList_Get(SeqList* list, int pos) // O(1) 
{
    TSeqList* sList = (TSeqList*)list;
    SeqListNode* ret = NULL;
    
    if( (sList != NULL) && (0 <= pos) && (pos < sList->length) )
    {
        ret = (SeqListNode*)(sList->node[pos]);
    }
    
    return ret;
}
/*
功能:删除元素 
参数:
返回:失败返回NULL,成功返回删除前的地址 
*/
SeqListNode* SeqList_Delete(SeqList* list, int pos) // O(n)
{
    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;
}

Seqlist.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



examp.c

#include <stdio.h>
#include <stdlib.h>
#include "SeqList.h"


/* run this program using the console pauser or add your own getch, system("pause") or input loop */


int main(int argc, char *argv[]) 
{
    SeqList* list = SeqList_Create(5);
    int index = 0;    
    
    int i = 0;
    int j = 1;
    int k = 2;
    int x = 3;
    int y = 4;
    int z = 5;
    
    
    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("TEST DELETE&&Insert\n");
    SeqList_Delete(list, 1);
    SeqList_Insert(list,&i,1) ;
    while( SeqList_Length(list) > 0 )
    {
        int* p = (int*)SeqList_Delete(list, 0);
        
        printf("%d\n", *p);
    }
    
    
    SeqList_Destroy(list);
    
    return 0;
}


你可能感兴趣的:(顺序表)