Hi everyone,
I am building a linked list by merging 2 sorted arrays.
My code is below. now my question is how can I make ptr->name point to list1[i] or list2[j] accroedingly in the if statement?
Thanks,
#include<string.h>
#include <stdio.h>
#define CHARS 30
#define NAMES 20
main()
{
struct NODE {
char name[CHARS];
struct NODE *next;
};
typedef struct NODE Node;
int count= -1;
int i,j,x;
char list1[NAMES][CHARS],list2[NAMES][CHARS];
FILE *fp;
Node* head;
Node* curr;
head=(Node*)malloc(sizeof(Node));
curr=head;
fp=fopen("merge.dat","r");
while (!feof(fp)) {
count++;
fscanf(fp,"%s %s ",list1[count],list2[count]);
}
fclose(fp);
while(i<count && j<count)
{
curr->next=(Node*)malloc(sizeof(Node));
curr=curr->next;
if(strcmp(list1[i],list2[j])<0)
{
curr->name=list1[i];//THIS IS THE PART THAT I HAVE A ? ON.
j++;
}
else
{
curr->name=list2[j];
i++;
}
}
if (i=count-1)
for(x=j;x<count;x++)
{
curr->next=(Node*)malloc(sizeof(Node));
curr=curr->next;
curr->name=list2[x];
}
else
for(x=i;x<count;x++)
{
curr->next=(Node*)malloc(sizeof(Node));
curr=curr->next;
curr->name=list1[x];
}
}