Thread: Check if the result list is the same as the expected after list operations

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    9

    Check if the result list is the same as the expected after list operations

    I'm doing a basic `linked-list` program. Now I have this code below in the main method to check if the list operations have the expected result.

    For example when I insert at the beginning of the list the elements are
    Code:
    {50, 70, 80, 100, 77, 200, 44, 70, 6, 0}
    . I expect the list with these elements
    Code:
    {0, 6, 70, 44, 200, 77, 100, 80, 70, 50}
    , when then I insert in the middle the elements are
    Code:
    {5, 20, 10, 30, 7, 8, 2, 104, 1, 22}
    , after the element "200", I expect the list as
    Code:
    {0, 6, 70, 44, 200, 22, 1, 104, 2, 8, 7, 30, 10, 20, 5, 77, 100, 80, 70, 50}
    . And I want the same when I insert an element at the end of the list and for delete operations.

    So, I want to show a message "Correct" when the result of operations and expected result are equal, and "Incorrect" if its different. But I'm not having success doing this. Can you help to understand how this can be achievable ?

    This is the code I have, What I have commented is what I was trying to achieve my goal for the first case (insert at the begin) :

    Code:
    int main()
        {
            int i=0;
            int listsize=10;
        
            int arrBegining[] = {50, 70, 80, 100, 77, 200, 44, 70, 6, 0};
            int arrBeginingExpected[] = {0, 6, 70, 44, 200, 77, 100, 80, 70, 50};
        
            int arrMiddle[] = {5, 20, 10, 30, 7, 8, 2, 104, 1, 22};
            int arrMiddleExpected[] = {0, 6, 70, 44, 200, 22, 1, 104, 2, 8, 7, 30, 10, 20, 5, 77, 100, 80, 70, 50};
        
            int arrEnd[] = {40, 30, 20, 1, 7, 76, 4 , 0, 80, 2};
            int arrEndExpected[] = {0, 6, 70, 44, 200, 22, 1, 104, 2, 8, 7, 30, 10, 20, 5, 77, 100, 80, 70, 50, 40, 30, 20, 1, 7, 76, 4, 0, 80, 2};
        
            int arrDeleteSpecificExpected[] = {0, 6, 70, 44, 200, 22, 1, 104, 2, 8, 7, 30, 10, 20, 5, 77, 100, 80, 70, 50, 40, 30, 20, 1, 7, 4 , 0,  80 ,2};
            int arrDeleteFromEndExpected[] = {0, 6, 70, 44, 200, 22, 1, 104, 2, 8, 7, 30, 10, 20, 5, 77, 100, 80, 70, 50, 40, 30, 20, 1, 7, 4, 0};
        
            // int newArr[] = {};
        
            for(i=0;i<listsize;i++){
                insert_at_begning(arrBegining[i]);
                // newArr[i] = arrBegining[i];
            }
            // if(newArr == arrBegining){
               // printf("Correct");
            // }
    
    
            // else{
               // printf(Incorrect);
            // }
        
            for(i=0;i<listsize;i++){
                insert_at_middle(arrMiddle[i], 200);
            }
            for(i=0;i<listsize;i++){
                insert_at_end(arrEnd[i]);
            }
        
            for(i=0;i<listsize;i++){
                delete_from_middle(76);
            }
        
            for(i=0;i<2;i++){
                delete_from_end();
            }
        
            display_file(FILEDIR);
        
            return 0;
        }
    Restant code: (this code is working, Im just in doubt in the main method above to check if the expected and actual result are the same)

    Code:
      void insert_at_begning(int value)
        {
            var=(struct node *)malloc(sizeof (struct node));
            var->data=value;
            if(head==NULL)
            {
                head=var;
                head->next=NULL;
            }
            else
            {
                var->next=head;
                head=var;
            }
        }
        
        void insert_at_end(int value)
        {
            struct node *temp;
            temp=head;
            var=(struct node *)malloc(sizeof (struct node));
            var->data=value;
            if(head==NULL)
            {
                head=var;
                head->next=NULL;
            }
            else
            {
                while(temp->next!=NULL)
                {
                    temp=temp->next;
                }
                var->next=NULL;
                temp->next=var;
            }
        }
        
        void insert_at_middle(int value, int loc)
        {
            struct node *var2,*temp;
            var=(struct node *)malloc(sizeof (struct node));
            var->data=value;
            temp=head;
        
            if(head==NULL)
            {
                head=var;
                head->next=NULL;
            }
            else
            {
                while(temp->data!=loc)
                {
                    temp=temp->next;
                }
                var2=temp->next;
                temp->next=var;
                var->next=var2;
            }
        }
        
        int delete_from_middle(int value)
        {
            struct node *temp,*var;
            temp=head;
            while(temp!=NULL)
            {
                if(temp->data == value)
                {
                    if(temp==head)
                    {
                        head=temp->next;
                        free(temp);
                        return 0;
                    }
                    else
                    {
                        var->next=temp->next;
                        free(temp);
                        return 0;
                    }
                }
                else
                {
                    var=temp;
                    temp=temp->next;
                }
            }
            printf("data deleted from list is %d",value);
        }
        
        int delete_from_end()
        {
            struct node *temp;
            temp=head;
            while(temp->next != NULL)
            {
                var=temp;
                temp=temp->next;
            }
            if(temp ==head)
            {
                head=temp->next;
                free(temp);
                return 0;
            }
            printf("data deleted from list is %d",temp->data);
            var->next=NULL;
            free(temp);
            return 0;
        }
    Last edited by jaxy; 11-27-2016 at 04:51 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well you need to implement some kind of checkList function.

    Say
    void checkList ( int expected[], int numExpected );

    Which you would call at the appropriate juncture with say
    arrBeginingExpected( arrBeginingExpected, 10 ); // these are the 10 expected entries

    You already know how to traverse a list with say while(temp->next != NULL)

    All you then need is something to compare temp->data with expected[pos] and print an appropriate message.

    If temp is NULL before you reach the end of the expected array, or temp isn't NULL at the end, then those are errors also.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2016
    Posts
    9
    Thanks for your answer. But it seems that how you are saying its just to check if the two lists, the expected and the actual, have the same number of elements. But I want to show only "Correct" if the lists have the same elements. And I dont see how we can do that part, compare the lists to see if have the same elements.
    Last edited by jaxy; 11-28-2016 at 01:58 PM.

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by jaxy View Post
    Thanks for your answer. But it seems that how you are saying its just to check if the two lists, the expected and the actual, have the same number of elements. But I want to show only "Correct" if the lists have the same elements. And I dont see how we can do that part, compare the lists to see if have the same elements.
    No, that's not what he said. He said to compare the values. He just emphasized the fact that you also need to be aware that the lengths may be different.

    Anyway, this is easy. Do you know how to traverse a list? Do you know how to iterate through an array? Start with the list traversal and then add the array stuff.

  5. #5
    Registered User
    Join Date
    Nov 2016
    Posts
    9
    For example the way you are saying is something like below in the main to check if they are icual. But after create the method to check if they are icual, how we do in the for? We need to create a new list and also add the elements to that new list?

    Code:
    newBeginList[] = {}; // we should create a new list??
    for(i=0; i<listsize; i++)
    {
        insert_at_begning(arrBegining[i]);
        // if we have to create a new list, here what we do? we should also insert the values in this new list?
    }
    
    
    // so we a method to check if they are icual, but above how we should do?
    
    
    if(check_icual(arrBeginingExpected, newListBegin))
    {
        printf("correct");
    }
    else
    {
        printf("incorret");
    }

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Why would you create a new list? That would be useless since it is the list creation that we are testing. I'm not sure how you would create a new list anyway, since you are using a global "head" variable.

    Just compare the values in the list to those in the array. Try to write a function to do that and post it. It would take the array with the expected values and compare them to the current contents of the "head" list.

    At least that's how I understand what you're trying to do.

  7. #7
    Registered User
    Join Date
    Nov 2016
    Posts
    9
    Ok thanks, but what are the parameteres of that check function, the expected list and the actual list right? So, in the
    Code:
    if(check_icual(expectedList, ???)){}
    we need to pass the expected list, and how to pass the actual list that already has the elements of the insert_at_begning(arrBegining[i])?

  8. #8
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by jaxy View Post
    how to pass the actual list that already has the elements of the insert_at_begning(arrBegining[i])?
    I assume that "head" is a global variable and therefore you don't need to pass it in.

  9. #9
    Registered User
    Join Date
    Nov 2016
    Posts
    9
    I have this function and it works:

    Code:
    bool compareArrays(int a[], int b[]) {
        int i;
        for (int i=0;i<10;i++){
            for (int j=0;j<10;j++) {
                if (a[i] != b[i])
                    return false;
                else
                    return true;
            }
        }
        return 1;
    }
    But Im not see what to pass as argument in the main method:
    Code:
    if(compareArrays(arrBeginingExpected, ???? ){
    printf("correct");
    }
    else{
    printf("incorrect");
    }
    The only part of the code that its not in the question is this:

    Code:
    typedef struct node
    {
        int data;
        struct node *next;
    };
    struct node *head;
    [/code]

  10. #10
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by jaxy View Post
    I have this function and it works:
    No it doesn't. It has two major errors. Try to fix them.

    But Im not see what to pass as argument in the main method:
    Code:
    if(compareArrays(arrBeginingExpected, ???? ){
    There's no way to pass a linked list to that function and have it magically work. You need to write a similar function (once that one actually works) that loops through the list, comparing each element to the one at the same position in the array. It would be passed the "expected" array but does not need to be passed the list since "head" is global. That's the last time I'm going to say that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help in Linked List Operations
    By aren34 in forum C Programming
    Replies: 54
    Last Post: 03-22-2011, 08:35 PM
  2. Cannot use any List operations(using Ubuntu)
    By samsun387 in forum C++ Programming
    Replies: 5
    Last Post: 06-04-2010, 01:54 AM
  3. expected specifier-qualifier-list ip.h
    By quantt in forum C Programming
    Replies: 1
    Last Post: 02-12-2009, 10:24 AM
  4. Replies: 1
    Last Post: 01-24-2008, 01:07 PM
  5. putting search result into a linked list
    By jetfreggel in forum C Programming
    Replies: 4
    Last Post: 03-03-2003, 01:22 PM

Tags for this Thread