#include
#include
struct node {
int value;
struct node *next;
};
void add_to_tail(struct node **head, int value)
{
struct node *temp, *i;
temp = malloc(sizeof(struct node));
temp->value = value;
temp->next = NULL;
/* list empty */
if (*head == NULL) {
*head = temp;
return;
}
/* find the tail node */
for( i = *head ; i->next != NULL ; i = i->next);
i->next = temp;
}
void print_list(struct node *head)
{
struct node *i = head;
while (i != NULL) {
printf("%d\n", i->value);
i = i->next;
}
}
void reverse_list(struct node **head)
{
struct node *prev, *curr, *next;
prev = NULL;
curr = *head;
while (curr != NULL) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
*head = prev;
}
int main(void)
{
struct node *head = NULL;
add_to_tail(&head, 1);
add_to_tail(&head, 2);
add_to_tail(&head, 3);
add_to_tail(&head, 4);
print_list(head);
reverse_list(&head);
print_list(head);
}
#include
struct node {
int value;
struct node *next;
};
void add_to_tail(struct node **head, int value)
{
struct node *temp, *i;
temp = malloc(sizeof(struct node));
temp->value = value;
temp->next = NULL;
/* list empty */
if (*head == NULL) {
*head = temp;
return;
}
/* find the tail node */
for( i = *head ; i->next != NULL ; i = i->next);
i->next = temp;
}
void print_list(struct node *head)
{
struct node *i = head;
while (i != NULL) {
printf("%d\n", i->value);
i = i->next;
}
}
void reverse_list(struct node **head)
{
struct node *prev, *curr, *next;
prev = NULL;
curr = *head;
while (curr != NULL) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
*head = prev;
}
int main(void)
{
struct node *head = NULL;
add_to_tail(&head, 1);
add_to_tail(&head, 2);
add_to_tail(&head, 3);
add_to_tail(&head, 4);
print_list(head);
reverse_list(&head);
print_list(head);
}