数据结构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;

}
相关推荐
ChoSeitaku几秒前
链表交集相关算法题|AB链表公共元素生成链表C|AB链表交集存放于A|连续子序列|相交链表求交点位置(C)
数据结构·考研·链表
秋の花2 分钟前
【JAVA基础】Java集合基础
java·开发语言·windows
偷心编程2 分钟前
双向链表专题
数据结构
香菜大丸2 分钟前
链表的归并排序
数据结构·算法·链表
jrrz08282 分钟前
LeetCode 热题100(七)【链表】(1)
数据结构·c++·算法·leetcode·链表
二十雨辰11 分钟前
[linux]docker基础
linux·运维·docker
@小博的博客30 分钟前
C++初阶学习第十弹——深入讲解vector的迭代器失效
数据结构·c++·学习
饮浊酒1 小时前
Linux操作系统 ------(3.文本编译器Vim)
linux·vim
lihuhelihu1 小时前
第3章 CentOS系统管理
linux·运维·服务器·计算机网络·ubuntu·centos·云计算
矛取矛求1 小时前
Linux系统性能调优技巧
linux