struct.h
#ifndef _STRUCT_H
#define _STRUCT_H
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct infonode{
int data;
struct infonode *next;
}info, *pinfo;
typedef struct search_node{
pinfo prev;
pinfo cur;
}searchnode, *psear;
searchnode sear;
void init(pinfo *head);
void insert(pinfo head, int data);
bool search(pinfo head,int data);
bool delete(pinfo head, int data);
bool is_empty(pinfo head);
void show(pinfo head);
#endif
func.c
#include"struct.h"
//初始化
void init(pinfo *head)
{
(*head) = (pinfo)malloc(sizeof(info));
if(*head == NULL)
{
perror("init malloc falied");
exit(1);
}
(*head)->next = NULL;
}
//插入
void insert(pinfo head, int data)
{
pinfo new = (pinfo)malloc(sizeof(info));
if(new == NULL)
{
perror("insert malloc");
return ;
}
new->data = data;
new->next= head->next;
head->next = new;
}
//查询
bool search(pinfo head,int data)
{
if(is_empty(head))
{
printf("not any data in the link\n");
return false;
}
for(sear.prev = head,sear.cur= head->next; sear.cur != NULL; sear.prev =
sear.cur, sear.cur = sear.cur->next)
{
if(sear.cur->data == data)
return true;
}
return false;
}
//删除
bool delete(pinfo head, int data)
{
bool result = search(head, data);
if(result == false)
return false;
sear.prev->next = sear.cur->next;
return true;
}
//判空
bool is_empty(pinfo head)
{
if(head->next == NULL)
return true;
else
return false;
}
//遍历
void show(pinfo head)
{
if(is_empty(head))
{
printf("not data\n");
return ;
} while(head->next != NULL) { head = head->next; printf("%d ", head->data); } printf("\n"); }
main.c
#include"struct.h"
int main(void)
{
int a[10] = {12,123,213,234,12,123,14,133432,12};
int i;
pinfo head;
init(&head);
for(i = 0; i < 10 ; i++)
insert(head, a[i]);
printf("the first add:");
show(head);
if(search(head, 123))
printf("find the data\n");
else
printf("not find data\n");
delete(head,213);
printf("************\nafter delete the 213:");
show(head);
return 0;
}