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

循环链表的源代码

已有 445109个资源

下载专区

文档信息举报收藏

标    签:循环链表的源代码

分    享:

文档简介

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

文档预览

#include #include typedef struct node { int item; struct node *next; }node; node *head = NULL; void init_link() { static node sentinel = {0,NULL}; head = &sentinel; head -> next = head; } node *mk_node(int item) { node *p = (node*)malloc(sizeof(node)); if(p == NULL ) { printf("malloc fail"); return NULL; } p -> item = item; p -> next = NULL; return p; } void insert_node(node *p) { p -> next = head -> next; head -> next = p; } void traverse() { node *p = head -> next; while(p != head) { printf("%d ",p -> item); p = p -> next; } printf("\n"); } node *find_node(int item) { node *p = head -> next; while(p != head) { if(p -> item == item) return p; p = p -> next; } return NULL; } void free_node(node *p) { free(p); } int delete_node(node *p) { if(p == NULL) { return 0; } node *pre = head; while(pre -> next != head) { if(pre ->next == p) { pre -> next = p -> next; return 1; } pre = pre -> next; } } void destory() { node *p = head -> next; while(p != head) { head -> next = p -> next; free(p); p = p -> next; } } int main(int argc,char *argv[]) { int i = 1; init_link(); node *p; for(i = 1; i < argc; i ++) { p = mk_node(atoi(argv[i])); insert_node(p); } /* init_link(); node *p = mk_node(1); insert_node(p); traverse(); p = mk_node(2); insert_node(p); traverse();*/ p = find_node(2); if(p != NULL) { printf("%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联系管理员 高进,我们会尽快处理。