// 5 — InsertNth() void InsertNthTest() { struct node* head = NULL; // start with the empty list InsertNth(&head, 0, 13); // build {13) InsertNth(&head, 1, 42); // build {13, 42} InsertNth(&head, 1, 5); // build {13, 5, 42} DeleteList(&head); // clean up after ourselves }
int main() { struct node*head=NULL; InsertNth(&head,0,13); InsertNth(&head,1,42); InsertNth(&head,1,5); printf("len=%d\n",length(head)); Print(head); }
main() { struct node* head = build123(); Push(&head, 0); printdata(head); InsertNth( &head, 0, 299); printdata(head); InsertNth( &head, 3, 845); printdata(head); }
void InsertNthTest() { struct node* head = NULL; InsertNth(&head, 0, 13); InsertNth(&head, 1, 42); InsertNth(&head, 1, 5); int len = Length(head); printf("%d\n", len); DeleteList(&head); len = Length(head); printf("%d\n", len); }
void InsertNthTest(void) { struct node *head = BuildOneTwoThree(); InsertNth(&head, 0, 13); InsertNth(&head, 1, 42); InsertNth(&head, 1, 5); DeleteList(&head); }
void InsertNthTest() { struct node* head = NULL; InsertNth(&head, 0, 13); InsertNth(&head, 1, 42); InsertNth(&head, 1, 5); DeleteList(&head); while(head!=NULL){ printf("%d ",head->data); head=head->next; } }
int main() { struct node *head = NULL; InsertNth(&head, 0, 13); InsertNth(&head, 1, 42); InsertNth(&head, 1, 5); PrintList(head); DeleteList(&head); InsertNth(&head, 5, 20); return 0; }
struct ListNode* insertionSortList(struct ListNode* head) { if(head == NULL || head->next == NULL) return head; struct ListNode* sortedList = NULL; struct ListNode* current = head; struct ListNode* temp = NULL; while(current != NULL) { temp = current; if(sortedList == NULL) { Push(&sortedList, temp->val); // PrintList(sortedList); } else { // trace through the whole sorted list find the right spot int counter = 0; struct ListNode* sortedListCur = sortedList; while(sortedListCur != NULL && sortedListCur->val < temp->val) { // printf("%d\t", sortedListCur->val); ++counter; sortedListCur = sortedListCur->next; } // printf("\nappend at %d\n", counter); InsertNth(&sortedList, counter, temp->val); } current = current->next; } return sortedList; }
// F**k loop indices, use recursion Node* InsertNth(Node *head, int data, int position) { Node *nn = new Node(); nn->data = data; nn->next = NULL; if (head == NULL) { return nn; } if (position == 0) { nn->next = head; return nn; } head->next = InsertNth(head->next, data, position-1); return head; }
void InsertNthTest() { struct node* head = NULL; int len = 0; len = Length(head); printf("Before insert, length of the list is: %d\n", len); // Print(head); InsertNth(&head, 0, 13); // Print(head); InsertNth(&head, 1, 42); // Print(head); InsertNth(&head, 2, 5); // Print(head); InsertNth(&head, 1, 3); // Print(head); InsertNth(&head, 0, 9); // Print(head); InsertNth(&head, 4, 11); // Print(head); len = Length(head); printf("After insert, length of the list is: %d\n", len); Print(head); }
main(){ struct node* head; makelist(&head); InsertNth(&head, 1, 11); print_list(head); }