In my code, I have to do compare two list, one of them should be dynamic array, another should be linked list. so I wrote something like this; Specially I do not sure for this function
node * insert(node *head, int num){
node *new = (node *) malloc(sizeof(node));
new->data = num;
new->next = NULL;
//if head is null execute
if (head == NULL){
head = new;
}
else{
node *current = head;
while (current->next != NULL){
current = current->next;
}
current->next = new;
}
return head;
}
And my whole code down below
#include <stdio.h>
#include <stdlib.h>
typedef struct nodes {
int data;
struct nodes * next;
}node;
typedef struct differents{
int n1,n2;
}different;
node * insert(node *head, int num);
different * compare(node *head, int * arr,int counter, int *count_dif);
void print_arr(int *arr,int counter);
void print_linked(node *head);
int main(){
int *arr,*temp;
node *head=NULL;
int counter=1,input,count_dif;
different *dif;
arr=(int*)malloc(sizeof(int));
printf("'0' for stop entering new number\n");
while (input !=0){
printf("Input:");
scanf(" %d",&input);
arr[counter-1]=input;
counter++;
temp=arr;
arr=(int*)calloc(counter,sizeof(int));
for (int i=0; i<counter-1; i++) arr[i]=temp[i];
free(temp);
}
counter-=2;
printf("You are entered %d num for the first list\nEnter %d num for the second list\n",counter,counter );
for (int i=0; i<counter; i++){
printf("Input:");
scanf(" %d",&input);
head=insert(head,input);
}
print_arr(arr,counter);
print_linked(head);
node *temp_head=head;
dif=compare(temp_head,arr,counter,&count_dif);
printf("There are %d different num in the lists\nThey are\nIn first list\tIn second list\n",count_dif );
for (int i=0; i<count_dif; i++){
printf("\t%d\t\t",dif[i].n1 );
printf("%d\n",dif[i].n2 );
}
}
node * insert(node *head, int num){
node *new = (node *) malloc(sizeof(node));
new->data = num;
new->next = NULL;
//if head is null execute
if (head == NULL){
head = new;
}
else{
node *current = head;
while (current->next != NULL){
current = current->next;
}
current->next = new;
}
return head;
}
different * compare(node *head, int * arr,int counter, int *count_dif){
different *dif,*temp;
int diff_num=1;
for (int i=0; i<counter; i++){
if (arr[i]!=head->data){
temp=dif;
dif=(different*)calloc(diff_num,sizeof(different));
for (int j=0; j<diff_num; j++) dif[j]=temp[j];
dif[diff_num-1].n1=arr[i];
dif[diff_num-1].n2=head->data;
if (temp!=NULL) free(temp);
diff_num++;
}
head=head->next;
}
*count_dif=diff_num-1;
return dif;
}
void print_arr(int *arr,int counter){
printf("first list:{ ");
for (int i=0; i<counter; i++) printf("%d ",arr[i] );
printf("}\n");
}
void print_linked(node *head){
printf("second list:{ ");
node *print=head;
while(print){
printf("%d ",print->data );
print=print->next;
}printf("}\n");
}
INPUT
5 4 6 3 0
5 2 4 3
Expected OUTPUT
first list:{ 5 4 6 3 }
second list:{ 5 2 4 3 }
There are 2 different num in the lists
They are
In first list In second list
4 2
6 4
OUTPUT
first list:{ 5 4 6 3 }
second list:{ 5 2 4 3 }
There are 2 different num in the lists
They are
In first list In second list
4 2
6 -281779616
If I enter more than 6 or 7 number, It works fine, But in little numbers, It gives me a garbage value.
In main
the variable input
isn't initialized but it's used like: while (input !=0){
before any assignment.
In compare
the variable dif
isn't initialized but it's used like: temp=dif;
before any assignment.
This code: for (int j=0; j<diff_num; j++) dif[j]=temp[j];
makes you access temp
out of bounds.
You probably want:
for (int j=0; j<diff_num-1; j++) dif[j]=temp[j];
^^
That said, you should really study what realloc
can do for you. Your code will be much easier to read and without the "copying bug".