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