#include#include "link.h"void print_item(link p){ printf("%d\n", p->item);}int main(void){ link head, p; //struct node *head; link_init(&head); //head => NULL p = make_node(3); link_insert(&head, p); //头插法 p = make_node(5); link_insert(&head, p); //头插法 p = make_node(1); link_insert(&head, p); //头插法 p = make_node(8); link_insert(&head, p); //头插法 link_travel(&head, print_item); //遍历打印链表数值域 printf("***************\n"); p = link_search(&head, 1); if (p != NULL) { link_delete(&head, p); free_node(p); } link_travel(&head, print_item); //遍历打印链表数值域 link_destory(&head); return 0;}
#include#include #include "link.h"void link_init(link *head) //struct node **head = &head{ //head = NULL; *head = NULL;}link make_node(int item){ //link p = (link *)malloc(sizeof(struct node)); link p = (link)malloc(sizeof(*p)); p->item = item; //(*p).itme = item; p->next = NULL; //#define NULL (void *)0 return p;}void link_insert(link *head, link p) //头插法{ p->next = *head; *head = p;}link link_search(link *head, int key){ link p; for (p = *head; p != NULL; p = p->next) if (p->item == key) return p; return NULL;}void link_delete(link *head, link q){ link p; if (q == *head) { *head = q->next; return; } for (p = *head; p != NULL; p = p->next) if (p->next == q) { p->next = q->next; return; }}void free_node(link p){ free(p);}void link_modfie(link p, int key){ p->item = key;}void link_destory(link *head){ link p= *head, q; while (p != NULL) { q = p->next; free(p); p = q; } *head = NULL;}void link_travel(link *head, void (*vist)(link)){ link p; for (p = *head; p != NULL; p = p->next) vist(p);}
#ifndef _LINK_H_#define _LINK_H_typedef struct node *link;struct node { int item; link next; //struct node *next;};void link_init(link *head);link make_node(int item);void link_insert(link *head, link p);link link_search(link *head, int key);void link_delete(link *head, link p);void free_node(link p);void link_modfie(link p, int key);void link_destory(link *head);void link_travel(link *head, void (*vist)(link));#endif