LinuxC语言顺序表的实现

头文件定义结构体和功能函数


/*
typedef int date_t;
#define N 128

struct sqlist_t{
	date_t date[N];
	int last;
};

typedef struct sqlist_t sqlist;//sqlist L; struct sqlist_t L;
typedef struct sqlist_t *sqlink;//sqlink p; struct sqlist_t *p ;

上面这段是下面结构简写的原型
*/

typedef int data_t;
#define N 128

typedef struct{
	data_t data[N];
	int last;
}sqlist ,*sqlink;

sqlink list_create();//创建顺序表
int list_clear(sqlink L);//清除顺序表
int list_free(sqlink L);//清除malloc申请的空间的数据
int list_delete(sqlink L,int pos);//指定删除顺序表的数据
int list_empty(sqlink L);//判断顺序表是不是为空
int list_length(sqlink L);//求顺序表的长度
int list_local(sqlink L,data_t value);//查找顺序表里某一个值的位置
int list_insert(sqlink L,data_t value,int pos);//在顺序表中指定插入
int list_show(sqlink L);//遍历顺序表
int list_merge(sqlink L1,sqlink L2);//将L2插入顺序表L1中,重复数据会删除L2中的元素
int list_purge(sqlink L);//将顺序表中重复的元素删除

sqlist.c文件(对头文件里面的函数进行实现)

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

sqlink list_create(){
//malloc 申请内存
	sqlink L;
	L=(sqlink)malloc(sizeof(sqlist));
	if(L==NULL){
		printf("malloc failed\n");
		return NULL;
	}
	
//initialize 在把表置空
	memset(L,0,sizeof(sqlist));
	L->last=-1;
//return list
	return L;
}

/*
@ret 0=succed 1=failed
*/
int list_clear(sqlink L){
	if(L==NULL){
		return 1;
	}else{
		memset(L,0,sizeof(sqlist));
		L->last=-1;
		return 0;
	}
}

int list_free(sqlink L){
	if(L==NULL){
		return -1 ;
	}
	free(L);
	L=NULL;
	return 0;
}

//0=succeed 1=failed
int list_delete(sqlink L,int pos){
	int i;
	if(pos>L->last||pos<0){
		printf("delete is invaild\n");
		return 1;
	}
	//move
	for(i=pos+1;i<=L->last;i++){
		L->data[i-1]=L->data[i];
	}
	//update
	L->last--;
	return 0;

}

/*
@ret 0=empty 1=not empty
*/
int list_empty(sqlink L){
	if(L->last==-1){
		return 0;
	}else{
	return 1;
	}
}

/*
ret 0=succeed 1=failed
*/
int list_length(sqlink L){
	if(L==NULL){
		return 1;
	}else{
		return L->last+1;
	}
}

/*
@ret -1= not exit 0=exit
*/
int list_local(sqlink L,data_t value){
	int i;
	for(i=0;i<=L->last;i++){
		if(L->data[i]==value){
			return i;
		}
	}
	return -1;
}

int list_insert(sqlink L,data_t value,int pos){
	int i;
//full 
	if(L->last==N-1){
		printf("L is full\n");
		return -1;
	}
//check pos [0,last+1] 
	if(pos<0||pos>L->last+1){
		printf("insert invaild\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;
}
int list_show(sqlink L){
	int i;
	if(L==NULL){
		return -1;
	}
	if(L->last==-1){
		printf("show failed sqlist is empty");
		return -1;
	}
	for(i=0;i<=L->last;i++){
		printf("%d ",L->data[i]);
	}
	puts("");

	return 0;
}

int list_merge(sqlink L1,sqlink L2){
	int i=0;
	int ret;
	while(i<=L2->last){
		ret=list_local(L1,L2->data[i]);
		if(ret == -1){
			if(list_insert(L1,L2->data[i],L1->last+1)==-1){
			return -1;
			}
		}
		i++;
	}
	return 0;
}

int list_purge(sqlink L){
	int i,j;
	if(L->last==-1){
		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++;
		}
	}
}

test.c

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

void test_insert();
void test_delete();
void test_merge();
void test_purge();
int main(){
	//test_insert();
	//test_delete();
	//test_merge();
	test_purge();
	return 0;
}

void test_insert(){
	sqlink L;
	L= list_create();
	if(L==NULL){
		printf("list create failed\n");
		return ;
	}
	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_show(L);

//invaild 
	list_insert(L,40,-12);
	list_insert(L,50,250);

	list_show(L);
	list_free(L);
}

void test_delete(){
	sqlink L;
	L= list_create();
	if(L==NULL){
		printf("list create failed\n");
		return ;
	}
	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,0);
	list_show(L);
	
	list_free(L);

}
void test_merge(){
	sqlink L1,L2;
	L1= list_create();
	if(L1==NULL){
		printf("list create failed\n");
		return ;
	}
	
	L2= list_create();
	if(L2==NULL){
		printf("list create failed\n");
		return ;
	}
	
	list_insert(L1,10,0);
	list_insert(L1,20,0);
	list_insert(L1,30,0);
	list_insert(L1,40,0);
	
	list_insert(L2,30,0);
	list_insert(L2,40,0);
	list_insert(L2,50,0);
	list_insert(L2,60,0);
	
	list_show(L1);
	list_show(L2);
	printf("*************************\n");
	list_merge(L1,L2);
	
	list_show(L1);
	list_show(L2);
	
	list_free(L1);
	list_free(L2);
}

void test_purge(){
	sqlink L;
	L= list_create();
	if(L==NULL){
		printf("list create failed\n");
		return ;
	}
	list_insert(L,10,0);
	list_insert(L,20,0);
	list_insert(L,20,0);
	list_insert(L,40,0);
	list_insert(L,40,0);
	list_insert(L,40,0);

	list_show(L);
	list_purge(L);
	list_show(L);

	list_free(L);
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值