Пример #1
0
int test_head(void)
{
    struct linkedlist *ll=initialize();
    struct node *newnode=NULL,*snode=NULL;
    newnode=inserthead(ll,init_nodedata("1","foo1"));
    newnode=inserthead(ll,init_nodedata("2","foo2"));
    append(ll,init_nodedata("999","bar999"));

    char *compare_to="2\0"; 
    snode=search(ll, (void *)compare_to, &compfn_head);
    char *headvalue=((struct nodedata *)(snode->data))->head;
    assert( !strcmp(headvalue,compare_to) );
    printf("search node data: %s\n",(char *)(headvalue));
    //delete(ll,NULL);
    printf("walk: ");
    walk(ll,"head: %s\n", &printfn_head);
    int cont=1;
    int pops=0;
    char *data;
    do {
        data=pop(ll);
        pops++;
        if (!data) { 
            headvalue=NULL;
            cont=0;}
        else {headvalue=((struct nodedata *)(data))->head;}
        printf("pop data: %s\n",headvalue);
        free(data);
    } while (cont);
    // we should call pop 4 times
    assert(pops==4);
    return 0;
}
Пример #2
0
int test_basic(void)
{
    struct linkedlist *ll=initialize();
    struct node *newnode=NULL,*snode=NULL;
    char *head_data;
    newnode=inserthead(ll,"1");
    newnode=inserthead(ll,"2");
    append(ll,"999");

    char *compare_to="2\0"; 
    snode=search(ll, (void *)compare_to, &compfn_basic);
    assert( !strcmp(snode->data,compare_to) );
    printf("search node data: %s\n",(char *)(snode->data));
    //delete(ll,NULL);
    printf("walk: ");
    walk(ll,"&s\n",&printfn_basic);
    int cont=1;
    int pops=0;
    do {
        head_data=pop(ll);
        pops++;
        printf("pop data: %s\n",(char *)head_data);
        if (!head_data) { cont=0;}
        //free(head_data);
    } while (cont);
    // we should call pop 4 times
    assert(pops==4);
    return 0;
}
Пример #3
0
int test_find_and_pop(void)
{
    struct linkedlist *ll=initialize();
    struct node *newnode=NULL,*snode=NULL;
    newnode=inserthead(ll,init_nodedata("1","foo1"));
    newnode=inserthead(ll,init_nodedata("2","foo2"));
    append(ll,init_nodedata("999","bar999"));
    assert( ll->count==3 );

    char *compare_to="2\0"; 
    struct nodedata *data=(struct nodedata *)find_and_pop(ll, (void *)compare_to, &compfn_head);
    assert (data);
    char *headvalue=data->head;
    assert( !strcmp(headvalue,compare_to) );
    assert( ll->count==2 );

    compare_to="1\0"; 
    data=(struct nodedata *)find_and_pop(ll, (void *)compare_to, &compfn_head);
    assert (data);
    headvalue=data->head;
    assert( !strcmp(headvalue,compare_to) );
    assert( ll->count==1 );

    compare_to="999\0"; 
    data=(struct nodedata *)find_and_pop(ll, (void *)compare_to, &compfn_head);
    assert (data);
    headvalue=data->head;
    assert( !strcmp(headvalue,compare_to) );
    assert( ll->count==0 );
}
Пример #4
0
/*
 * sortedInsert is recursive function.  It inserts a new node containing the variable data
 * into a sorted singly linked list that is provided as a parameter.  The data in the linked
 * should be in increasing order.
 *
 * It returns the pointer that the previous node in the linked list should point to.
 *
 * Feel free to call inserthead in your function.
 */
node *sortedInsert(node *head, int data){
    if (head == NULL) {
        return inserthead(head, data);
    } else if (data < head->info) {
        return inserthead(head, data);
    }else if (data >= head->info) {
        head->next = sortedInsert(head->next, data);
    }
    return head;
}
Пример #5
0
/* DO NOT CHANGE
 *
 * Test code.
 */
int main(void)
{
    node *oldList=NULL, *newList=NULL, *sortList=NULL;
    int i;

    /* seed random number generator */
    srand(time(NULL));

    /* Create a linked list */
    for(i=0; i<=N; i++){
        oldList = inserthead(oldList, 10*(N-i));
    }

    /* Print this linked list */
    printf("Old linked list: \n");
    printLinkedList(oldList);
    printf("\n");

    /* Copy linked list to a new one */
    newList = copyLinkedList(oldList);

    /* Print copied linked list */
    printf("Copied linked list: \n");
    printLinkedList(newList);
    printf("\n");

    /* Update values in linked lists */
    modifyLinkedList(oldList,-1);
    modifyLinkedList(newList,1);

    /* Print updated old linked list */
    printf("Numbers should be 1 less: \n");
    printLinkedList(oldList);
    printf("\n");

    /* Print updated new linked list */
    printf("Numbers should be 1 more: \n");
    printLinkedList(newList);
    printf("\n");

    /* Create a sorted linked list */
    for(i=0; i<=N; i++){
        sortList = sortedInsert(sortList, rand()%N);
    }

    /* Print updated old linked list */
    printf("Numbers should be in sorted order: \n");
    printLinkedList(sortList);
    printf("\n");

    /* Free created lists */
    freeLinkedList(oldList);
    freeLinkedList(newList);
    freeLinkedList(sortList);

    return 0;
}
Пример #6
0
/*-------------------------------------------------------------------*/
void main()
{
int a;
clrscr();
createlist(a);
addnode(a);
addnode(a);
addnode(a);
display();
inserthead(a);
display();
inserttail(a);
display();
delhead();
display();
deltail();
display();
getch();
}
Пример #7
0
int main()
{
    printf("Input values in the form of positive integers separated by spaces (\"1 2 3\" is proper input, \"1,2,3\" is not): ");

    int holding;
    cnode *head = malloc(sizeof(cnode));
    scanf("%d", &holding);
    head -> info = holding;
    head -> next = NULL;
    head -> previous = NULL;

    int input = 1; //repurposed code from recitation 1
    char junk = ' ';
    junk = getchar();
    if (junk != ' ')
        input = 0;
    while (input == 1)
    {
        if (scanf("%d", &holding) > 0)
            head = inserthead(head, holding);
        junk = getchar();
        if (junk != ' ')
            input = 0;
    }
    printf("All numbers inserted at the head.\n\n");

    printf("Doubly linked list: ");
    cnode *ptr = head;
    while (ptr -> next != NULL)
    {
        printf("%d, ", ptr -> info);
        ptr = ptr -> next;
    }
    printf("%d\n\n", ptr -> info);

    printf("To singly linked list: ");
    node *singleHead = CopytoSinglyLinked(head);
    node *point = singleHead;
    while (point -> next != NULL)
    {
        printf("%d, ", point -> info);
        point = point -> next;
    }
    printf("%d\n\n", point ->  info);

    printf("Input a node value to return the previous of (0 is head, this may require some counting!): ");
    int go;
    scanf("%d", &go);
    if (singleHead -> next != NULL && go != 0) //prevents a null pointer from being passed to Previous()
    {
        point = singleHead;
        int x;
        for (x = 0; x < go; x++)
            if (point -> next != NULL)
                point = point -> next;
            else
            {
                printf("Input out of bounds\n\n");
                point = singleHead; //prevents if statement below from being called
                break;
            }
        if (Previous(singleHead, point) != NULL)
            printf("\nNode given: %d, Previous node: %d\n\n", point -> info, Previous(singleHead, point) -> info);
    }
    else
        printf("NULL, no previous for value given\n\n");

    printf("Printed in reverse: ");
    PrintReverse(singleHead);

    printf("\nList without duplicates: ");
    singleHead = RemoveDuplicates(singleHead);
    point = singleHead;
    while(point -> next  != NULL)
    {
        printf("%d, ", point -> info);
        point = point -> next;
    }
    printf("%d\n", point -> info);

    return 0;
}