#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
typedef long TYPE;
typedef int (*Compare)(void * ,void *);
typedef struct node{
void *data;
struct node *next;
}Nnode,*Link;
Compare com ;
/*
*Fun Name: InitLink
*Fun Could: Create a Link
*Input Num: none
*Output Num: L
*/
Link InitLink()
{
Link L;
L = (Link)malloc(sizeof(Nnode));
L->next = NULL;
return L;
}
/*
*Fun Name: InsertL
*Fun Could: Insert a Link
*Input Num: L,e
*Output Num: L
*/
Link InsertL(Link L, void *e)
{
Link s,r;
r = L;
while(r->next != NULL)
r = r->next;
s = (Link)malloc(sizeof(Nnode));
s->data = e;
r->next = s;
s->next = NULL;
return L;
}
int compare(void *a, void *b)
{
if(*((TYPE *)a) == *((TYPE *)b))
return 0;
else
return -1;
}
/*
*
*
*/
Link Seach_List(Link L, Compare c, void *value)
{
Link r;
r = L->next;
com = c;
while(r != NULL){
if((com(r->data, value)) == 0)
break;
r = r->next;
}
return r;
}
/*
*Fun Name: DeleteL
*Fun Could: Delete a jiedian
*Input Num: L,i
*Output Num: L
*/
Link DeleteL(Link L, int i)
{
Link r,p;
int i1,i2;
r = p = L;
i1 = i2 = i;
while(i1 != 1){
p = p->next;
i1--;
}
while(i2 != 0){
r = r->next;
i2--;
}
p->next = r->next;
free(r);
return L;
}
/*
*Test
*/
void test_one()
{
Link L,r;
int i;
TYPE value = 54;
TYPE temp[15];
memset(temp, 0, 15);
for (i = 0; i < 10; i++)
temp[i] = (i+48);
L = InitLink();
for(i = 0;i < 10; i++){
L = InsertL(L, (temp+i));
}
r = L->next;
while(r != NULL){
printf("%ld\n", *((TYPE *)r->data));
r = r->next;
}
r = Seach_List(L, compare, &value);
printf("seach :%ld\n", *(TYPE *)r->data);
}
void test_str()
{
Link L, r;
int i;
char *str = "peng";
char *a[5]={
"lan",
"peng",
"shell",
"linux",
"bash"
};
L = InitLink();
for(i = 0; i < 5; i++)
L = InsertL(L, a[i]);
r = L->next;
while(r != NULL){
printf("%s\n", (char *)r->data);
r = r->next;
}
r = Seach_List(L, strcmp, str);
printf("seach :%s\n", (char *)r->data);
}
int main()
{
test_one();
test_str();
return 0;
}
|