Esempio n. 1
0
 /** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
 void addAtIndex(int index, int val) {
     if (index < 0 || index > size) return;
     if (index == 0) {addAtHead(val); return;}
     if (index == size) {addAtTail(val); return;}
     Node *cur = head;
     for (int i = 0; i < index - 1; ++i) cur = cur->next;
     Node *t = new Node(val, cur->next);
     cur->next = t;
     ++size;
 }
Esempio n. 2
0
int main(void){
    Node *myList = NULL;
    Node *dupList = NULL;
    //Node *nn= newNode(4);
    Node *randomList =NULL;
    append(&myList, 10);
    //append(&myList, 15);
    printList(myList);

    myList= buildOneTwoThree();
    if(myList != NULL){
        printList(myList);
        printf("\nmyList length = %d; ", linkLength(myList));
    }

    push(&myList, 0);
    push(&(myList->next), 42);
    printList(myList);

    append(&myList,5);
    printList(myList);

    dupList = copyList(myList);
    printList(dupList);

    changeToNull(&dupList);
    printList(dupList);

    dupList=addAtHead();
    printList(dupList);

    dupList= buildWithSpecialCase();
    printList(dupList);

    dupList=buildWithDummyNode();
    printList(dupList);
 printf("\n List has %d 5.", countTest(dupList,5));
    printf("\n 3rd Element of list = %d", getNth(dupList, 3));
    //deleteList(&dupList);

    insertNthTest();

    //sortedInsert(&dupList, nn);
    printList(dupList);

    // random list
    insertNth(&randomList, 0, 13);
    insertNth(&randomList, 1, 13);
    insertNth(&randomList, 2, 27);
    insertNth(&randomList, 3, 55);
    insertNth(&randomList, 4, 55);

    printList(randomList);
    removeDuplicates(randomList);
    printList(randomList);

    //insertSort(&randomList);
    //printList(randomList);

    //testAppendList();

    //testFrontBackSplit();
    return 0;
}
Esempio n. 3
0
void push(Stack* stack, StackVal val) {
    addAtHead(stack->pList, (void*) val);
}