列表函数封装,seqlist函数封装

发布于:2023-01-13 ⋅ 阅读:(460) ⋅ 点赞:(0)

过程:

seqlist.h文件

*==z00=============================================
 *   文件名称:seqlist.h
 *   创 建 者:     
 *   创建日期:2022年08月17日
 *   描    述:
 ================================================*/
#ifndef _SEQLIST_H_
#define _SEQLIST_H_
#include <stdbool.h>
#define MAXSIZE 100
typedef int data_t;
typedef  struct List{
    data_t data [MAXSIZE];
    int last;

}sqlist_t;
//creat and initialize
sqlist_t *sqlist_create();
void sqlist_init(sqlist_t **L);
//clear or destroy
void sqlist_clear(sqlist_t *L);
//length
int get_length(sqlist_t *L);
//empty
bool is_empty(sqlist_t *L);
//full
bool is_full(sqlist_t *L);
//display
void sqlist_show(sqlist_t *L);
//insert
int sqlist_insert(sqlist_t *L,data_t x,int pos);
//delete
int sqlist_delete(sqlist_t *L,int pos);
//search
int sqlist_search_by_pos(sqlist_t *L,int pos);
int sqlist_search_by_data(sqlist_t *L,data_t x);
//change
int sqlist_change_by_pos(sqlist_t *L,data_t x,int pos);
int sqlist_change_by_data(sqlist_t *L,data_t oval,data_t nval);
#endif
main.c

/*===============================================
 *   文件名称:main.c
 *   创 建 者:     
 *   创建日期:2022年08月17日
 *   描    述:
 ================================================*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "seqlist.h"

int main(int argc, char *argv[])

#if 1
    sqlist_t *L=sqlist_create();
    printf("%p\n",L);
#else
    sqlist_t *L=NULL;
    sqlist_init(&L);
    printf("%p\n",L);
#endif

    sqlist_clear(L);
    if(is_empty(L)) 
        printf("yes\n");
    else
        printf("no\n");

      int i=0;
      while (i<10){
      sqlist_insert(L,i+1,i);
      i++;
      }
      sqlist_insert(L,56,4);
      sqlist_show(L);
      sqlist_delete(L,5);
      sqlist_show(L);
      printf("sqlist_search_by_pos(L,5)所查位置的值是:%d\n",sqlist_search_by_pos(L,5));
      sqlist_change_by_pos(L,404,8);
      sqlist_show(L);
      sqlist_change_by_data(L,404,401);
      sqlist_show(L);
}

seqlist.c

#include <stdio.h>
#include <stdlib.h>
#include  <unistd.h>
#include <string.h>
#include  "seqlist.h"
//create
sqlist_t *sqlist_create()
{
    sqlist_t *list=(sqlist_t*)malloc(sizeof(sqlist_t));
    if(list == NULL)
    {   
        perror("malloc");
        return NULL;
    }
    printf("create success\n");
    memset(list,0,sizeof(sqlist_t));
    list->last=-1;
    return list;
}

//initialize
void sqlist_init(sqlist_t **L)
{
    *L=(sqlist_t*)malloc(sizeof(sqlist_t));
    if(*L==NULL)
        return;
    printf("success\n");
    (*L)->last=-1;
}

//clear
void sqlist_clear(sqlist_t *L)
{
    if(L==NULL)

    {
        printf("sqlist not exist\n");
        return;
    }
    L->last=-1;
}
//length
int get_length(sqlist_t *L)
{
    if(L==NULL)
    {
        printf("sqlist not exist\n");
        return -1;
    }
    return L->last+1;
}

//empty
bool is_empty(sqlist_t *L)
{
    return L->last==-1?true:false;
}
//full
bool is_full(sqlist_t *L)
{
    return (L->last==(MAXSIZE-1));
}

//show
void sqlist_show(sqlist_t *L)
{
    if(L==NULL)
        return;
    int i;
    for (i=0;i<=L->last;i++)
        printf("data[%d]=%d\n",i,L->data[i]);
    printf("----------------------------------\n");
}
//insert
int sqlist_insert(sqlist_t *L,data_t x,int pos)
{
    if(L==NULL)
    {
        printf("sqlist not exist\n");
        return -1;
    }
    if(is_full(L)||pos>L->last+1||pos<0)
    {
       printf("sqlist can not insert\n");
       return -1;
    }
    int i;
    for(i=L->last;i>=pos;i--)

     L->data[i+1]=L->data[i];
     L->data[pos]=x;
     L->last++;
     return 1;
     }

//delete
int sqlist_delete(sqlist_t *L,int pos)
{
  if(L==NULL)
  {
    return -1;
  }
  if(is_empty(L)||pos<0||pos>L->last)
  {
   printf("sqlist can not delete\n");
   return -1;
  }
  int i;
  for(i=pos;i<L->last;i++)
  L->data[i]=L->data[i+1];
  L->last--;
  return 0;
}

//search
int sqlist_search_by_pos(sqlist_t *L,int pos)
{
  if(L==NULL)
      return -1;
 if(is_empty(L)||pos<0||pos>L->last)
     return -1;
 int i;
for(i=0;i<=L->last;i++)
   if(i==pos){
    L->data[pos]=L->data[i];
   }
return L->data[pos];
}

//change
int sqlist_change_by_pos(sqlist_t *L,data_t x,int pos)
{ if(L==NULL)
    return -1;
    if(is_empty(L)||pos<0||pos>L->last)
        return -1;
    L->data[pos]=x;

}

int sqlist_change_by_data(sqlist_t*L,data_t oval,data_t nval)
{
    if(L==NULL)
        return -1;
    if(is_empty(L))
        return -1;
    int i,flag=0;
    for(i=0;i<L->last+1;i++)
    {
     if(L->data[i]==oval){
        L->data[i]=nval;
        flag=1;
     }

    }
     return flag;
}
 

结果:create success
0x55be5a5cc2a0
yes
data[0]=1
data[1]=2
data[2]=3
data[3]=4
data[4]=56
data[5]=5
data[6]=6
data[7]=7
data[8]=8
data[9]=9
data[10]=10
----------------------------------
data[0]=1
data[1]=2
data[2]=3
data[3]=4
data[4]=56
data[5]=6
data[6]=7
data[7]=8
data[8]=9
data[9]=10
----------------------------------
sqlist_search_by_pos(L,5)所查位置的值是:6
data[0]=1
data[1]=2
data[2]=3
data[3]=4
data[4]=56
data[5]=6
data[6]=7
data[7]=8
data[8]=404
data[9]=10
----------------------------------
data[0]=1
data[1]=2
data[2]=3
data[3]=4
data[4]=56
data[5]=6
data[6]=7
data[7]=8
data[8]=401
data[9]=10
----------------------------------