本文介绍了链接列表需要帮助......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 大家好, i需要链接列表帮助... 问题是这样,反转单链表的内容 不使用临时节点... 解决方案与代码将不胜感激...hi all,i need help with linked lists...the problem is this, "reverse the contents of a singly linked listwithout using a temporary node"...solution with code will be appreciated...推荐答案 它是'不会发生。抱歉,我们不做家庭作业。 您可以自由地询问有关您提出的解决方案的具体问题 并且似乎无法完成,我们很乐意回答任何语言 你可能会有的问题。 但你自己做作业首先。 哦,如果你真的很想得到快速简便的解决方案 而不理解:考虑使用谷歌。这个问题很老了。 S.It''s not going to happen. We don''t do homework assignments, sorry.You''re free to ask specific questions on a solution you''ve come up withand can''t seem to complete, and we''ll be happy to answer any languagequestions you might have.But do your own homework first.Oh, and if you''re that hell-bent on getting the quick and easy solutionswithout understanding: consider using Google. This problem is very old.S. 我无法想象一个临时节点将如何有用。 / * BEGIN list.h * / #ifndef H_LIST #define H_LIST typedef struct list_node { struct list_node * next; void * data; } list_type; list_type * list_make(long unsigned); list_type * list_rev(list_type *); list_type * list_sort (list_type *, int(*)(const list_type *,const list_type *)); void list_free(list_type *,void(*)(void *)) ; #endif / * END list.h * / / * BEGIN list.c * / #include< stdlib.h> #include" list.h" static long unsigned list_count(list_type *); static list_type * node_sort(list_type *,long unsigned, int(*)(const list_type *,const list_type *)); static list_type * list_merge(list_type *,list_type *, int(*)(const list_type *,const list_type *)); 静态list_type * list_split(list_type *,long unsigned); list_type * list_rev(list_type * head) { list_type * next_node,* previous; if(head!= NULL){ next_node = head - >下一个; 头 - > next = NULL; while(next_node!= NULL){ previous = next_node - >下一个; next_node - > next = head; head = next_node; next_node = previous; } } 返回头; } list_type * list_make(长无符号数) { list_type * node,* list; list = count!= 0? malloc(sizeof * list):NULL; if(list!= NULL){ node = list; while(--count != 0){ node - > data = NULL; node - > next = malloc(sizeof * node - > next); if(node - > next == NULL){ list_free(list,free); 返回NULL; } else { node = node - >下一个; } } 节点 - > data = NULL; node - > next = NULL; } 返回列表; } void list_free(list_type * node,void(* free_data)(void *)) { list_type * next_node; while(node!= NULL){ next_node = node - >下一个; free_data(节点 - >数据); 免费(节点); node = next_node; } } list_type * list_sort(list_type * head, int(* compar)(const list_type *, const list_type *)) { 返回node_sort(head,list_count(head),compar); } static long unsigned list_count(list_type * head) { 长无符号数; for( count = 0; head!= NULL; head = head - > next){ ++ count; } 返回计数; } static list_type * node_sort(list_type * head,long unsigned count, int(* compar)(const list_type *,const list_type *)) { 长无符号的一半; list_type * tail; if(count> 1){ half = count / 2; tail = list_split(head,half); tail = node_sort(tail,count - half,compar); head = node_sort (头,半,比较); head = list_merge(head,tail,compar); } 返回头部; } 静态list_type * list_split(list_type * head,long unsigned count) { list_type *尾巴; while(count--> 1){ head = head - >下一个; } tail = head - >下一个; 头 - > next = NULL; 返回尾巴; } 静态list_type * list_merge(list_type * head,list_type * tail, int(* compar)(const list_type *,const list_type *)) { list_type * list,* sorted,** node; node = compar(head,tail)> 0? & tail:& head; sorted = list = * node; * node = sorted - >下一个; while(* node!= NULL){ node = compar(head,tail)> 0? & tail:& head; sorted - > next = * node; sorted = * node; * node = sorted - >下一个; } 排序 - > next = head!= NULL? head:tail; 返回列表; } / * END list.c * / / * BEGIN list_sort.c * / #include< stdio.h> #include< stdlib.h> #include" list.h" #define NODES 17 #define LU_RAND_SEED 123456789LU #define LU_RAND(S)((S)* 69069 + 362437& 0xffffffff) struct score { 浮动文献; 漂浮历史; 浮点社会学; }; int hist_score_comp(const list_type * ,const list_type *); void score_init(list_type *,long unsigned); void score_print(list_type *); int main(void) { list_type *得分; 得分= list_make(NODES); if(得分== NULL){ fputs(得分列表未分配。\ n,stderr); 退出(EXIT_FAILURE ); } score_init(得分,LU_RAND_SEED); put(list_sort.c \ nn \ nandand order中的BEGIN输出); score_print(得分); put(按历史排序); 得分= list_sort(得分,hist_score_comp); 得分_印记(得分); put(反向排序顺序); 得分= list_rev(得分); 得分_打印(得分); puts(" list输出来自list_sort.c \ n"); list_free(得分,免费); 返回0; } void score_init(list_type * node,long unsigned seed) { while( node!= NULL){ node - > data = malloc(sizeof(struct score)); if(node - > data == NULL){ fputs(" malloc problem \\\", stderr); 退出(EXIT_FAILURE); } seed = LU_RAND(种子); (( struct score *)node - > data) - >文献 =种子%40 + 60.0f; seed = LU_RAND(种子); ((struct score *)node - >数据) - >历史 =种子%40 + 60.0f; seed = LU_RAND(种子); ((struct score *)node - >数据) - >社会学 =种子%40 + 60.0f; node = node - >下一个; } } void score_print(list_type * node) { 浮点数; while(node!= NULL){ 得分=((结构得分*)节点 - >数据) - >文学作品; printf(" literature%d",(int)得分); 得分=((结构得分*)节点 - >数据) - > ;历史; printf(" history%d",(int)得分); 得分=((结构得分*)节点 - >数据) - > ;社会学; printf(" sociology%d \ nn",(int)得分); node = node - >下一个; } putchar(''\ n''); } int hist_score_comp(const list_type * head,const list_type * tail) { float first =((struct score *)head - > data) - >历史; float second =((struct score *)tail - > data) - >历史; 返回第二个>先? -1:秒!=第一个; } / * END list_sort.c * / - peteI can''t imagine how a temporary node would be helpful./* BEGIN list.h */#ifndef H_LIST#define H_LISTtypedef struct list_node {struct list_node *next;void *data;} list_type;list_type *list_make(long unsigned);list_type *list_rev(list_type *);list_type *list_sort(list_type *,int (*)(const list_type *, const list_type *));void list_free(list_type *, void (*)(void *));#endif/* END list.h *//* BEGIN list.c */#include <stdlib.h>#include "list.h"static long unsigned list_count(list_type *);static list_type *node_sort(list_type *, long unsigned,int (*)(const list_type *, const list_type *));static list_type *list_merge(list_type *, list_type *,int (*)(const list_type *, const list_type *));static list_type *list_split(list_type *, long unsigned);list_type *list_rev(list_type *head){list_type *next_node, *previous;if (head != NULL) {next_node = head -> next;head -> next = NULL;while (next_node != NULL) {previous = next_node -> next;next_node -> next = head;head = next_node;next_node = previous;}}return head;}list_type *list_make(long unsigned count){list_type *node, *list;list = count != 0 ? malloc(sizeof *list) : NULL;if (list != NULL) {node = list;while (--count != 0) {node -> data = NULL;node -> next = malloc(sizeof *node -> next);if (node -> next == NULL) {list_free(list, free);return NULL;} else {node = node -> next;}}node -> data = NULL;node -> next = NULL;}return list;}void list_free(list_type *node, void (*free_data)(void *)){list_type *next_node;while (node != NULL) {next_node = node -> next;free_data(node -> data);free(node);node = next_node;}}list_type *list_sort(list_type *head,int (*compar)(const list_type *, const list_type *)){return node_sort(head, list_count(head), compar);}static long unsigned list_count(list_type *head){long unsigned count;for (count = 0; head != NULL; head = head -> next) {++count;}return count;}static list_type *node_sort(list_type *head, long unsigned count,int (*compar)(const list_type *, const list_type *)){long unsigned half;list_type *tail;if (count > 1) {half = count / 2;tail = list_split(head, half);tail = node_sort(tail, count - half, compar);head = node_sort(head, half, compar);head = list_merge(head, tail, compar);}return head;}static list_type *list_split(list_type *head, long unsigned count){list_type *tail;while (count-- > 1) {head = head -> next;}tail = head -> next;head -> next = NULL;return tail;}static list_type *list_merge(list_type *head, list_type *tail,int (*compar)(const list_type *, const list_type *)){list_type *list, *sorted, **node;node = compar(head, tail) > 0 ? &tail : &head;sorted = list = *node;*node = sorted -> next;while (*node != NULL) {node = compar(head, tail) > 0 ? &tail : &head;sorted -> next = *node;sorted = *node;*node = sorted -> next;}sorted -> next = head != NULL ? head : tail;return list;}/* END list.c *//* BEGIN list_sort.c */#include <stdio.h>#include <stdlib.h>#include "list.h"#define NODES 17#define LU_RAND_SEED 123456789LU#define LU_RAND(S) ((S) * 69069 + 362437 & 0xffffffff)struct score {float literature;float history;float sociology;};int hist_score_comp(const list_type *, const list_type *);void score_init(list_type *, long unsigned);void score_print(list_type *);int main(void){list_type *score;score = list_make(NODES);if (score == NULL) {fputs("The score list was not allocated.\n", stderr);exit(EXIT_FAILURE);}score_init(score, LU_RAND_SEED);puts("BEGIN output from list_sort.c\n\nRandom order");score_print(score);puts("Sorted on history");score = list_sort(score, hist_score_comp);score_print(score);puts("Reverse Sorted order");score = list_rev(score);score_print(score);puts("END output from list_sort.c\n");list_free(score, free);return 0;}void score_init(list_type *node, long unsigned seed){while (node != NULL) {node -> data = malloc(sizeof (struct score));if (node -> data == NULL) {fputs("malloc problem\n", stderr);exit(EXIT_FAILURE);}seed = LU_RAND(seed);((struct score *)node -> data) -> literature= seed % 40 + 60.0f;seed = LU_RAND(seed);((struct score *)node -> data) -> history= seed % 40 + 60.0f;seed = LU_RAND(seed);((struct score *)node -> data) -> sociology= seed % 40 + 60.0f;node = node -> next;}}void score_print(list_type *node){float score;while (node != NULL) {score = ((struct score *)node -> data) -> literature;printf("literature %d ", (int)score);score = ((struct score *)node -> data) -> history;printf("history %d ", (int)score);score = ((struct score *)node -> data) -> sociology;printf("sociology %d\n", (int)score);node = node -> next;}putchar(''\n'');}int hist_score_comp(const list_type *head, const list_type *tail){float first = ((struct score *)head -> data) -> history;float second = ((struct score *)tail -> data) -> history;return second > first ? -1 : second != first;}/* END list_sort.c */--pete 这篇关于链接列表需要帮助......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!