int main(){ init(); insert_tail(1); insert_tail(5); insert_tail(9); insert_in_order(7); insert_in_order(13); printf("有效数据个数是%d\n",size()); for_each(print_cb,NULL); deinit(); return 0; }
/* * Inserts a node in an ordered list. */ void insert(ordered_list_t *listp, /* input/output - ordered list */ int key) /* input */ { ++(listp->size); listp->headp = insert_in_order(listp->headp, key); }
Figure 14.34 Function insert and Recursive Function insert_in_order /* * Inserts a new node containing new_key in order in old_list, returning as * the function value a pointer to the first node of the new list */ list_node_t * insert_in_order(list_node_t *old_listp, /* input/output */ int new_key) /* input */ { list_node_t *new_listp; if (old_listp == NULL) { new_listp = (list_node_t *)malloc(sizeof (list_node_t)); new_listp->key = new_key; new_listp->restp = NULL; } else if (old_listp->key >= new_key) { new_listp = (list_node_t *)malloc(sizeof (list_node_t)); new_listp->key = new_key; new_listp->restp = old_listp; } else { new_listp = old_listp; new_listp->restp = insert_in_order(old_listp->restp, new_key); } return (new_listp); }
int main(int argc, char **argv) { /* int list[INTCNT] = {1,9,0,3,2,4,6,7,5,8,10,13,11,12,14}, i; */ int list[INTCNT] = {0,1,2,3,4,6,7,8,9,23,32,35,33,54,42}, i; tree_t *mytree = make_empty_tree(void_intcmp); /* Unbalanced */ for(i=0; i<INTCNT; i++) { insert_in_order(mytree, list+i); } print_tree(mytree); printf("------------------------\n"); free_tree(mytree); mytree = make_empty_tree(void_intcmp); /* Balanced */ int_balance_insert(mytree, list, INTCNT); print_tree(mytree); return 0; }
void rec_ins_pair(tree_t *tree, int *datalist, int n) { if(n<=0) { return; } insert_in_order(tree, datalist+n/2); rec_ins_pair(tree, datalist, n/2); rec_ins_pair(tree, datalist+n/2+1, n/2 - !(n%2)); }
int main(int argc, char **argv) { list_t *mylist; int i, find; mylist = make_empty_list(); for(i=100; i > 0; i--) { mylist = insert_in_order(mylist, i); } printf("Insert what value?: "); scanf("%d", &find); mylist = insert_in_order(mylist, find); printlist(mylist); return 0; }
void main() { WCValDList<int> list; insert_in_order( list, 5 ); insert_in_order( list, 20 ); insert_in_order( list, 1 ); insert_in_order( list, 25 ); cout << "List elements in ascending order:\n"; WCValDListIter<int> iter( list ); while( ++iter ) { cout << iter.current() << "\n"; } cout << "List elements in descending order\n"; // iterator is past the end of the list while( --iter ) { cout << iter.current() << "\n"; } }
int main(int argc, char **argv) { /* int list[INTCNT] = {1,9,0,3,2,4,6,7,5,8,10,13,11,12,14}, i; */ int list[INTCNT] = {1,2,3,4,6,7,0,8,9,23,32,35,33,54,42}, i; tree_t *mytree = make_empty_tree(void_intcmp); /* Unbalanced */ for(i=0; i<INTCNT; i++) { mytree = insert_in_order(mytree, list+i); } print_tree(mytree); printf("minimum: %d\n\n", *(int*)((node_t*)get_tree_head(mytree))->data); /* Unbalanced Tail */ mytree = get_tree_tail(mytree); print_tree(mytree); printf("minimum: %d\n", *(int*)((node_t*)get_tree_head(mytree))->data); free_tree(mytree); mytree = make_empty_tree(void_intcmp); /* Balanced */ int_balance_insert(mytree, list, INTCNT); print_tree(mytree); printf("minimum: %d\n", *(int*)((node_t*)get_tree_head(mytree))->data); /* Balanced Tail */ mytree = get_tree_tail(mytree); print_tree(mytree); printf("minimum: %d\n", *(int*)((node_t*)get_tree_head(mytree))->data); return 0; }
int main() { node_t head = NULL; node_t first = NULL; node_t second = NULL; node_t merged = NULL; int choice = 0; int ele; int pos; node_t node_pointer = NULL; int n; int is_cycle = 0; node_t list_cycle = NULL; int isPalindrome = 0; int intersection = 0; node_t resultant_of_addition = NULL; int k = 0; int m; while(1) { printf("--------------------------------------------------------------------------------------------------------\n"); printf("1. Add node in the front end \n"); printf("2. Display the list \n"); printf("3. Exit \n"); printf("4. Add node at the rear end of the list \n"); printf("5. Delete a node at the front end of the list \n"); printf("6. Delete a node from the rear end of the list \n"); printf("7. Insert a node in order to the list \n"); printf("8. Merge two ordered linked lists \n"); printf("9. Search for an item in the list \n"); printf("10. Delete a node whose value is specified \n"); printf("11. Delete a node at the specified position \n"); printf("12. Reverse list wihtout creating extra nodes \n"); printf("13. Delete a node given only a pointer to the node \n"); printf("14. Print middle element of the list \n"); printf("15. Print the nth last element in the list \n"); printf("16. Delete the entire list \n"); printf("17. Detect a loop in the list \n"); printf("18. Check whether a list is a palindrome \n"); printf("19. Find the intersection of two lists \n"); printf("20. Print reverse recursively \n"); printf("21. Remove duplicates in a sorted linked list \n"); printf("22. Move the last node in the list to the first node \n"); printf("23. Reverse the list pairwise \n"); printf("24. Find the intersection of two lists recursively \n"); printf("25. Delete alternate nodes in the list \n"); printf("26. Alternating split of the list \n"); printf("27. Delete nodes whose neighbours value is greater \n"); printf("28. Sepearate into even and odd in that order \n"); printf("29. Add two lists and give the resultant list \n"); printf("30. Rotate the list by k elements \n"); printf("31. Separate into 0s and 1s \n"); printf("32. Delete n nodes after the first m nodes \n"); printf("-------------------------------------------------------------------------------------------------------\n"); printf("Enter the choice\n"); scanf("%d", &choice); switch(choice) { case 1: printf("Enter the element to enter to the front end of the list \n"); scanf("%d", &ele); head = add_front(head, ele); break; case 2: display(head); break; case 3: exit(0); case 4: printf("Enter an element to be added to the end of the list \n"); scanf("%d", &ele); head = add_end(head, ele); break; case 5: head = delete_front(head); break; case 6: head = delete_rear(head); break; case 7: printf("Enter the element to be inserted \n"); scanf("%d", &ele); head = insert_in_order(head, ele); break; case 8: first = insert_in_order(first, 92); first = insert_in_order(first, 42); first = insert_in_order(first, 35); second = insert_in_order(second, 100); second = insert_in_order(second, 432); second = insert_in_order(second, 90); second = insert_in_order(second, 10); printf("The elements of the first list are: \n"); display(first); printf("The elements of the second list are \n"); display(second); merged = merge_ordered_lists(first, second); printf("The ordered list is: \n"); display(merged); case 9: printf("Enter the element of the list to be searched for \n"); scanf("%d", &ele); int pos = search(head, ele); if(pos != -1) { printf("The element is found at %d: \n", pos); } else { printf("The element is not found in the list \n"); } break; case 10: printf("Enter the element of the list to be deleted: \n"); scanf("%d", &ele); head = delete_with_info(head, ele); if(head == (node_t)NULL) { printf("The list is empty or the element u specified is not found: \n"); } break; case 11: printf("Enter the position with the first node as position 1 \n"); scanf("%d", &ele); head = delete_with_pos(head, ele); if(head == (node_t)NULL) { printf("Either the list is empty or the position specified is not appropriate \n"); } break; case 12: head = reverse(head); break; case 13: node_pointer = head -> link -> link; delete_node_given_pointer(node_pointer); break; case 14: print_middle(head); break; case 15: printf("Enter the value of n \n"); scanf("%d",&n); print_nth_last(head, n); break; case 16: head = delete_list(head); break; case 17: list_cycle = add_end(list_cycle,1); list_cycle = add_end(list_cycle,2); list_cycle = add_end(list_cycle,3); list_cycle = add_end(list_cycle,4); list_cycle = add_end(list_cycle,5); list_cycle -> link -> link -> link -> link -> link = list_cycle -> link; is_cycle = find_cycle(list_cycle); if(is_cycle) { printf("There is a cycle in the list \n"); } else { printf("There is no cycle in the list \n"); } break; case 18: isPalindrome = is_palindrome(&head, head); if(isPalindrome) { printf("The list is a palindrome \n"); } else { printf("The list is not a palindrome \n"); } break; case 19: first = add_end(first,10); first = add_end(first,20); second = add_end(second,43); second = add_end(second,3); second = add_end(second,34); second = add_end(second,44); first -> link -> link = second -> link; intersection = find_intersection(first, second); printf("The intersection point of the two lists are %d \n", intersection); break; case 20: print_reverse_recursively(head); printf("\n"); break; case 21: remove_duplicates(head); break; case 22: head = move_last_to_first(head); break; case 23: head = pairwise_reverse(head); break; case 24: first = add_end(first, 10); first = add_end(first, 30); first = add_end(first, 40); first = add_end(first, 50); first = add_end(first, 60); second = add_end(second, 10); second = add_end(second, 20); second = add_end(second, 30); find_common_recursively(first, second); break; case 25: head = delete_alternate(head); break; case 26: alternating_split_v2(head); break; case 27: head = delete_node_when_neigbour_higher(head); break; case 28: head = separate_into_even_odd_v2(head); break; case 29: first = add_front(first, 2); first = add_front(first, 4); first = add_front(first, 8); second = add_front(second,2); second = add_front(second,4); second = add_front(second,5); second = add_front(second,3); resultant_of_addition = add_two_lists(first, second); printf("The resultant list is as follows \n"); display(resultant_of_addition); break; case 30: printf("Enter the value of k \n"); scanf("%d",&k); head = rotate_by_k(head, k); break; case 31: head = separate_into_zeroes_ones(head); break; case 32: printf("Enter the value of m \n"); scanf("%d", &m); printf("Enter the value of n \n"); scanf("%d", &n); head = retain_m_delte_n(head, m , n); break; default: printf("Invalid choice... Please try again \n"); break; } } }
/* Parameters library: Pointer to start of library array song_artist: Song Artist song_name: Song Name */ void add_song(struct song_node* *library, char *song_artist, char *song_name) { library[ letter_to_index(song_artist) ] = insert_in_order( library[ letter_to_index(song_artist) ], song_artist, song_name ); }