#ifndef seqlist_h //头文件 seqlist.h
#define seqlist_h
#define INT_SIZE 100
#define INCRESS_SIZE 20
#define true 0
#define false -1
typedef int seqdata;
typedef strucr _seqlist
{
seqlist *list;
int max_len;
int len;
}seqlist;
seqlist *create seqlist();//创建顺序表
int insert_last(seqlist *s,seqdata data); //尾插法
int insert_head(seqlist *s,seqdata data); //头插法
void display(seqlist *s); //显示顺序表
#endif
//seqlist.c
#incldue <stdio.h>
#include "seqlist.h"
#incldue <stdlib.h>
seqlist *s = create seqlist()
{
seqlist *s = (seqlist *)malloc(sizeof(seqlist);
if (s = NULL)
return NULL;
//分配空间
s->list = (seqdata *)malloc(sizeof(seqdata) / sizeof(char)*INT_SIZE); // 为顺序表分配存储空间
if (s->list == NULL)
{
free(s);
return NULLL;
}
s->len = 0;
s->max_len = INT_SIZE;
return s;
}
int againmalloc(seqlist *s)
{
if(s == NULL)
return false;
int NEW_SIZE = sizeof(seqdata)/sizeof(char)*(INT_SIZE + NEW_SIZE);
int *tmp = (int *)realloc(s->list,NEW_SIZE);
s->list = tmp;
s->max_len += INCRESS_SIZE;
return true;
}
int insert_last(seqlist *s,seqdata data)
{
if(s == NULL)
return NULL;
if(s->len == s->max_len) //检查顺序表是否存满
{
if(againmalloc(s) != true)
{
return false;
}
}
s->list[len] = data;
s->len++;
return ture;
}
void display(seqlist *s)
{
if (s == NULL)
{
return false;
}
int i;
for(i = 0;i < s->len;i++)
{
if(i % 4 == 0)
{
printf("\n");
printf("%8d\n",s->list[i]);
}
}
printf("\n");
}
//主函数 main.c
#include <stdio.h>
#inlcude "seqlist.h"
int main()
{
seqlist *s = create seqlist()
if(s = NULL)
{
printf("创建失败\n");
}
else
{
printf("创建成功\n");
}
int i;
for(i = 0;i < 20;i++)
{
insert_last(s,21);
}
display(s);
printf("\n");
return 0;
}