线性表(1)

//main.c
#include <stdio.h>
#include <stdlib.h>
#include "SeqList.h"

int main()
{
SeqList* list = SeqList_Creat(6);

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_Destory(list);

return 0;
}

//Seqlist.h
#ifndef SEQLIST_H_INCLUDED
#define SEQLIST_H_INCLUDED

typedef void SeqList; //数据封装,有效防止误操作
typedef void SeqListNode;

SeqList* SeqList_Creat(int capacity);//创建一个顺序表
int SeqList_Capacity(SeqList* list);//表的最大容量
void SeqList_Destory(SeqList* list);//销毁线性表
void SeqList_Clear(SeqList* list);//清空线性表
int SeqList_Length(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 // SEQLIST_H_INCLUDED

//Seqlist.c
#include "SeqList.h"
#include <stdio.h>
#include <malloc.h>

typedef unsigned int TSeqListNode;

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

SeqList* SeqList_Creat(int capacity)//创建一个顺序表 O(1)
{
TSeqList* ret = NULL;
if(capacity >= 0)
{
//ret = (TSeqList*)malloc(sizeof(TSeqList)) + sizeof(SeqListNode)*capacity);
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_Destory(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;
// printf("*****\n");//调试输出用的
int ret = -1;

if(sList != NULL)
{
ret = sList->length;
}
//printf("rrrrrrrrrrrrr\n");
return ret;
}

int SeqList_Capacity(SeqList* list)//得到线性表容量
{
TSeqList* sList = (SeqList*)list;

int ret = -1;

if(sList != NULL);
{
ret = sList->capacity;
}
return ret;
}

int SeqList_Insert(SeqList* list, SeqListNode* node, int pos)//向表中插入元素 O(N)
{
//printf("ddddd\n");//调试输出用的
TSeqList* sList = (TSeqList*)list;//强制类型转换

int ret = (sList != NULL);//判断链表合法性
int i = 0;

ret = ret && (sList->length + 1 <= sList->capacity);//插入位置合法性判断
ret = ret && (pos >= 0);

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++;//表长度加1

return ret;
}
}

SeqListNode* SeqList_Get(SeqList* list, int pos)//得到表中某一个位置的值
{
TSeqList* sList = (TSeqList*)list;
SeqListNode* ret = NULL;

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

SeqListNode* SeqList_Delete(SeqList* list, int pos)//O(n)
{
TSeqList* sList = (TSeqList*)list;
TSeqList* ret = SeqList_Get(list, pos);

int i = 0;

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

sList->length--;

return ret;
}


全部评论

相关推荐

11-15 19:28
已编辑
蚌埠坦克学院 硬件开发
点赞 评论 收藏
分享
头像
10-16 09:58
已编辑
门头沟学院 Java
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务