线性表
概念
顺序表
示例:创建一个存储学生信息的顺序表
表头(Tlen总长度, Clen当前长度)
函数
#include <seqlist.c>
#include <stdio.h>
#include <stdlib.h>
#include "seqlist.h"
#include <string.h>
SeqList *CreateSeqList(int len)
{
SeqList *s1 = (SeqList *)malloc(sizeof(SeqList));
if(NULL == s1)
{
perror("Create SeqList malloc fail");
return NULL;
}
s1->head = (DATATYPE *)malloc(sizeof(DATATYPE)*len);
if(NULL == s1->head)
{
perror("create sqlist head malloc");
return NULL;
}
s1->tlen = len;
s1->clen = 0;
return s1;
}
int IsFullSeqList(SeqList *list)
{
return list->clen == list->tlen;
}
int InsertTailSeqList(SeqList *list, DATATYPE *data)
{
if(IsFullSeqList(list))
{
return -1;
}
memcpy(&list->head[list->clen], data, sizeof(DATATYPE));
++list->clen;
return 0;
}
int get_size_seqlist(SeqList *list)
{
return list->clen;
}
int ShowSeqList(SeqList *list)
{
int len = get_size_seqlist(list);
int i = 0 ;
for(i = 0; i < len; ++i)
{
printf("%s %c %d %d\n", list->head[i].name, list->head[i].sex, list->head[i].age, list->head[i].score);
}
return 0;
}
int InsertPosSeqList(SeqList *list, DATATYPE *data, int pos)
{
if(IsFullSeqList(list) || pos > list->clen)
{
perror("insert fail");
return -1;
}
int i = 0;
for(i = list->clen; pos<i; --i)
{
list ->head[i] = list->head[i-1];
}
memcpy(&list->head[pos], data, sizeof(DATATYPE));
list->clen++;
return 0;
}
int FindSeqList(SeqList *list, char *name)
{
int len = get_size_seqlist(list);\
int i = 0;
for(i = 0; i < len; ++i)
{
if(0 == strcmp(list->head[i].name, name))
{
return i;
}
}
return -1;
}
DATATYPE* get_item_seqlist(SeqList *list, int pos)
{
if(pos < 0 || pos >= list->clen)
{
return NULL;
}
return &list->head[pos];
}
int ModifySeqList(SeqList *list, char *name, DATATYPE *newdata)
{
int ret = FindSeqList(list, name);
if(-1 == ret)
{
return -1;
}
memcpy(&list->head[ret], newdata, sizeof(DATATYPE));
return 0;
}
int ClearSeqList(SeqList *list)
{
list->clen = 0;
return 0;
}
int DestroySeqList(SeqList *list)
{
free(list->head);
free(list);
return 0;
}
int DeleteSeqList(SeqList *list, char *name)
{
int ret = FindSeqList(list, name);
if(ret < 0)
{
printf("not find");
return -1;
}
int i = ret;
for(; i < list->clen; i++)
{
memcpy(&list->head[i+1], &list->head[i], sizeof(list->head[0]));
}
list->clen--;
}
#include “seqlist.h”
#ifndef _SEQLIST_H
#define _SEQLIST_H
typedef struct person{
char name[32];
char sex;
int age;
int score;
}DATATYPE;
typedef struct list{
DATATYPE *head;
int tlen;
int clen;
}SeqList;
SeqList *CreateSeqList(int len);
int DestroySeqList(SeqList *list);//free
int ShowSeqList(SeqList *list);//show
int InsertTailSeqList(SeqList *list, DATATYPE *data);//写
int IsFullSeqList(SeqList *list);//clen ?= tlen
int IsEmptySeqList(SeqList *list);
int InsertPosSeqList(SeqList *list, DATATYPE *data, int pos);//插入
int FindSeqList(SeqList *list, char *name);//find
int ModifySeqList(SeqList *list, char *old, DATATYPE *new);//修改
int DeleteSeqList(SeqList *list, char *name);//删除某
int ClearSeqList(SeqList *list);//clear all
int get_size_seqlist(SeqList *list);//clen = ?
DATATYPE *get_item_seqlist(SeqList *list, int pos);//get head[?]
#endif
主函数
#include <stdio.h>
#include "seqlist.h"
int main()
{
DATATYPE data[] = {
{"jack", 'm', 20, 90},
{"tom", 'm', 21, 78},
{"tony", 'f', 19, 42},
{"sora", 'f', 23, 74},
{"amy", 'f', 21, 69}
};
SeqList *s1 = CreateSeqList(10);
InsertTailSeqList(s1, &data[0]);
InsertTailSeqList(s1, &data[1]);
InsertTailSeqList(s1, &data[2]);
ShowSeqList(s1);
printf("----------------------------------------\n");
InsertPosSeqList(s1, &data[4], 2);
ShowSeqList(s1);
printf("----------------------------------------\n");
int ret = FindSeqList(s1, "tony");
if(-1 == ret)
{
printf("not find\n");
}
else
{
DATATYPE *tmp = get_item_seqlist(s1, ret);
printf("%s %d\n", tmp->name, tmp->score);
}
printf("----------------------------------------\n");
ModifySeqList(s1, "lisi", &data[4]);
ShowSeqList(s1);
printf("----------------------------------------\n");
DeleteSeqList(s1, "tony");
ShowSeqList(s1);
ClearSeqList(s1);
DestroySeqList(s1);
return 0;
}
内存泄漏检测