seqlist.h
#ifndef __SEQLIST_H__
#define __SEQLIST_H__
#define MAXSIZE 100
typedef int data_t;
typedef struct{
data_t data[MAXSIZE];
int last;
}seqlist_t;
seqlist_t *create_seqlist(void);
void clear_seqlist(seqlist_t *L);
int is_empty_seqlist(seqlist_t *L);
int is_full_seqlist(seqlist_t *L);
void set_empty_seqlist(seqlist_t *L);
int get_length_seqlist(seqlist_t *L);
void show_seqlist(seqlist_t *L);
int insert_seqlist(seqlist_t *L,data_t x,int pos);
int delete_seqlist(seqlist_t *L,int pos);
int change_seqlist(seqlist_t *L,data_t x,int pos);
int search_seqlist(seqlist_t *L,data_t x);
#endif
seqlist.c
#include <stdio.h>
#include <stdlib.h>
#include "seqlist.h"
seqlist_t *create_seqlist(void)
{
seqlist_t *L=NULL;
L=(seqlist_t *)malloc(sizeof(seqlist_t));
if(L == NULL)
{
puts("malloc no memory!");
return NULL;
}
L->last = -1;
return L;
}
void clear_seqlist(seqlist_t *L)
{
if(L == NULL)
{
puts("seqlist_t *L is NULL");
return ;
}
free(L);
L = NULL;
return ;
}
int is_empty_seqlist(seqlist_t *L)
{
if(L == NULL)
{
puts("seqlist_t *L is NULL");
return -1;
}
return (L->last == -1);
}
int is_full_seqlist(seqlist_t *L)
{
if(L == NULL)
{
puts("seqlist_t *L is NULL");
return -1;
}
return (L->last == MAXSIZE-1);
}
void set_empty_seqlist(seqlist_t *L)
{
if(L == NULL)
{
puts("seqlist_t *L is NULL");
return ;
}
L->last = -1;
return ;
}
int get_length_seqlist(seqlist_t *L)
{
if(L == NULL)
{
puts("seqlist_t *L is NULL");
return -1;
}
return (L->last+1);
}
void show_seqlist(seqlist_t *L)
{
int i=0;
if(L == NULL)
{
puts("seqlist_t *L is NULL");
return ;
}
for(i=0;i<=L->last;i++)
{
printf("L->data[%d] = %d\n",i,L->data[i]);
}
return ;
}
int insert_seqlist(seqlist_t *L,data_t x,int pos)
{
int i=0;
if(is_full_seqlist(L) || pos<0 || pos >L->last+1)
{
puts("input argv is invalid");
return -1;
}
for(i=L->last;i>=pos;i--)
{
L->data[i+1] = L->data[i];
}
L->data[pos] = x;
L->last++;
return 0;
}
int delete_seqlist(seqlist_t *L,int pos)
{
int i=0;
if(pos<0 || pos>L->last)
{
puts("input pos is invalid");
return -1;
}
for(i=pos;i<L->last;i++)
{
L->data[i] = L->data[i+1];
}
L->last--;
return 0;
}
int change_seqlist(seqlist_t *L,data_t x,int pos)
{
if(pos<0 || pos>L->last)
{
puts("input pos is invalid");
return -1;
}
L->data[pos] = x;
return 0;
}
int search_seqlist(seqlist_t *L,data_t x)
{
int i=0;
if(L == NULL)
{
puts("seqlist_t *L is NULL");
return -1;
}
for(i=0;i<get_length_seqlist(L);i++)
{
if(L->data[i] == x)
return i;
}
return -1;
}
main.c
#include <stdio.h>
#include <stdlib.h>
#include "seqlist.h"
int main(int argc, const char *argv[])
{
int i=0;
seqlist_t *L=NULL;
L=create_seqlist();
if(is_empty_seqlist(L))printf("seqlist L is empty\n");
printf("last=%d\n",L->last);
for(i=0;i<5;i++)
{
insert_seqlist(L,i,i);
}
printf("last=%d\n",L->last);
printf("seqlist L length is %d\n",get_length_seqlist(L));
show_seqlist(L);
puts("==========yy================================");
printf("3 in seqlist L is %d\n",search_seqlist(L,3));
delete_seqlist(L,search_seqlist(L,3));
printf("seqlist L length is %d\n",get_length_seqlist(L));
show_seqlist(L);
puts("==========yy================================");
change_seqlist(L,8,search_seqlist(L,4));
show_seqlist(L);
puts("==========yy================================");
set_empty_seqlist(L);
show_seqlist(L);
clear_seqlist(L);
return 0;
}
输出结果