如何用数据结构来实现顺序表的创建,删除,插入,排序,查询,释放

头文件的编写

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值