首页资源分类嵌入式系统 > 链表的源代码

链表的源代码

已有 445110个资源

下载专区

文档信息举报收藏

标    签:链表的实现

分    享:

文档简介

链表的实现源代码,包括链表的插入删除修改等

文档预览

#include #include typedef struct node { int item; struct node *next; }node; node *head = NULL; node *mk_node(int item) { node *p = (node *)malloc(sizeof(node)); if(p == NULL) { printf("malloc fail"); } p -> item = item; p -> next = NULL; return p; } void insert_node(node *p) { p -> next = head; head = p; } void traverse() { node *p = head; while(p != NULL) { printf("%d ",p -> item); p = p -> next; } printf("\n"); } node *find_item(int item) { node *p = head; while(p != NULL) { if(p -> item == item) { return p; } p = p -> next; } return NULL; } void delete_node(node *p) { node *pre = head; if(pre == p) { head = head -> next; } while(pre != NULL) { if(pre -> next == p) { pre -> next = p -> next; } pre = pre -> next; } } void free_node(node *p) { free(p); } void destory() { node *p; while(head != NULL) { p = head; head = head -> next; free_node(p); } } void reverse() { node *nhead = NULL; node *p; while(head != NULL) { p = head; head = head -> next; p -> next = nhead; nhead = p; } head = nhead; } main() { node *p = mk_node(1); insert_node(p); traverse(); p = mk_node(2); insert_node(p); traverse(); p = mk_node(3); insert_node(p); reverse(); traverse(); p = find_item(1); if(p != NULL) { printf("find %d\n",p -> item); }else { printf("not find"); } delete_node(p); free_node(p); traverse(); destory(); traverse(); }

Top_arrow
回到顶部
EEWORLD下载中心所有资源均来自网友分享,如有侵权,请发送举报邮件到客服邮箱bbs_service@eeworld.com.cn 或通过站内短信息或QQ:273568022联系管理员 高进,我们会尽快处理。