int main() { for (size_t i: {0, 1, 2, 4, 5, 9, 10}) { std::cout << "Test case i = " << i << ":" << std::endl; auto root = create(i); print(root); root = pairwise_swap(root); print(root); destroy(root); std::cout << std::endl; } return 0; }
void main() { struct listnode *head = NULL; add(&head, getnode(1)); add(&head, getnode(2)); add(&head, getnode(3)); add(&head, getnode(4)); add(&head, getnode(5)); add(&head, getnode(6)); print_list(head); reverse(&head); print_list(head); head = pairwise_swap(head); print_list(head); head = pairwise_swap(head); print_list(head); rotate(&head, 3); print_list(head); segregate_even_odd(&head); print_list(head); int a = add_digit(head, 9); print_list(head); }
struct listnode * pairwise_swap(struct listnode *head) { // never forget to add error cases if(head == NULL || head->next == NULL) { return head; } struct listnode *newhead; struct listnode *remaining; newhead = head->next; remaining = head->next->next; newhead->next = head; head->next = pairwise_swap(remaining); return newhead; }
int main (int argc, char *argv[]) { char ch; int choice; int ret_val = 0; struct node *head = NULL; int *data = NULL; int value = 0; int pos = -1; while (1) { printf("\nMENU\n\n"); printf("1. Insert at the front\n"); printf("2. Insert at a position\n"); printf("3. Delete from the front\n"); printf("4. Delete from a position\n"); printf("5. Display\n"); printf("6. Reverse the linked list\n"); printf("7. Pairwise Swap\n"); printf("\nEnter your choice\n"); scanf("%d", &choice); switch (choice) { case 1: printf("\nEnter a value\n"); scanf(" %d", &value); data = get_new_node(&value); ret_val = insert_node(&head, (void *)data); if (ret_val) printf("\nOperation failed\n"); break; case 2: printf("\nEnter the position at which to insert\n"); scanf(" %d", &pos); printf("\nEnter a value\n"); scanf(" %d", &value); data = get_new_node(&value); ret_val = insert_node_pos(&head, pos, (void *)data); if (ret_val) printf("\nOperation failed\n"); break; case 3: ret_val = delete_node(&head); if (ret_val) printf("\nOperation failed\n"); break; case 4: printf("\nEnter the position at which to delete\n"); scanf(" %d", &pos); ret_val = delete_node_pos(&head, pos); if (ret_val) printf("\nOperation failed\n"); break; case 5: print_list(head); break; case 6: head = reverse(head); break; case 7: pairwise_swap(&head); break; default: printf("\nWrong choice. Doing nothing\n"); } printf("Do you want to continue (y/n) ??"); scanf(" %c", &ch); if (ch == 'n' || ch == 'N') { printf("\nBreaking the driver loop\n"); break; } } return 0; }