读书人

关于单链表的思忖

发布时间: 2013-04-20 19:43:01 作者: rapoo

关于单链表的思考

?

?no_delete_node


关于单链表的思忖
?

?

?

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137link2.c??#include?<stdio.h>??struct?node{????char?element;?????struct?node?*next;};??struct?node?*?create_node(char?ch);struct?node?*?delete_node(struct?node?*node,?char?ch);struct?node?*?delete_node4(struct?node?*node,?char?ch);struct?node?*?search_node(struct?node?*node,?char?ch);struct?node?*?insert?(struct?node?*node,?char?ch);??void?list_link(struct?node??*node);??void?main(void){????struct?node?*c_node?=?malloc(sizeof(*c_node));??????????struct?node?*node?=?create_node('a');?????struct?node?*node2?=?node;?????printf("node->element=%c\n",?node->element);????node?=?insert(node,?'b');????printf("node->element=%c\n",?node->element);????node?=?insert(node,?'c');????printf("node->element=%c\n",?node->element);????node?=?insert(node,?'d');????printf("node->element=%c\n",?node->element);????node?=?insert(node,?'e');??????//found_node?=?search_node(node,?'b');????//printf("found_node->element=%c\n",?found_node->element);????puts("before\n");????list_link(node);????puts("del\n");????c_node?=?delete_node4(node,?'b');????puts("after?c_node\n");????list_link(c_node);????puts("after?list?node\n");????list_link(node);????puts("after?list?node2\n");????list_link(node2);????//printf("%d\n",?node.next);}??void?list_link(struct?node??*node){????printf("%c\n",?node->element);????struct?node?*head?=?node;????while(head->next?!=?0){????????head?=?head?->next;????????????printf("%c\n",?head?->element);????}}??struct?node?*?insert(struct?node?*head,?char?ch){????struct?node?*node1?=?create_node(ch);????node1->next?=?head;????return?node1;}??struct?node?*?create_node(char?ch){????struct?node?*head?=?(struct?node?*)malloc(sizeof(struct?node));????head->element?=?ch;????head->next?=?NULL;????return?head;?}??struct?node?*?search_node(struct?node?*head,?char?ch){????struct?node?*pre?=?head;????if(pre?==?NULL){????????return?NULL;?????}else{????????while(pre?!=?NULL){????????????if(pre?->?element?==?ch){????????????????printf("found?pre->element=%c\n",?pre?->element);????????????????return?pre;????????????}????????????pre?=?pre->next;????????}????????return?NULL;????}}?????struct?node?*?delete_node(struct?node?*head,?char?ch){????printf("delete_node?head->element=%c\n",?head->element);????struct?node?*ret?=?head;????struct?node?*pre?=?head;????if(pre?==?NULL){????????return?ret;?????}else{????????if(pre->element?==?ch){????????????pre?=?pre?->next;????????????return?ret;????????}????????while(pre->next?!=?NULL){????????????printf("-----?%c\n",?pre?->element);????????????if(pre->next->?element?==?ch){????????????????printf("found?del?%c\n",?pre?->element);????????????????struct?node?*del?=?pre->next;????????????????free(del);????????????????pre->next?=?pre?->next->next;????????????????}else{????????????????printf("notfound?del%c\n",?pre?->element);????????????}????????????pre?=?pre->next;????????}????}????return?ret;}???????struct?node?*?delete_node4(struct?node?*head,?char?ch)????????????{??printf("delete_node4?head->data=%d\n",?ch);?????????????????struct?node?*ret?=?head;??struct?node?*pre?=?head;??????????????????????????????????????????????while(NULL?!=?pre->next)??{??????if(pre->next->element?==?ch)??????{??????????printf("found?del?%d\n",?pre->next->element);??????????pre->next?=?pre?->next->next;?????????????????????}??????else?pre?=?pre->next;??}??return?ret;}?

?

读书人网 >编程

热点推荐