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. I expect the list with these elementsCode:{50, 70, 80, 100, 77, 200, 44, 70, 6, 0}, when then I insert in the middle the elements areCode:{0, 6, 70, 44, 200, 77, 100, 80, 70, 50}, after the element "200", I expect the list asCode:{5, 20, 10, 30, 7, 8, 2, 104, 1, 22}. And I want the same when I insert an element at the end of the list and for delete operations.Code:{0, 6, 70, 44, 200, 22, 1, 104, 2, 8, 7, 30, 10, 20, 5, 77, 100, 80, 70, 50}
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) :
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: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; }
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; }