实现线性顺序表的增删查改功能

实现线性顺序表的增删查改功能,主要有main.h  main.c list.c  三个模块函数实现。
#ifndef _MAIN_H_
#define _MAIN_H_
#define SIZE 500
typedef int data_t;

typedef struct
{
	data_t data[SIZE];
	int count;
}LIST; 
enum e_List
{
	ERROR = -1,
	OK
};
enum e_isListFull
{
	FALSE = 0,
	TRUE
};



LIST * createList();

int insertList(LIST * List, data_t Data, int offset);

int updateList(LIST * List, data_t Data, int offset);

void destroyList(LIST * List);

int deleteList(LIST * List, data_t  * Data, int offset);

int updateList(LIST * List, data_t   Data, int offset);

void showList(LIST * List);

int isListFull(LIST * List);

#endif

#include "main.h"
#include <stdio.h>


int main(void)
{
	LIST * List = NULL;
	int  Data = 0;
	List = createList();
	// 插入数据
	insertList(List, 1, 0);
	insertList(List, 2, 0);
	insertList(List, 3, 0);
	insertList(List, 4, 0);
	insertList(List, 5, 0);
	insertList(List, 8, 4);
	insertList(List, 5, -1);
	insertList(List, 8, 5);
	insertList(List, 8, 6);
	printf("%d\n",List->count);
	showList(List);
	
	//更新数据
	if (OK == updateList(List, 100, 3))
	{
		showList(List);
	}
	if (OK == updateList(List, 100, 5))
	{
		showList(List);
	}
	if (OK == updateList(List, -100, 0))
	{
		showList(List);
	}
	
	//删除数据
	if (OK == deleteList(List, &Data, 0))
	{
		showList(List);
		printf("%d\n",Data);
	}
	if (OK == deleteList(List, &Data, 0))
	{
		showList(List);
		printf("%d\n",Data);
	}
	if (OK == deleteList(List, &Data, 4))
	{
		showList(List);
		printf("%d\n",Data);
	}
	if (OK == deleteList(List, &Data, -1))
	{
		showList(List);
		printf("%d\n",Data);
	}
	

	destroyList(List);
	List = NULL;
	return 0;
}


/*
	易错点: 1, List->count 容易写成List.count
	        2, 在offset位置处插入和删除数据的时候,容易越界出现段错误
*/
#include "main.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

LIST * createList()
{
	LIST * List = NULL;
	List = (LIST *)malloc(SIZE * sizeof(LIST));
	
	if (NULL == List)
	{
		printf("create List error!\n");
		return ;
	}
	memset(List, 0, SIZE * sizeof(LIST));
	List->count = 0;
	//List->data[0] = 0;
	return List;
}
void destroyList(LIST * List)
{
	if (NULL == List)
	{
		printf("List is empty!\n");
		return ;
	}
	free(List);
	List = NULL;
}
int isListFull(LIST * List)
{
	if (NULL == List)
	{
		printf("List is empty!\n");
		return ERROR;
	}
	if (List->count == SIZE - 1)
	{
		printf("List if full!\n");
		return TRUE;
	}
	return FALSE;
}
int insertList(LIST * List, data_t Data, int offset)
{
	if (NULL == List 
	|| offset < 0 
	|| offset > List->count)
	{
		printf("Parameter is error!\n");
		return ERROR;
	}
	if (isListFull(List) == FALSE)
	{
		int i = 0;
		for (i = List->count; i >= offset; i--)
		{
			List->data[i+1] =  List->data[i];
		}
		List->data[offset] = Data;
	}
	List->count++;
	return OK;
}
void showList(LIST * List)
{
	if (NULL == List)
	{
		printf("List is empty!\n");
		return ;
	}
	int i = 0;
	for (i = 0; i < List->count; i++)
	{
		printf("%d ",List->data[i]);
	}
	puts("");
	//return TRUE;
}
int deleteList(LIST * List, data_t *  Data, int offset)
{
	if (NULL == List 
	|| offset < 0
	|| offset >= List->count)
	{
		printf("Parameter is ERROR!\n");
		return ERROR;
	}
	*Data = (List->data[offset]);
	int i = offset;
	for (i = offset; i < List->count - 1; i++)
	{
		List->data[i] = List->data[i+1];
	}
	List->count--;
	return OK;
}
int updateList(LIST * List, data_t   Data, int offset)
{
	if (NULL == List
	|| offset < 0
	|| offset >= List->count)
	{
		printf("Parameter is wrong!\n");
		return ERROR;
	}
	List->data[offset] = Data;
	return OK;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值