标签:
#include <stdio.h> #include <stdlib.h> typedef struct node { int num; struct node* next; }node; node *head = NULL; node *make_node(int data) { node *p = (node *)malloc(sizeof(node)); if(p == NULL) { return NULL; } else { p -> num = data; p -> next = NULL; } return p; } void insert_node_head(node *p) { p -> next = head; head = p; } void insert_node_tail(node *p) { node *p1 = head; node *end = NULL; if(head == NULL) { head = p; } else { while(p1 -> next != NULL) { p1 = p1 -> next; } p1 -> next = p; p1 = p; p -> next = NULL; } } void free_node(node *p) { free(p); } void traverse() { node *p = head; if( p == NULL) { printf("no num/n"); } while(p != NULL) { printf("%d ",p -> num); p = p -> next; } printf("\n"); } node *find(int n) { node *p = head; while(p != NULL) { if(n == p -> num) { return p; } p = p -> next; } return NULL; } void rm_node(node *p) { if(p == head) { head = head -> next; p -> next = NULL; return; } else { node *pre = head; while(pre != NULL) { if(pre -> next == p) { pre -> next = p -> next ; p -> next = NULL; return ; } pre = pre -> next; } } } void destory() { node *p = head ; while(head != NULL) { head = head -> next; free_node(p); p = head; } } int main() { node *p = make_node(1); insert_node_tail(p); p = make_node(3); insert_node_tail(p); p = make_node(5); insert_node_tail(p); p = make_node(7); insert_node_tail(p); traverse(); int a = 3; p = find(3); if(p == NULL) { printf("can not find num3\n"); } else { printf("%d\n",p -> num); } destory(); traverse(); }
标签:
原文地址:http://www.cnblogs.com/glllvwcc/p/4925827.html