数据结构day2(超级数组)

顺序存储的常用函数

main.c函数:

这里定义的是相当于一个 包括人名字、性别、年龄的数组。

cs 复制代码
#include <stdio.h>
#include "seqlist.h"
int main()
{
    SeqList* sl = CreateSeqList(10);

    DATATYPE data[5]={
        {"zhangsan",'m',20,70},
        {"lisi",'f',21,60},
        {"wangmazi",'m',25,80},
        {"liubei",'f',30,85},
        {"caocao",'f',40,90},
    };
    InsertTailSeqList(sl,&data[0]);
    InsertTailSeqList(sl,&data[1]);
    InsertTailSeqList(sl,&data[2]);


    ShowSeqList(sl);
    //    char find_name[50]="li1si";
    //    int ret = FindSeqList(sl,find_name);
    //    if(-1 == ret)
    //    {
    //        printf("can't find person ,%s\n",find_name);
    //    }
    //    else
    //    {
    //       DATATYPE* tmp   =  GetSeqListItem(sl,ret) ;
    //       printf("name:%s score:%d\n",tmp->name,tmp->score);
    //    }
    printf("----------------pos------------------\n");
    InsertPosSeqList(sl,&data[3],3);
    ShowSeqList(sl);
    printf("----------------modify------------------\n");
    ModifySeqList(sl,"lisi",&data[4]);
    ShowSeqList(sl);
    printf("----------------delete------------------\n");
    DeleteSeqList(sl,"wangmazi");
    ShowSeqList(sl);
    DestroySeqList(sl);
    printf("Hello World!\n");
    return 0;
}

seqlist.h文件:包括函数的声明和定义:

cs 复制代码
#ifndef SEQLIST_H
#define SEQLIST_H


typedef struct{
    char name[32];
    char sex;
    int age;
    int score;
}DATATYPE;
//typedef int Datatype;
typedef struct list {
    DATATYPE *head;
    int tlen;
    int clen;
}SeqList;

SeqList* CreateSeqList(int size);
int DestroySeqList(SeqList*sl);
int InsertTailSeqList(SeqList *list, DATATYPE *data);
int IsFullSeqList(SeqList *list);
int IsEmptySeqList(SeqList *list);
int ShowSeqList(SeqList* list);
int GetSizeSeqList(SeqList* list);
int FindSeqList(SeqList *list, char *name);
DATATYPE* GetSeqListItem(SeqList *list,int ind);
int InsertPosSeqList(SeqList *list, DATATYPE *data, int pos);
int ModifySeqList(SeqList *list, char *old, DATATYPE *newdata);
int DeleteSeqList(SeqList *list, char *name);
int ClearSeqList(SeqList *list);
#endif // SEQLIST_H

seqlist.c文件,包括创建,插入,删除,查找,修改。

cs 复制代码
#include "seqlist.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
SeqList *CreateSeqList(int size)    //开辟空间创造sl数组
{
    if(size<=0)
    {
        fprintf(stderr,"size is error,range >1");
        return NULL;
    }
    SeqList* sl = ( SeqList*)malloc(sizeof(SeqList));
    if(NULL == sl)
    {
        perror("CreateSeqList malloc");
        exit(1);
    }

    sl->head = (DATATYPE*)malloc(sizeof(DATATYPE)*size);
    if(NULL == sl->head)
    {
        perror("CreateSeqList malloc");
        exit(1);
    }

    sl->tlen = size;
    sl->clen = 0;
    return sl;
}


int DestroySeqList(SeqList *sl)      //释放sl数组
{
    if(NULL == sl)
    {
        fprintf(stderr,"SeqList point not NULL");
        return 1;
    }
    if(sl->head)
        free(sl->head);
    free(sl);
    return 0;
}

int InsertTailSeqList(SeqList *list, DATATYPE *data)  //尾插数据
{
    if(NULL == list)
    {
        fprintf(stderr,"InsertTailSeqList error,list is null\n");
        return -1;
    }

    if(IsFullSeqList(list))
    {
        fprintf(stderr,"InsertTailSeqList error ,seqlist is full\n");
        return 1;
    }
    //list->head[list->clen] = *data;
    memcpy(&list->head[list->clen] , data,sizeof(DATATYPE));
    list->clen++;
    return 0;
}

int IsFullSeqList(SeqList *list)    //判断是不是满了
{
    if(NULL == list)
    {
        fprintf(stderr,"IsFullSeqList error,list is null\n");
        return -1;
    }
    return list->clen == list->tlen;
}

int IsEmptySeqList(SeqList *list)   //判断是不是为空
{
    return 0==list->clen;
}

int ShowSeqList(SeqList *list)     //打印sl
{
    if(NULL == list)
    {
        fprintf(stderr,"list error,list is null\n");
        return -1;
    }
    int i = 0 ;
    int len = GetSizeSeqList(list);
    for(i=0;i<len;i++)
    {
        printf("name:%s sex:%c age:%d score:%d\n",list->head[i].name,list->head[i].sex,list->head[i].age
               ,list->head[i].score);
    }
    return 0;
}

int GetSizeSeqList(SeqList *list)    //获得大小sizeof
{
    if(NULL == list)
    {
        fprintf(stderr,"GetSizeSeqList error,list is null\n");
        return -1;
    }
    return list->clen;
}

int FindSeqList(SeqList *list, char *name)    //在list里面找到name
{
    if(NULL == list)
    {
        fprintf(stderr,"FindSeqList error,list is null\n");
        return 1;
    }
    if(IsEmptySeqList(list))
    {
         fprintf(stderr,"FindSeqList error,seqlist is empty\n");
        return -1;
    }
    int len = GetSizeSeqList(list);
    int i = 0 ;
    for(i=0;i<len;i++)
    {
        if(0==strcmp(list->head[i].name,name))
        {

            return i;
        }
    }
   return -1;

}

DATATYPE *GetSeqListItem(SeqList *list, int ind)   //返回第ind个元素
{
    if(NULL == list)
    {
        fprintf(stderr,"seqlist is NULL\n");
        return NULL;
    }
    if(ind<0 || ind>GetSizeSeqList(list))
    {
        fprintf(stderr,"index is error . range>0  && <size\n");
        return NULL;

    }
    return &list->head[ind];
}

int InsertPosSeqList(SeqList *list, DATATYPE *data, int pos)  //在中间插入
{

    if(NULL == list)
    {
        fprintf(stderr,"list is null\n");
        return 1;
    }
    if(IsFullSeqList(list))
    {
         fprintf(stderr,"list is full\n");
        return 1;
    }
    if(pos<0 ||pos>GetSizeSeqList(list))
    {
        fprintf(stderr,"pos is error\n");
       return 1;
    }

    int i = 0 ;

    for(i =GetSizeSeqList(list); i>=pos ; i-- )
    {
        memcpy(&list->head[i],&list->head[i-1],sizeof(DATATYPE));

    }
    memcpy(&list->head[pos],data,sizeof(DATATYPE));
    list->clen++;
    return 0;
}

int ModifySeqList(SeqList *list, char *old, DATATYPE *newdata)  //替换
{
    if(NULL == list)
    {
        fprintf(stderr,"ModifySeqList error,list is null\n");
        return 1;
    }
    int ret = FindSeqList(list,old);
    if(-1 == ret)
    {
        fprintf(stderr,"modify error,can't find\n");
        return 1;
    }
    DATATYPE* tmp = GetSeqListItem(list,ret);
    memcpy(tmp,newdata,sizeof(DATATYPE));
    return 0;
}

int DeleteSeqList(SeqList *list, char *name)   //删除
{
    if(NULL == list)
    {
        fprintf(stderr,"list is null\n");
        return 1;
    }
    if(IsFullSeqList(list))
        {
             fprintf(stderr,"list is full\n");
            return 1;
        }
    int i = 0;
    int ret = FindSeqList(list,name);
    if(ret == -1)
    {
        fprintf(stderr,"no find %s\n",name);
        return 1;
    }
    for(i = ret;i<GetSizeSeqList(list);i++)
    {
        memcpy(&list->head[i],&list->head[i+1],sizeof(DATATYPE));
    }

    list->clen--;
    return 0 ;
}

int CleanSeqList(SeqList *list)  //清零
{
    if(NULL == list)
    {
        fprintf(stderr,"CleanSeqList error,list is null\n");
        return 1;
    }
    list->clen = 0 ;
    return 0;

}
相关推荐
Heisenberg~30 分钟前
详解八大排序(五)------(计数排序,时间复杂度)
c语言·数据结构·排序算法
Abladol-aj30 分钟前
并发和并行的基础知识
java·linux·windows
JunLan~5 小时前
Rocky Linux 系统安装/部署 Docker
linux·docker·容器
方竞6 小时前
Linux空口抓包方法
linux·空口抓包
海岛日记7 小时前
centos一键卸载docker脚本
linux·docker·centos
AttackingLin8 小时前
2024强网杯--babyheap house of apple2解法
linux·开发语言·python
n***85948 小时前
嵌入式 UI 开发的开源项目推荐
windows·开源·开源软件
小袁搬码9 小时前
Windows中指定路径安装DockerDesktop
windows·docker·容器·docker desktop
学Linux的语莫9 小时前
Ansible使用简介和基础使用
linux·运维·服务器·nginx·云计算·ansible