Can you please help me find errors I made and fix ...? the program compiles but
ID: 3654254 • Letter: C
Question
Can you please help me find errors I made and fix ...?
the program compiles but I'm getting run-time errors
Instruction:
listNode zip(listNode *p, listNode *q);
Connect the first node of p to the first node of q. First node of q should connect to the second node of p, second node of p connects to second node of q, second node of q connects to third node of p etc.
Make sure you handle the case that if p has more elements than q (and vice-versa), you append the rest of the p to the end of the list.
p.txt
1
2
3
4
q.txt
5
6
7
8
9
10
Example output:
The first List is:
1 --> 2 --> 3 --> 4 --> NULL
The second List is:
5 --> 6 --> 7 --> 8 --> 9 --> 10 --> NULL
The new list is:
1 --> 5 --> 2 --> 6 --> 3 --> 7 --> 4 --> 8 --> 9 --> 10 --> NULL
Steps I followed....
1) Open the provided files: p.txt, q.txt
2) Create two separate linked lists
3) After populating each list, print out each list
4) Then call zip, and print the new list
5) Free the nodes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node {
int n;
struct node * next;
}Node, *listNode;
//prototypes
listNode zip(listNode p, listNode q); // zip the left and right
void printList(listNode currentPtr); // print the list
void insert(listNode *sPtr, int q); // insert at end
void freelist(listNode head); // free list
int main(int argc, char *argv[]){
if(argc!=3)
{
printf("Please enter output in the form of ./a.out p.txt q.txt ");
return 0;
}
listNode head = NULL;
listNode p = NULL;
listNode q = NULL;
int i=0;
FILE *fp = NULL;
fp = fopen(argv[0], "r");
if(fp==NULL)
{
printf("File not found. ");
return 0;
}
while(fscanf(fp, "%d", &i) !=EOF){
insert(*p, i);
}
fclose(fp);
fp = fopen(argv[1], "r");
if(fp==NULL)
{
printf("File not found. ");
return 0;
}
while(fscanf(fp, "%d", &i) !=EOF){
insert(*q, i);
}
fclose(fp);
printf("The first List is: ");
printList(p);
printf("The second List is: ");
printList(q);
head=zip(p, q);
printf("The new list is: ");
printList(head);
freelist(head);
return 0;
}
void insert(listNode *sPtr, int q){
listNode newPtr;
listNode previousPtr;
listNode currentPtr;
newPtr = malloc(sizeof(Node));
if(newPtr != NULL){
newPtr->n = q;
newPtr->next = NULL;
previousPtr = NULL;
currentPtr = *sPtr;
while(currentPtr != NULL){
previousPtr = currentPtr;
currentPtr = currentPtr->next;
}
if(previousPtr == NULL){ //handle new linked list
newPtr->next = currentPtr;
currentPtr= newPtr;
}
else{
previousPtr->next = newPtr;
}
}
else{
printf("%d not inserted. No memory.", q);
}
}
void printList(listNode currentPtr){
if(currentPtr == NULL)
printf("List is empty. ");
else{
while(currentPtr != NULL){
printf("%d --> ", currentPtr->n);
currentPtr = currentPtr->next;
}
printf("NULL ");
}
}
listNode zip ( listNode p, listNode q )
{
listNode result = NULL; // save the head of the new list
listNode tmp = NULL;
int count=0;
while (p!= NULL || q!=NULL)
{
if (p != NULL && q!= NULL) //both not null
{
if (count==0) //first time
{
result= p;
tmp=result;
p=p->next;
count++;
}
else if(count % 2 == 1) //link q
{
count++;
tmp->next=q;
q=q->next;
tmp=tmp->next;
}
else{ //link p
count++;
tmp->next=p;
p=p->next;
tmp=tmp->next;
}
}
else if (p!= NULL ) // append p to list
{
tmp->next=p;
}
else if (q!=NULL ) // append q to list
{
tmp->next=q;
}
}
return result;
}
void freelist(listNode head)
{
listNode cur;
while(1)
{
cur=head->next;
free(head);
head=cur;
}
return;
}