线性顺序表实现(C语言)

2 篇文章 0 订阅
1 篇文章 0 订阅

1. SeqList.h


#ifndef _LINKLIST_H

#define _LINKLIST_H


typedef void SeqList;


typedef void SeqListNode;



SeqList* SeqList_Create(intcapacity);

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

SeqListNode* SeqList_Get(SeqList*list,intposition);

SeqListNode* SeqList_Delete(SeqList*list,intposition);

int SeqList_GetLength(SeqList*list);

int SeqList_GetCapacity(SeqList*list);

void SeqList_Destroy(SeqList*list);

void SeqList_Clear(SeqList*list);


#endif


2. SeqList.c


#include <stdlib.h>

#include <stdio.h>

#include <string.h>

#include "SeqList.h"


typedef struct _Tag_SeqList{

int capacity;

int length;

unsigned int ** node;

} TSeqList;


SeqList* SeqList_Create(intcapacity) {

int ret = 0;

TSeqList* list = (TSeqList*)malloc(sizeof(TSeqList));

if (list == NULL) {

ret = -1;

printf("function SeqList_Create error: malloc error, ret = %d", ret);

}

memset(list, 0, sizeof(list));



list->node = (unsigned int **)malloc(sizeof(unsignedint *) *capacity);

if (list->node == NULL) {

ret = -2;

printf("function SeqList_Create error: malloc error, ret = %d", ret);

}

list->capacity = capacity;

list->length = 0;

return list;

}


int SeqList_Insert(SeqList*list,SeqListNode*node, int position) {

int ret = 0;

if (list ==NULL) {

ret = -1;

printf("function SeqList_Insert error: ret = %d", ret);

}


if (position < 0) {

ret = -2;

printf("function SeqList_Insert error: ret = %d", ret);

}


TSeqList* tlist = (TSeqList*)list;

if (tlist->length >= tlist->capacity) {

ret = -3;

printf("function SeqList_Insert error: ret = %d", ret);

}


if (position >= tlist->length) {

position = tlist->length;

}


int i = 0;

for (i = tlist->length; i > position; i--) {

tlist->node[i] = tlist->node[i-1];

}


tlist->node[i] = (unsigned int *)node;

tlist->length++;


return ret;

}


SeqListNode* SeqList_Get(SeqList*list,intposition) {

int ret = 0;

if (list ==NULL) {

ret = -1;

printf("function SeqList_Get error: ret = %d", ret);

}


if (position < 0) {

ret = -2;

printf("function SeqList_Get error: ret = %d", ret);

}


TSeqList* tlist = (TSeqList*)list;

if (position >= tlist->length) {

ret = -3;

printf("function SeqList_Get error: ret = %d", ret);

}


return tlist->node[position];

}


SeqListNode* SeqList_Delete(SeqList*list,intposition) {

int ret = 0;

if (list ==NULL) {

ret = -1;

printf("function SeqList_Delete error: ret = %d", ret);

}


if (position < 0) {

ret = -2;

printf("function SeqList_Delete error: ret = %d", ret);

}


TSeqList* tlist = (TSeqList*)list;

if (position >= tlist->length) {

ret = -3;

printf("function SeqList_Delete error: ret = %d", ret);

}


SeqListNode* node = tlist->node[position];


for (int i =position; i < tlist->length-1; i++) {

tlist->node[i] = tlist->node[i+1];

}

tlist->length--;

return node;

}


int SeqList_GetLength(SeqList*list) {

int ret = 0;

if (list ==NULL) {

ret = -1;

printf("function SeqList_GetLength error: ret = %d", ret);

}

TSeqList* tlist = (TSeqList*)list;

return tlist->length;

}


int SeqList_GetCapacity(SeqList*list) {

int ret = 0;

if (list ==NULL) {

ret = -1;

printf("function SeqList_GetCapacity error: ret = %d", ret);

}

TSeqList* tlist = (TSeqList*)list;

return tlist->capacity;

}


void SeqList_Destroy(SeqList*list) {

int ret = 0;

if (list ==NULL) {

ret = -1;

printf("function SeqList_Destroy error: ret = %d", ret);

}


TSeqList* tlist = (TSeqList*)list;

if (tlist->node != NULL) {

free(tlist->node);

tlist->node = NULL;

}

if (tlist != NULL) {

free(tlist);

tlist = NULL;

}

return;

}


void SeqList_Clear(SeqList*list) {

int ret = 0;

if (list ==NULL) {

ret = -1;

printf("function SeqList_Clear error: ret = %d", ret);

}


TSeqList* tlist = (TSeqList*)list;

tlist->length = 0;

return;

}



3. SeqListMain.c


#define _CRT_SECURE_NO_WARNINGS

#include <stdlib.h>

#include <stdio.h>

#include <string.h>

#include "SeqList.h"


typedef struct Teacher {

int age;

char name[64];

} Teacher;


void main() {

SeqList* list = SeqList_Create(10);

if (list == NULL) {

return;

}

Teacher t1, t2, t3;

strcpy(t1.name, "A");

strcpy(t2.name, "B");

strcpy(t3.name, "C");


SeqList_Insert(list, (SeqListNode*)&t1, 0);

SeqList_Insert(list, (SeqListNode*)&t2, 0);

SeqList_Insert(list, (SeqListNode*)&t3, 0);

int length = SeqList_GetLength(list);

for (int i = 0; i < length; i++) {

Teacher* t = (Teacher*)SeqList_Get(list, i);

printf("%s\n", t->name);

}


printf("delete\n");

while (SeqList_GetLength(list) > 0) {

Teacher* t = (Teacher*)SeqList_Delete(list, 0);

printf("%s\n", t->name);

}


printf("length:%d\n", SeqList_GetLength(list));

printf("capacity:%d\n", SeqList_GetCapacity(list));

SeqList_Clear(list);

SeqList_Destroy(list);

system("pause");


}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值