Jax Jax - 7 days ago 3
C Question

Check if the result list is the same as the expected list after insert and delete elements in a list

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
{50, 70, 80, 100, 77, 200, 44, 70, 6, 0}
. I expect the list with these elements
{0, 6, 70, 44, 200, 77, 100, 80, 70, 50}
, when then I insert in the middle the elements are
{5, 20, 10, 30, 7, 8, 2, 104, 1, 22}
, after the element "200", I expect the list as
{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) :

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:

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;
}

Answer

At first you need to create a linked list, and for that we should define a new type which is member of our list and can be linked to other members. it is something like this:

typedef struct SMember{
    int data;
    struct SMember *next;
    struct SMember *previous;
}Member;

now we should have a header for our list to count and get first and last members for insertion requirements, like this:

typedef struct sList{
    int count;
    struct SMember *first;
    struct SMember *last;
}List;

so we can use this:

#include <stdio.h>

typedef struct SMember{
    int data;
    struct SMember *next;
    struct SMember *previous;
}Member;

typedef struct SListHeader{
    int count;
    struct SMember *first;
    struct SMember *last;
}ListHeader;

void InsertAtBegning(int value, ListHeader *list)
{
    Member *newMember = (Member *)calloc(1, sizeof(Member));
    newMember->next=NULL;
    newMember->previous=NULL;

    newMember->data = value;
    if(list->count==0)
    {
        list->count = 1;
        list->first = newMember;
        list->last = newMember;
    }
    else{
        Member *tempMember;

        list->count++;
        tempMember = list->first;
        list->first = newMember;
        newMember->next = tempMember;
        tempMember->previous = newMember; 
    }
}

void InsertAtMiddle(int value, int middle, ListHeader *list)
{
    Member *newMember = (Member *)calloc(1, sizeof(Member));
    newMember->next=NULL;
    newMember->previous=NULL;

    newMember->data = value;
    if(list->count==0)
    {
        list->count = 1;
        list->first = newMember;
        list->last = newMember;
        return;
    }
    else{
        int i;
        Member *tempMember;

        for(i=0, tempMember = list->first; i < list->count; i++, tempMember=tempMember->next)
            if(tempMember->data == middle)
            {
                if(tempMember->next != NULL)
                {
                    Member *tempMemberNext = tempMember->next;
                    tempMember->next = newMember;
                    newMember->next = tempMemberNext;
                    newMember->previous = tempMember;
                    tempMemberNext->previous = newMember;
                }
                else{
                    tempMember->next = newMember;
                    newMember->previous = tempMember;
                    list->last = newMember;
                }
                list->count++;
                return;
            }
        //if we dont find the middle member we dont insert this member!
        free(newMember);
    }
}

void InsertAtEnd(int value, ListHeader *list)
{
    Member *newMember = (Member *)calloc(1, sizeof(Member));
    newMember->next=NULL;
    newMember->previous=NULL;

    newMember->data = value;
    if(list->count==0)
    {
        list->count = 1;
        list->first = newMember;
        list->last = newMember;
    }
    else{
        Member *tempMember;

        list->count++;
        tempMember = list->last;
        list->last = newMember;
        newMember->previous = tempMember;
        tempMember->next = newMember; 
    }
}

void DeleteFromMiddle(int middle, ListHeader *list)
{
    if(list->count==0)
    {
        return;
    }
    int i;
    Member *tempMember;

    for(i=0, tempMember = list->first; i < list->count; i++, tempMember=tempMember->next)
        if(tempMember->data == middle)
        {
            if(tempMember->next != NULL && tempMember->previous != NULL)
            {
                Member *tempMemberNext = tempMember->next;
                Member *tempMemberPrevious = tempMember->previous;
                tempMemberPrevious->next = tempMemberNext;
                tempMemberNext->previous = tempMemberPrevious;
            }
            else if(tempMember->previous == NULL && tempMember->next == NULL)
            {
                list->first = NULL;
                list->last = NULL;
            }
            else if(tempMember->next == NULL)
            {
                Member *tempMemberPrevious = tempMember->previous;
                tempMemberPrevious->next = NULL;
                list->last = tempMemberPrevious;
            }
            else if(tempMember->previous == NULL)
            {
                Member *tempMemberNext = tempMember->next;
                tempMemberNext->previous = NULL;
                list->first = tempMemberNext;
            }
            free(tempMember);
            list->count--;
            return;
        }
}

void DeleteFromEnd(ListHeader *list)
{
    if(list->count==0)
    {
        return;
    }
    else{
        Member *lastMember;

        list->count--;
        lastMember = list->last;
        list->last = lastMember->previous;
        if(lastMember->previous != NULL)
        {
            Member *beforeLastMember = lastMember->previous;
            beforeLastMember->next = NULL;
        }
        free(lastMember);
    }
}

void PrintList(ListHeader *list)
{
    int i;
    Member *tempMember;

    for(i=0, tempMember = list->first; i<list->count; i++, tempMember = tempMember->next)
    {
        printf("Member [%d] = %d\n", i, tempMember->data);
    }
}

int main(int argc, char *argv[])
{
    ListHeader list;
    list.count = 0;
    list.first = NULL;
    list.last = NULL;

    InsertAtBegning(1, &list);
    InsertAtBegning(2, &list);
    InsertAtBegning(3, &list);

    InsertAtMiddle(4, 1, &list);
    InsertAtMiddle(5, 2, &list);
    InsertAtMiddle(6, 5, &list);

    InsertAtEnd(7, &list);
    InsertAtEnd(8, &list);
    InsertAtEnd(9, &list);

    printf("==================================1\n");
    PrintList(&list);

    DeleteFromMiddle(1, &list);
    DeleteFromMiddle(2, &list);
    DeleteFromMiddle(3, &list);
    DeleteFromMiddle(4, &list);
    DeleteFromMiddle(5, &list);
    printf("==================================2\n");
    PrintList(&list);
    DeleteFromMiddle(6, &list);
    DeleteFromMiddle(7, &list);
    DeleteFromMiddle(8, &list);
    DeleteFromMiddle(9, &list);
    printf("==================================3\n");
    PrintList(&list);

    InsertAtBegning(1, &list);
    InsertAtBegning(2, &list);
    InsertAtBegning(3, &list);

    DeleteFromEnd(&list);

    printf("==================================4\n");
    PrintList(&list);
}

===========================================================

EDIT

===========================================================

After your comment I made some changes in your main and added two functions,

please check this out

#include <stdio.h>

typedef struct node
{
    int data;
    struct node *next;
};

struct node *head;

void insert_at_begning(int value)
{
    struct node *var;

    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, *var;
    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 *var,*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\n",value);
}

int delete_from_end()
{
    struct node *var, *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\n",temp->data);
    var->next=NULL;
    free(temp);
    return 0;
}

int check_results(int expected[], int count)
{
    struct node *temp;
    int i;

    if(head==NULL)
        return -1;

    if(expected[0]!=head->data)
        return -1;
    i=1;
    temp = head->next;
    while(temp->next != NULL && i<count)
    {
        temp = temp->next;
        if(expected[i]!=temp->data)
            return -1;
        i++;
    }
    return 0;
}

void printResults()
{
    struct node *temp;

    if(head==NULL)
        return;

    temp = head;
    printf("Results:");
    printf(" %d ", head->data);
    temp = head;
    while(temp->next != NULL)
    {
        temp = temp->next;
        printf(" %d ", temp->data);
    }
    printf("\n");
}

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]);
    }

    printResults();
    if(check_results(arrBeginingExpected, 10))
        printf("correct!\n");
    else
        printf("incorrect!\n");

    for(i=0; i<listsize; i++)
    {
        insert_at_middle(arrMiddle[i], 200);
    }

    printResults();
    if(check_results(arrMiddleExpected, 20))
        printf("correct!\n");
    else
        printf("incorrect!\n");

    for(i=0; i<listsize; i++)
    {
        insert_at_end(arrEnd[i]);
    }

    printResults();
    if(check_results(arrEndExpected, 30))
        printf("correct!\n");
    else
        printf("incorrect!\n");

    for(i=0; i<listsize; i++)
    {
        delete_from_middle(76);
    }

    printResults();
    if(check_results(arrDeleteSpecificExpected, 29))
        printf("correct!\n");
    else
        printf("incorrect!\n");

    for(i=0; i<2; i++)
    {
        delete_from_end();
    }

    printResults();
    if(check_results(arrDeleteFromEndExpected, 27))
        printf("correct!\n");
    else
        printf("incorrect!\n");

    //display_file(FILEDIR);

    return 0;
}

the result is:

Results: 0  6  70  44  200  77  100  80  70  50
correct!
Results: 0  6  70  44  200  22  1  104  2  8  7  30  10  20  5  77  100  80  70  50
correct!
Results: 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
correct!
data deleted from list is 76
data deleted from list is 76
data deleted from list is 76
data deleted from list is 76
data deleted from list is 76
data deleted from list is 76
data deleted from list is 76
data deleted from list is 76
data deleted from list is 76
Results: 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
correct!
data deleted from list is 2
data deleted from list is 80
Results: 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
correct!
Comments