reverse函数的作用_C reverse函数_环球视点
发布时间:2023-06-01 16:07:10
来源:互联网
(相关资料图)
1、#include #include typedef struct _node { int value; struct _node *next;} node;node * make_node(int value) { node *new_node = (node *) malloc(sizeof(node)); new_node->value = value; new_node->next = 0; return new_node;}node * add_after(node *pos, int value) { node *new_node = make_node(value); pos->next = new_node; return new_node;}void print_node_list(node *head) { while (head) { printf("%d ", head->value); head = head->next; } printf("");}void free_node_list(node *head) { node *temp; while (head) { temp = head; head = head->next; free(temp); }}node * reverse(node *head) { node *f = 0, *s = 0; while (head) { f = s; s = head; head = head->next; s->next = f; } return s;}int main() { int i = 0; node *head = make_node(i), *last = head; while (i < 10) { last = add_after(last, ++i); } print_node_list(head); head = reverse(head); print_node_list(head); free_node_list(head); return 0;}。
本文分享完毕,希望对大家有所帮助。
关键词: