头文件的编写
#ifndef __MYLIST_H__
#define __MYLIST_H__
#define N 32
//构造头文件的组成
//
//1.结构类型定义
//2.宏定义
//3.函数声明
typedef int DATA_T;
typedef struct mylist
{
DATA_T *phead;//定义首地址
int size;
int count;
}MYLIST;
//函数说明:先创建函数和销毁
//函数名:create_list
//函数功能:create list
//起名: int size :list size
//返回值:MYLIST *;
//是否成功`:MYLIST struct var pointer
//失败:NULL;
MYLIST *create_list (int size);
//函数名:destroy_list
//函数功能:destroy list
//起名:
//MYLIST **pplist; pplist 是 Plist的指针
//plist 是list 的数据指针
//int destroy_list(int size);
int destroy_list(MYLIST **pplist);
//函数名:insert_elem
//函数功能:插入一个新的数据指向plist 的指针
//起名:
//MYLIST *plist
int insert_elem(MYLIST *plist,int index,DATA_T newdata);
int print_list(MYLIST *plist);
//函数名:int del_emel
//函数功能:插入一个新的数据指针指向plist 的指针
//起名:MYLIST *plist
int delete_elem(MYLIST *plist,int index);
int serach_list(MYLIST *plist,int datax);
int change_list(MYLIST *plist,DATA_T x,int n);
int paixu_list(MYLIST *plist);
主函数
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "mylist.h"
//函数代码的实现
MYLIST *create_list(int size)
{
//实现
//开辟表结构
if(size <= 0)
{
puts("size <= 0, cannot creat list");
return NULL;
}
MYLIST *plist = NULL;
plist = (MYLIST *)malloc(sizeof(MYLIST));//不需要
if(NULL == plist)
{
//直接开辟表结构失败
puts("malloc list struct error.");
return NULL;
}
memset(plist,0,sizeof(MYLIST));//清空
plist->phead = (DATA_T *)malloc(sizeof(DATA_T)*size);
if(NULL == plist->phead)
{
//报错
//存储内存空间失败
puts("malloc storge of elem memery erro.");
free(plist);
plist = NULL;
return NULL;
}
plist->size = size;
plist->count = 0;
return plist;
}
int destroy_list(MYLIST **pplist)
{
if(NULL == pplist)
{
puts("NULL pointer error.");//空指针异常
return -1;
}
free((*pplist)->phead);
free(*pplist);
*pplist = NULL;
return 0;
}
//测试mylist.c文件
//调用
#include <stdio.h>
#include "mylist.h"
int main()
{
//开辟
MYLIST *plist = create_list(100);
if(NULL == plist)
{
puts("create list error.");
return -1;
}
//打印信息
printf("plisad->phead:%p\n",plist->phead);
printf("plisad->size:%d\n",plist->size);
printf("plisad->count:%d\n",plist->count);
//指针指向info
//释放
destroy_list(&plist);
return 0;
}