数据结构day2-day3-线性表之顺序表的代码

sqlist.h



#define N 100
typedef int data_t;

typedef struct sqlist_t{
	data_t data[N];
	int last;

}sqlist,*sqlink;


sqlink list_create();
int list_clear(sqlink L);
int list_empty(sqlink L);
int list_length(sqlink L);
int list_locate(sqlink L, int value);
int list_insert(sqlink L, data_t value, int pos);
int list_show(sqlink L);
int list_free(sqlink L);
int list_delete(sqlink L, int pos);
int list_merge(sqlink L1,sqlink L2);//线性表的合并
int list_purge(sqlink L);//删除线性表的重复元素

sqlist.c

#include "sqlist.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/*
 * list_create: create a list
 * @ret: sqlink
 *
 *
 */
sqlink list_create()
{

	sqlink L;
	
	L = (sqlink)malloc(sizeof(sqlist));
	
	if(L == NULL)
	{
		printf("list malloc failed\n");
		return L;
	}

	memset(L, '\0', sizeof(sqlist));
	L->last = -1;
	return L;
}

/*
 * @ret		0-success	-1-failed
 */
int list_clear(sqlink L)
{
	if(L == NULL)
		return -1;
	
	memset(L, '\0', sizeof(sqlist));
	L->last = -1;
	return 0;
}
/*
 * list_empty:Is list empty?
 * param: L-list
 * @ret: 1-empty  0-not empty
 */

int list_empty(sqlink L)
{
	if(L->last == -1)
		return 1;
	else
		return 0;
	return 0;
}
/* list_length: caculate list's length.
 * param: L-list
 * @ret: -1-empty list (failed)  
 * @ret: list's length 
 */

int list_length(sqlink L)
{
	if(L == NULL)
		return -1;

	return (L->last+1);
}

/*
 * @ret -1--not exist   否则就是那个位置pos
 *
 *
 */
int list_locate(sqlink L, int value)
{
	int i;
	for(i = 0;i <= L->last; i++)
	{
		if(L->data[i] == value)
			return i;
	}
	
	return -1;
}
/*
 * list_insert:Insert value in specific position.
 * param:L-list 
 * 	 value-value you want
 * 	 pos-position
 * @ret: -1-full 0-success 
 *
 */
int list_insert(sqlink L, data_t value, int pos)
{
	int i;
	//full
	 if(L->last == N-1)
	 {
	 printf("list is full\n");
	 return -1;
	 }
	//check pos 0<=pos<=last+1
	 if(pos < 0 || pos > L->last+1)
	 {
		printf("Pos is invalid\n");
	 	return -1;
	 }

	//move
	for(i = L->last; i>=pos; i--)
	{
		L->data[i+1] = L->data[i];//数据一个一个往后移动
	}

	 //update value last
	L->data[pos] = value;
	L->last++;


	return 0;
}

/*
 * list_show:show the list's all value
 * param: L-list
 * @ret: -1-empty list failed
 * 	  0-success
 *
 */
int list_show(sqlink L)
{
	int i;
	if(L == NULL)
		return -1;
	if(L->last == -1)
		printf("list is empty\n");

	for(i = 0; i <= L->last; i++)
	{
	printf("%d ",L->data[i]);
	}
	puts("");
	return 0;

}
/*
 * list_free:free the list you have created
 * param: L-list
 * @ret: -1-failed 0-success
 *
 */
int list_free(sqlink L)
{
	if(L == NULL)
		return -1;

	free(L);
	L == NULL;
	return 0;
}

/*
 * list_delete: delete the element in specific position
 * @ret: -1-list empty	0-success
 * param: L-list	pos-position
 *
 *
 */
int list_delete(sqlink L, int pos)
{
	int i;
	if(L->last == -1)
	{
		printf("list is empty\n");
		return -1; 
	}
	//pos [0,last]
	if(pos < 0 || pos > L->last)
	{
		printf("delete pos is invalid\n");
		return -1;
	}
	//move [pos+1,last]
	for(i = pos+1; i<= L->last; i++)
	{
		L->data[i-1] = L->data[i];//pos那个位置就会自动被后面的值覆盖
	}
	L->last--;
	


	return 0;
}
/*
 * list_merge: 将L2的元素合并到L1 
 * param: L1-destination	L2-source
 * @ret: 0-success
 */
int list_merge(sqlink L1,sqlink L2)
{
	int i =  0;
	int ret;

	while(i <= L2->last)
	{
		ret = list_locate(L1, L2->data[i]);//L1当中是不是存在L2中i位置的元素

		if(ret == -1)
		{
			if(list_insert(L1, L2->data[i], L1->last+1) == -1)//L1空间不足,插入失败
				return -1;
		}
		i++;
	}

	return 0;
}
/*
 * list_purge: delete the same elegant in the list you give
 * param: L-list
 * @ret: 0-success
 *
 *
 */
int list_purge(sqlink L)
{
	int i;
	int j;

	if(L->last == 0)
	{
		return 0;
	}

	i=1;
	while(i <= L->last)
	{
		j = i-1;
		while(j >= 0)
		{
			if(L->data[i] == L->data[j])
			{
				list_delete(L, i);
				break;

			}
			else
			{

				j--;
			}

		}
		if(j < 0)
		{
			i++;
		}
	}

	return 0;
}

test.c

#include "sqlist.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int test_insert(void);
int test_delete(void);
int test_merge(void);
int test_purge(void);
int main(int argc, char* argvp[])
{
	test_purge();
	return 0;
}

int test_purge(void)
{

	sqlink L;

	L = list_create();
	if(L == NULL)
		return -1;

	list_insert(L, 10, 0);
	list_insert(L, 20, 0);
	list_insert(L, 30, 0);
	list_insert(L, 30, 0);
	list_insert(L, 40, 0);
	list_insert(L, 50, 0);
	list_insert(L, 60, 0);
	list_insert(L, 60, 0);
	list_show(L);
	printf("*****************************\n");
	list_purge(L);
	list_show(L);
	list_free(L);
}
int test_merge(void)
{
	sqlink L1, L2;
	L1 = list_create();
	L2 = list_create();
	
	if(L1 == NULL)
		return -1;
	if(L2 == NULL)
		return -1;

	list_insert(L1, 10, 0);
	list_insert(L1, 20, 0);
	list_insert(L1, 30, 0);
	list_insert(L1, 40, 0);
	list_insert(L1, 50, 0);
	list_insert(L1, 60, 0);
	list_show(L1);
	
	
	list_insert(L2, 10, 0);
	list_insert(L2, 20, 0);
	list_insert(L2, 30, 0);
	list_insert(L2, 40, 0);
	list_insert(L2, 70, 0);
	list_insert(L2, 80, 0);
	list_show(L2);
	
	printf("****************************\n");

	list_merge(L1, L2);
	list_show(L1);


	list_free(L2);
	list_free(L1);

}
int test_delete(void)
{
	sqlink L;

	L = list_create();
	if(L == NULL)
		return -1;

	list_insert(L, 10, 0);
	list_insert(L, 20, 0);
	list_insert(L, 30, 0);
	list_insert(L, 40, 0);
	list_insert(L, 50, 0);
	list_insert(L, 60, 0);
	list_show(L);
	list_delete(L, 3);
	list_show(L);
	list_free(L);


}
int test_insert(void)
{

	sqlink L;

	L = list_create();
	if(L == NULL)
		return -1;

	list_insert(L, 10, 0);
	list_insert(L, 20, 0);
	list_insert(L, 30, 0);
	list_insert(L, 40, 0);
	list_insert(L, 50, 0);
	list_insert(L, 60, 0);
	list_show(L);
	list_free(L);
}

申请内存之后,要给内存清零,因为动态分配(malloc)的内存里面的值是不确定的。

用函数memset(p, '\0', size);

puts("");相当于加了一个换行符

在命令行中输入:vsp sqlist.h就可以一个页面中看两个文件,CTRL+W然后方向键←→进行页面切换

在命令行中输入:8,20 d 意思是将第8行到第20行删除

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Aurora Smith

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值