打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
线性表顺序存储(c实现)
seqlist.h
========================================
#ifndef _MY_SEQLIST_H
#define _MY_SEQLIST_H
typedef void SeqList;
typedef void SeqLinkNode;
SeqList* SeqList_Create(int capacity);
void SeqList_Clear(SeqList* slist);
void SeqList_Destroy(SeqList* slist);
int SeqList_Length(SeqList* slist);
int SeqList_Capacity(SeqList* slist);
int SeqList_Insert(SeqList* slist, int pos, SeqLinkNode* node);
SeqLinkNode* SeqList_Get(SeqList* slist, int pos);
SeqLinkNode* SeqList_Delete(SeqList* slist, int pos);
#endif // _MY_SEQLIST_H
======================
seqlinklist.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "seqlist.h"
typedef struct tag_SeqList
{
int length;
int capacity;
unsigned int *node;
}TSeqList;
SeqList* SeqList_Create(int capacity)
{
TSeqList *tlist = NULL;
if(capacity < 0 )
{
return NULL;
}
tlist = (TSeqList*)malloc(sizeof(TSeqList) + sizeof(unsigned int)* capacity);
if(tlist == NULL)
{
return NULL;
}
memset(tlist, 0, sizeof(TSeqList) + sizeof(unsigned int)* capacity);
tlist->node = (unsigned int*)(tlist+1);
tlist->length = 0;
tlist->capacity = capacity;
return tlist;
}
void SeqList_Clear(SeqList* slist)
{
if(slist == NULL)
{
return;
}
TSeqList *tlist = NULL;
tlist =  (TSeqList*)slist;
tlist->length = 0;
return ;
}
void SeqList_Destroy(SeqList* slist)
{
if(slist != NULL)
{
free(slist);
}
return ;
}
int SeqList_Length(SeqList* slist)
{
if(slist == NULL)
{
return -1;
}
TSeqList *tlist = NULL;
tlist =  (TSeqList*)slist;
return tlist->length;
}
int SeqList_Capacity(SeqList* slist)
{
if(slist == NULL)
{
return -1;
}
TSeqList *tlist = NULL;
tlist =  (TSeqList*)slist;
return tlist->capacity;
}
int SeqList_Insert(SeqList* slist, int pos, SeqLinkNode* node)
{
TSeqList *tlist = NULL;
tlist =  (TSeqList*)slist;
if(slist == NULL || node == NULL)
{
return -1;
}
if(pos<0 || pos >= tlist->capacity)
{
return -2;
}
if(tlist->length >= tlist->capacity)
{
return -3;
}
if(pos >= tlist->length)
{
pos = tlist->length;
}
int i;
for(i = tlist->length; i > pos; i--)
{
tlist->node[i] = tlist->node[i-1];
}
tlist->node[pos] = (unsigned int)node;
tlist->length++;
return 0;
}
SeqLinkNode* SeqList_Get(SeqList* slist, int pos)
{
TSeqList *tlist = NULL;
tlist =  (TSeqList*)slist;
if(slist == NULL || pos < 0 || pos >= tlist->length)
{
return NULL;
}
SeqLinkNode* ret = NULL;
ret = (SeqLinkNode*)(tlist->node[pos]);
return ret;
}
SeqLinkNode* SeqList_Delete(SeqList* slist, int pos)
{
TSeqList *tlist = NULL;
tlist =  (TSeqList*)slist;
if(slist == NULL || pos < 0 || pos >= tlist->length)
{
return NULL;
}
SeqLinkNode* ret = NULL;
ret = (SeqLinkNode*)(tlist->node[pos]);
int i;
for(i = pos+1; i < tlist->length; i++)
{
tlist->node[i-1] = tlist->node[i];
}
tlist->length--;
return ret;
}
======================
seqlink_test.c
===================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "seqlist.h"
typedef struct Teacher
{
char name[64];
int age;
}Teacher;
int main()
{
SeqList* slist = SeqList_Create(20);
Teacher t1,t2,t3;
t1.age = 31;
t2.age = 32;
t3.age = 33;
SeqList_Clear(slist);
printf("the length of list is :%d, the capacity of list is :%d \n", SeqList_Length(slist), SeqList_Capacity(slist));
printf("=================");
SeqList_Insert(slist, 0, (SeqLinkNode*)&t1);
SeqList_Insert(slist, 0, (SeqLinkNode*)&t2);
SeqList_Insert(slist, 0, (SeqLinkNode*)&t3);
printf("the length of list is :%d, the capacity of list is :%d \n", SeqList_Length(slist), SeqList_Capacity(slist));
int i;
for(i=0; i< SeqList_Length(slist); i++)
{
Teacher* ret = (Teacher*)SeqList_Get(slist, i);
printf(" the ret age: %d \n", ret->age);
}
for(i=0; i<3; i++)
{
Teacher* ret = (Teacher*)SeqList_Delete(slist, 0);
printf(" the ret age: %d \n", ret->age);
}
printf("the length of list is :%d, the capacity of list is :%d \n", SeqList_Length(slist), SeqList_Capacity(slist));
SeqList_Destroy(slist);
printf("Hello world!\n");
return 0;
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
第2章 线性表
C数据结构与算法02——线性表(传统链表/单向链表)
javascript对下拉列表框(select)的操作
java字符串数学公式运算
树的实现与操作(C语言实现)
jQuery源码解析1(Utilities)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服