Hi everyone, I have coded a linked list that is to be reversed by directions of my instructor. I for some reason cannot get the reverse portion to print. I know it is something simple i missed. I do not get any compile errors. Could someone help me out? I really appreciate it. Thank You in advance.
Code:#include <stdio.h> #include <stdlib.h> struct node { int value; struct node *next; }; struct node * reverse(struct node * original, struct node * reversed); struct node *add_to_list_1(struct node *list, int n); void traverse(struct node * first); struct node *delete_first(struct node *first); int main() { struct node * first = NULL; struct node * reversed = NULL; first = add_to_list_1(first, 15); first = add_to_list_1(first, 13); first = add_to_list_1(first, 11); first = add_to_list_1(first, 9); first = add_to_list_1(first, 7); first = add_to_list_1(first, 5); printf("Linked List:\n"); traverse(first); return 0; first = reverse(first, reversed); printf("Linked List Reversed:\n"); traverse(first); return; } struct node * reverse(struct node * first, struct node * reversed) { struct node *p; p = first; while (p!= NULL) { reversed = add_to_list_1(reversed, p-> value);; p = delete_first(p); } return reversed; } struct node *add_to_list_1(struct node *list, int n) { struct node *new_node; new_node = malloc(sizeof (struct node)); new_node -> value = n; new_node -> next = list; return new_node; } void traverse(struct node * first) { struct node * p; p = first; while (p!= NULL) { printf("Value = %d\n", p->value); p = p->next; } printf("------------\n"); return; } struct node *delete_first(struct node *first) { struct node *p; p = first; first = first->next; free (p); return first; }



LinkBack URL
About LinkBacks



